#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
using  namespace std;
typedef long long ll;
#define pd(x) printf("%d\n",x)
#define cls(a,x) memset(a,x,sizeof(a))
const double eps=1e-8;
const double PI=acos(-1.0);
const int INF=1e9+10;
const int MOD=1e9+7;
const int N=2e5+10;
int a[N],vis[N],v[N],vis2[N];
priority_queue<int,vector<int> ,greater<int> >q;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        while(!q.empty()) q.pop();
        memset(vis,0,sizeof(vis));
//        memset(vis2,0,sizeof(vis2));
        memset(v,0,sizeof(v));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]),vis[a[i]]++;
        for(int i=1; i<=n; i++)
            if(!vis[i])
                q.push(i);
        int ans=0;
        for(int i=1; i<=n; i++)
        {
//            printf("%d %d %d\n",i,a[i],vis[a[i]]);
            if(vis[a[i]]<=1)
            {
                v[a[i]]=1;
                continue;
            }
            else
            {
                if(!q.empty())
                {
                    int x=q.top();
                    if(x>a[i]&&!v[a[i]])
                    {
                        v[a[i]]=1;
                        vis[a[i]]--;
                        continue;
                    }
                    else
                    {
//                        if(vis[a[i]]==1)
//                        {
//                            vis[a[i]]--;
//                        }
                        ans++;
                        vis[a[i]]--;
                        a[i]=x;
                        q.pop();
                        v[a[i]]=1;
                    }
                }
                else
                {
                    v[a[i]]=1;
                    vis[a[i]]--;
                }
            }
        }
        printf("%d\n",ans);
        for(int i=1; i<=n; i++)
            printf("%d%c",a[i],i==n?'\n':' ');
    }
    return 0;
}


#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<functional>
using  namespace std;
typedef long long ll;
#define pd(x) printf("%d\n",x)
#define cls(a,x) memset(a,x,sizeof(a))
const double eps=1e-8;
const double PI=acos(-1.0);
const int INF=1e9+10;
const int MOD=1e9+7;
const int N=2e5+10;
int a[N],vis[N],v[N],vis2[N];
priority_queue<int,vector<int> ,greater<int> >q;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        while(!q.empty()) q.pop();
        memset(vis,0,sizeof(vis));
//        memset(vis2,0,sizeof(vis2));
        memset(v,0,sizeof(v));
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]),vis[a[i]]++;
        for(int i=1; i<=n; i++)
            if(!vis[i])
                q.push(i);
        int ans=0;
        for(int i=1; i<=n; i++)
        {
//            printf("%d %d %d\n",i,a[i],vis[a[i]]);
            if(v[a[i]])
            {
                 int x=q.top();
                 ans++;
                vis[a[i]]--;
                a[i]=x;
                 q.pop();
                v[a[i]]=1;
                  continue;
            }
            if(vis[a[i]]<=1)
            {
                v[a[i]]=1;
                continue;
            }
            else
            {
                if(!q.empty())
                {
                    int x=q.top();
                    if(x>a[i]&&!v[a[i]])
                    {
                        v[a[i]]=1;
                        vis[a[i]]--;
                        continue;
                    }
                    else
                    {
                        ans++;
                        vis[a[i]]--;
                        a[i]=x;
                        q.pop();
                        v[a[i]]=1;
                    }
                }
                else
                {
                    v[a[i]]=1;
                    vis[a[i]]--;
                }
            }
        }
        printf("%d\n",ans);
        for(int i=1; i<=n; i++)
            printf("%d%c",a[i],i==n?'\n':' ');
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>

using namespace std;

const int MAXN = 200000 + 10;
typedef pair<int,int> PI;

int A[MAXN];
int result[MAXN];
int marked[MAXN];
set<int> S;
map<int,int> imap;
map<int, vector<int> > imapv;
vector<int> my_array;

int find_result(int n) {

	int start = 1, opt_result = 0;

	for(set<int>::iterator iter = S.begin(); iter != S.end(); iter++) {
		int count = imap[*iter];
		opt_result = opt_result + count - 1;
	}

	for(int i = 0; i < n; i++) {
		int value = my_array[i];
		int count = imap[value];
		if(count == 1 && marked[value] == 0) {
			result[i] = value;
			continue;
		}

		while(S.find(start) != S.end()) {
			start++;
		}

		if(start < my_array[i] || marked[value]) {
			result[i] = start;
			imap[value] = imap[value] - 1;
			start++;
		}else {
			result[i] = value;
			marked[value] = 1;
		}

	}

	return opt_result;
}

int main() {
	int n;
	cin>>n;
	for(int i = 0; i < n; i++) {
		int v;
		cin>>v;
		my_array.push_back(v);
		imap[v]++;
		if(imapv.find(v) == imapv.end())  {
			imapv[v] = vector<int>();
			imapv[v].push_back(i);
		}else {
			imapv[v].push_back(i);
		}
		S.insert(v);
	}

	cout<<find_result(n)<<endl;

	for(int i = 0; i < n; i++) {
		if(i) cout<<" ";
		cout<<result[i];
	}

	cout<<endl;

	return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

const int MAXN = 200000 + 10;
int A[MAXN];
map<int,int> imap;
set<int> S;
vector<int> result;

int find_result(int n) {
	int start = 1, opt_result = 0;
	for(set<int>::iterator iter = S.begin(); iter != S.end(); iter++) {
		int value = *iter;
		int count = imap[value];
		opt_result = opt_result + count - 1;

		result.push_back(value);

		if(count > 1) {
			for(int i = 0; i < count - 1; i++) {
				while(S.find(start) != S.end()) {
					start++;
				}
				result.push_back(start);
				start++;
			}
		}
	}

	return opt_result;
}

int main() {
	int n;
	cin>>n;
	for(int i = 0; i < n; i++) {
		cin>>A[i];
		imap[A[i]]++;
		S.insert(A[i]);
	}

	sort(A, A + n);

	cout<<find_result(n)<<endl;

	for(int i = 0; i < n; i++) {
		if(i) cout<<" ";
		cout<<result[i];
	}

	cout<<endl;

	return 0;
}


/*******************************\
   Name:    REAJUL HAQUE REAYZ  |
   School:  COMILLA UNIVERSITY  |
            CSE (5TH BATCH)     |
********************************/
#include<bits/stdc++.h>
#define endl "\n"
#define inf 1<<30
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ss stringstream
#define all(v) v.begin(), v.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define bit_cnt(mask) __builtin_popcount(mask)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1000000007; /// 1e9+7
const int MAX = 200005;     /// 1e5+5
ll bigmod(ll n, ll r)
{
    if(r==0) return 1;
    if(r==1) return n;
    if(r%2==0)
    {
        ll ret = bigmod(n, r/2);
        return ((ret%MOD) * (ret%MOD)) % MOD;
    }
    else return ((n%MOD) * (bigmod(n,r-1)%MOD)) % MOD;
}
ll modinverse(ll a){return bigmod(a,MOD-2)%MOD;}
ll lcm(ll a, ll b) {a=abs(a); b=abs(b); return (a/__gcd(a,b))*b;}
ll gcd(ll a, ll b) {a=abs(a); b=abs(b); if(!b) return a; return __gcd(b,a%b);}

int n, a[MAX], cnt[MAX], ans[MAX], now, flag[MAX];
set<int>s;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i], cnt[a[i]]++, s.insert(a[i]);
    now=1;
    for(int i=1; i<=n; i++){
        if(cnt[a[i]]==1){
            if(flag[a[i]]) {
                while(cnt[now]!=0) now++;
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            else ans[i]=a[i], flag[a[i]]=1;
        }
        else {
            while(cnt[now]!=0) now++;
            if(now>a[i]){
                ans[i]=a[i];
                flag[a[i]]=1;
            }
            else {
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            cnt[a[i]]--;
        }
    }
    int sz = s.size();
    cout<<n-sz<<endl;
    for(int i=1; i<=n; i++) cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


/*******************************\
   Name:    REAJUL HAQUE REAYZ  |
   School:  COMILLA UNIVERSITY  |
            CSE (5TH BATCH)     |
********************************/
#include<bits/stdc++.h>
#define endl "\n"
#define inf 1<<30
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ss stringstream
#define all(v) v.begin(), v.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define bit_cnt(mask) __builtin_popcount(mask)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1000000007; /// 1e9+7
const int MAX = 200005;     /// 1e5+5
ll bigmod(ll n, ll r)
{
    if(r==0) return 1;
    if(r==1) return n;
    if(r%2==0)
    {
        ll ret = bigmod(n, r/2);
        return ((ret%MOD) * (ret%MOD)) % MOD;
    }
    else return ((n%MOD) * (bigmod(n,r-1)%MOD)) % MOD;
}
ll modinverse(ll a){return bigmod(a,MOD-2)%MOD;}
ll lcm(ll a, ll b) {a=abs(a); b=abs(b); return (a/__gcd(a,b))*b;}
ll gcd(ll a, ll b) {a=abs(a); b=abs(b); if(!b) return a; return __gcd(b,a%b);}

int a[MAX], cnt[MAX], ans[MAX], flag[MAX];
set<int>s;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n; cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i];
        cnt[a[i]]++;
        s.insert(a[i]);
    }
    for(int i=1; i<=n; i++){
        if(cnt[a[i]]==1){
            ans[i]=a[i];
            flag[a[i]]=1;
        }
    }
    int now=1;
    for(int i=1; i<=n; i++){
        if(ans[i]) continue;
        if(cnt[a[i]]==1){
            if(flag[a[i]]) {
                while(cnt[now]!=0) now++;
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            else {
                ans[i]=a[i];
                flag[a[i]]=1;
            }
        }
        else {
            while(cnt[now]!=0) now++;
            if(now>=a[i]){
                ans[i]=a[i];
                flag[a[i]]=1;
            }
            else {
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            cnt[a[i]]--;
        }
    }
    int sz = s.size();
    cout<<n-sz<<endl;
    for(int i=1; i<=n; i++) cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


/*******************************\
   Name:    REAJUL HAQUE REAYZ  |
   School:  COMILLA UNIVERSITY  |
            CSE (5TH BATCH)     |
********************************/
#include<bits/stdc++.h>
#define endl "\n"
#define inf 1<<30
#define pi acos(-1.0)
#define pb push_back
#define mp make_pair
#define ss stringstream
#define all(v) v.begin(), v.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define bit_cnt(mask) __builtin_popcount(mask)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MOD = 1000000007; /// 1e9+7
const int MAX = 200005;     /// 1e5+5
ll bigmod(ll n, ll r)
{
    if(r==0) return 1;
    if(r==1) return n;
    if(r%2==0)
    {
        ll ret = bigmod(n, r/2);
        return ((ret%MOD) * (ret%MOD)) % MOD;
    }
    else return ((n%MOD) * (bigmod(n,r-1)%MOD)) % MOD;
}
ll modinverse(ll a){return bigmod(a,MOD-2)%MOD;}
ll lcm(ll a, ll b) {a=abs(a); b=abs(b); return (a/__gcd(a,b))*b;}
ll gcd(ll a, ll b) {a=abs(a); b=abs(b); if(!b) return a; return __gcd(b,a%b);}

int a[MAX], cnt[MAX], ans[MAX], flag[MAX];
set<int>s;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n; cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>a[i];
        cnt[a[i]]++;
        s.insert(a[i]);
    }
    for(int i=1; i<=n; i++){
        if(cnt[a[i]]==1){
            ans[i]=a[i];
            flag[a[i]]=1;
        }
    }
    int now=1;
    for(int i=1; i<=n; i++){
        if(ans[i]) continue;
        if(cnt[a[i]]==1){
            if(flag[a[i]]) {
                while(cnt[now]!=0) now++;
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            else {
                ans[i]=a[i];
                flag[a[i]]=1;
            }
        }
        else {
            cnt[a[i]]--;
            if(flag[a[i]]) {
                while(cnt[now]!=0) now++;
                ans[i]=now;
                flag[now]=1;
                cnt[now]=1;
                now++;
            }
            else {
                while(cnt[now]!=0) now++;
                if(now>=a[i]){
                    ans[i]=a[i];
                    flag[a[i]]=1;
                }
                else {
                    ans[i]=now;
                    flag[now]=1;
                    cnt[now]=1;
                    now++;
                }
            }

        }
    }
    int sz = s.size();
    cout<<n-sz<<endl;
    for(int i=1; i<=n; i++) cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i=a;i<=b;++i)
#define rev(i,a,b)  for(int i=a;i>=b;--i)
#define reg(i,g,u)  for(int i=h[u];i;i=g[i].nxt)
#define Inf (1<<30)
using namespace std;
typedef long long LL;
typedef double db;
inline void read(int &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void read(LL &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void write(int data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
inline void write(LL data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
int n,cnt;
int a[200005],v[200005],s[200005];
int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		if (!s[a[i]])
			cnt++,v[a[i]]=true;
		s[a[i]]++;
	}
	write(n-cnt),puts("");
	int q=1;
	rep(i,1,n)
		if (s[a[i]]>1){
			while (v[q])	q++;
			if (s[a[i]]>n||q<a[i])	s[a[i]]--,a[i]=q++;
			else s[a[i]]=Inf;
		}
	rep(i,1,n)	write(a[i]),putchar(' ');
}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i=a;i<=b;++i)
#define rev(i,a,b)  for(int i=a;i>=b;--i)
#define reg(i,g,u)  for(int i=h[u];i;i=g[i].nxt)
#define Inf (1<<30)
using namespace std;
typedef long long LL;
typedef double db;
inline void read(int &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void read(LL &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void write(int data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
inline void write(LL data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
int n,cnt;
int a[200005],v[200005],s[200005];
int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		if (!s[a[i]])
			cnt++,v[a[i]]=true;
		s[a[i]]++;
	}
	write(n-cnt),puts("");
	int q=1;
	rep(i,1,n)
		if (s[a[i]]>1){
			while (v[q])	q++;
			s[a[i]]--;
			if (s[a[i]]==Inf||q<a[i])	a[i]=q++;
			else s[a[i]]=Inf;
		}
	rep(i,1,n)	write(a[i]),putchar(' ');
}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i=a;i<=b;++i)
#define rev(i,a,b)  for(int i=a;i>=b;--i)
#define reg(i,g,u)  for(int i=h[u];i;i=g[i].nxt)
#define Inf (1<<30)
using namespace std;
typedef long long LL;
typedef double db;
inline void read(int &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void read(LL &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void write(int data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
inline void write(LL data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
int n,cnt;
int a[200005],v[200005],s[200005];
int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		if (!s[a[i]])
			cnt++,v[a[i]]=true;
		s[a[i]]++;
	}
	write(n-cnt),puts("");
	int q=1;
	rep(i,1,n)
		if (s[a[i]]>1){
			while (v[q])	q++;
			if (s[a[i]]==Inf||q<a[i])	s[a[i]]--,a[i]=q++;
			else s[a[i]]=Inf;
		}
	rep(i,1,n)	write(a[i]),putchar(' ');
}


#include<bits/stdc++.h>
#define rep(i,a,b)  for(int i=a;i<=b;++i)
#define rev(i,a,b)  for(int i=a;i>=b;--i)
#define reg(i,g,u)  for(int i=h[u];i;i=g[i].nxt)
#define Inf (1<<30)
using namespace std;
typedef long long LL;
typedef double db;
inline void read(int &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void read(LL &data){
    data=0;int e=1; char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-')    e=-1,ch=getchar();
    while (ch>='0'&&ch<='9')  data=data*10+ch-48,ch=getchar();
    data*=e;
}
inline void write(int data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
inline void write(LL data){
    if (data<0)  data=-data,putchar('-');
    if (data>9)  write(data/10);
    putchar(data%10+48);
}
int n,cnt;
int a[200005],v[200005],s[200005];
int main(){
	read(n);
	rep(i,1,n){
		read(a[i]);
		if (!s[a[i]])
			cnt++,v[a[i]]=true;
		s[a[i]]++;
	}
	write(n-cnt),puts("");
	int q=1;
	rep(i,1,n)
		if (s[a[i]]>1){
			while (v[q])	q++;
			if (s[a[i]]==Inf||q<a[i])	a[i]=q++;
			else s[a[i]]=Inf;
		}
	rep(i,1,n)	write(a[i]),putchar(' ');
}


#include<cstdio>
#include<algorithm>
using namespace std;
int n,hd,tl,ans,a[200005],num[200005],lasts[200005],q[200005];
int main()
{
	scanf("%d",&n);
	for (int i=1; i<=n; i++)
	{
		scanf("%d",&a[i]);
		num[a[i]]++;
	}
	hd=1; tl=0;
	for (int i=1; i<=n; i++)
		if (!num[i]) q[++tl]=i;
	for (int i=1; i<=n; i++)
	{
		if (num[a[i]]>1 && q[hd]<a[i])
		{
			num[a[i]]--;
			ans++;
			a[i]=q[hd++];
		}
		if (num[a[i]]>1 && q[hd]>a[i])
		{
			if (!lasts[a[i]]) lasts[a[i]]=1;
			else
			{
				num[a[i]]--;
				ans++;
				a[i]=q[hd++];
			}
		}
	}
	printf("%d\n",ans);
	for (int i=1; i<=n-1; i++)
		printf("%d ",a[i]);
	printf("%d\n",a[n]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[200003];
vector<int> v;
queue<int> num;
set<int> s;
int main(){
	v.clear();
	int n;
	cin >> n;
	for(int i = 0;i < n;++i){
		int j;
		cin >> j;
		v.push_back(j);
		cnt[j]++;
	}
	int q = 0;
	for(int i = 1;i <= n;++i){
		if(cnt[i] == 0) num.push(i),++q;
		else if(cnt[i] > 1) s.insert(i);
	}
	for(int i = 0;i < n;++i){
		if(s.find(v[i]) != s.end() && !num.empty()){
			int jizz = v[i];
			if(cnt[v[i]] == 2) s.erase(v[i]);
			if(num.front() < v[i]){
				v[i] = num.front();
				num.pop();
			} else if(cnt[v[i]] == 1){
				v[i] = num.front();
				num.pop();
			}
			--cnt[jizz];
		}
	}
	cout << q << "\n";
	for(int i = 0;i < n;++i) cout << v[i] << " ";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[200003];
vector<int> v;
queue<int> num;
set<int> s;
int main(){
	int n;
	cin >> n;
	for(int i = 0;i < n;++i){
		int j;
		cin >> j;
		v.push_back(j);
		cnt[j]++;
	}
	int q = 0;
	for(int i = 1;i <= n;++i){
		if(cnt[i] == 0) num.push(i),++q;
		else if(cnt[i] > 1) s.insert(i);
	}
	for(int i = 0;i < n;++i){
		if(s.find(v[i]) != s.end() && !num.empty()){
			int jizz = v[i];
			//cout << i << " " << v[i] << " : " << cnt[v[i]] << "\n";
			if(num.front() < v[i]){
				v[i] = num.front();
				num.pop();
			} else if(cnt[v[i]] == 1){
				v[i] = num.front();
				num.pop();
			}
			--cnt[jizz];
		}
	}
	//cout << num.size() << "\n";
	cout << q << "\n";
	for(int i = 0;i < n;++i) cout << v[i] << " ";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[200003];
vector<int> v;
queue<int> num;
set<int> s;
int main(){
	v.clear();
	int n;
	cin >> n;
	for(int i = 0;i < n;++i){
		int j;
		cin >> j;
		v.push_back(j);
		cnt[j]++;
	}
	int q = 0;
	for(int i = 1;i <= n;++i){
		if(cnt[i] == 0) num.push(i),++q;
		else if(cnt[i] > 1) s.insert(i);
	}
	for(int i = 0;i < n;++i){
		if(s.find(v[i]) != s.end() && !num.empty()){
			int jizz = v[i];
			if(num.front() < v[i]){
				v[i] = num.front();
				num.pop();
			} else if(cnt[v[i]] == 1){
				v[i] = num.front();
				num.pop();
			}
			--cnt[jizz];
		}
	}
	cout << q << "\n";
	for(int i = 0;i < n;++i) cout << v[i] << " ";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int cnt[200003],n[200003];
queue<int> q; // which number doesn't appear in array
set<int> s,used; // 1.which number appear too many times in array
				// 2.to check if use the number
int main(){
	int t;
	cin >> t;
	for(int i = 0;i < t;++i) cin >> n[i],cnt[n[i]]++;
	int a = 0;
	for(int i = 1;i <= t;++i){
		if(cnt[i] == 0) q.push(i),a++;
		else if(cnt[i] > 1) s.insert(i);
	}
	for(int i = 0;i < t;++i){
		if(s.find(n[i]) != s.end() && !q.empty()){
			int jizz = n[i];
			if(used.find(n[i]) == used.end()){ // not use before, need to compare
				if(cnt[jizz] == 1){
					continue;
				} else if(q.front() < jizz){
					n[i] = q.front();
					q.pop();
					cnt[jizz]--;
				} else {
					used.insert(n[i]);
				}
			} else { // used before, no need to compare anymore
				n[i] = q.front();
				q.pop();
			}
		}
	}
	cout << a << "\n";
	for(int i = 0;i < t;++i) cout << n[i] << " ";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase2[200005];
ll arr[3000000];
ll skip[200005];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
    reverse(nai.begin(),nai.end());
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        cout<<nai.size()<<endl;
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
                continue;
            }
            if(nai.size()==0)
            {
                continue;
            }
            ll now_choto=nai[nai.size()-1];
            if(arr[i]>now_choto)
            {
                arr[i]=now_choto;

                nai.pop_back();
            }
            else if(arr[i]<now_choto)
            {
                if(skip[arr[i]]==0)
                {
                    skip[arr[i]]=1;
                    continue;
                }
                else
                {
                    arr[i]=now_choto;

                     nai.pop_back();
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase[200005];
ll ase2[200005];
ll arr[3000000];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase[arr[i]]=i;
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
//    cout<<total_option<<endl;
    reverse(nai.begin(),nai.end());
//    for(ll i=0;i<nai.size();i++)
//    {
//        cout<<nai[i]<<" ";
//    }
//    cout<<endl;
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        cout<<nai.size()<<endl;
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
               continue;
            }
            if(ase2[arr[i]]>1)
            {
                if(nai.size()==0)
                {
                    continue;
                }
                ll now_choto=nai[nai.size()-1];
//                cout<<now_choto<<endl;
                if(arr[i]>now_choto)
                {
//                    cout<<"dhukse"<<endl;
                    total_option--;
                    arr[i]=now_choto;
//                    cout<<i<<" "<<arr[i]<<endl;
                    nai.pop_back();

                }
                else if(arr[i]<now_choto)
                {
                    if(total_option>nai.size())
                    {
//                        cout<<"dhukse"<<endl;
                        total_option--;
                        continue;
                    }
                    else if(total_option==nai.size())
                    {
                        if(ase[arr[i]]==i)
                        {
                            total_option--;
                            arr[i]=now_choto;
                            nai.pop_back();
                        }
                    }
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase2[200005];
ll arr[3000000];
ll skip[200005];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
    reverse(nai.begin(),nai.end());
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        cout<<nai.size()<<endl;
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
                continue;
            }
            if(nai.size()==0)
            {
                continue;
            }
            ll now_choto=nai[nai.size()-1];
            if(arr[i]>now_choto)
            {
                ase2[arr[i]]--;
                arr[i]=now_choto;
                nai.pop_back();
            }
            else if(arr[i]<now_choto)
            {
                if(skip[arr[i]]==0)
                {
                    skip[arr[i]]=1;
                    continue;
                }
                else
                {
                    arr[i]=now_choto;
                    skip[arr[i]]=0;
                     nai.pop_back();
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase[200005];
ll ase2[200005];
ll arr[3000000];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase[arr[i]]=i;
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
//    cout<<total_option<<endl;
    reverse(nai.begin(),nai.end());
//    for(ll i=0;i<nai.size();i++)
//    {
//        cout<<nai[i]<<" ";
//    }
//    cout<<endl;
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
               continue;
            }
            if(ase2[arr[i]]>1)
            {
                if(nai.size()==0)
                {
                    continue;
                }
                ll now_choto=nai[nai.size()-1];
//                cout<<now_choto<<endl;
                if(arr[i]>now_choto)
                {
//                    cout<<"dhukse"<<endl;
                    total_option--;
                    arr[i]=now_choto;
//                    cout<<i<<" "<<arr[i]<<endl;
                    nai.pop_back();

                }
                else if(arr[i]<now_choto)
                {
                    if(total_option>nai.size())
                    {
//                        cout<<"dhukse"<<endl;
                        total_option--;
                        continue;
                    }
                    else if(total_option==nai.size())
                    {
                        if(ase[arr[i]]==i)
                        {
                            total_option--;
                            arr[i]=now_choto;
                            nai.pop_back();
                        }
                    }
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase2[200005];
ll arr[3000000];
ll skip[200005];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
    reverse(nai.begin(),nai.end());
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        cout<<nai.size()<<" ";
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
                continue;
            }
            ll now_choto=nai[nai.size()-1];
            if(arr[i]>now_choto)
            {
                arr[i]=now_choto;

                nai.pop_back();
            }
            else if(arr[i]<now_choto)
            {
                if(skip[arr[i]]==0)
                {
                    skip[arr[i]]=1;
                    continue;
                }
                else
                {
                    arr[i]=now_choto;

                     nai.pop_back();
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
ll ase2[200005];
ll arr[3000000];
ll skip[200005];
vector<ll>nai;
ll total_option;
int main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&arr[i]);
        ase2[arr[i]]++;
    }
    for(ll i=1;i<=n;i++)
    {
        if(ase2[i]==0)
        {
            nai.push_back(i);
            continue;
        }
        total_option+=(ase2[i]-1);
    }
    reverse(nai.begin(),nai.end());
    if(nai.size()==0)
    {
        cout<<"0"<<endl;
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
        return 0;
    }
    else
    {
        for(ll i=1;i<=n;i++)
        {
            if(ase2[arr[i]]==1)
            {
                continue;
            }
            ll now_choto=nai[nai.size()-1];
            if(arr[i]>now_choto)
            {
                arr[i]=now_choto;

                nai.pop_back();
            }
            else if(arr[i]<now_choto)
            {
                if(skip[arr[i]]==0)
                {
                    skip[arr[i]]=1;
                    continue;
                }
                else
                {
                    arr[i]=now_choto;

                     nai.pop_back();
                }
            }
        }
        for(ll i=1;i<=n;i++)
        {
            cout<<arr[i]<<" ";
        }
    }
    return 0;
}


/* DHUOJ solution #324963 @ 2018-05-19 22:27:15.905 */

#include <iostream>
#include <string.h>
using namespace std;

int array[200001];
int a[200001];
int vis[200001];

int main()
{
	int num,k,ff;memset(array,0,sizeof(array));
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>num;a[i]=num;
		if(!array[num]){
			array[num]=1;
		}
		else {
			array[num]++;
		}
	}
	int j=1;
	for(int i=0;i<k;i++){
		if(array[a[i]]>1){
			while(array[j]!=0)j++;
			if(a[i]<=j&&vis[a[i]]==0){
				vis[a[i]]++;continue;
			}
			ff++;
			array[a[i]]--;
			array[j]++;
			a[i]=j;
		}
	}
	cout<<ff<<endl;
	for(int i=0;i<k-1;i++)cout<<a[i]<<" ";cout<<a[k-1]<<endl;
	return 0;
 }


/* DHUOJ solution #324853 @ 2018-05-18 16:54:17.082 */

#include <iostream>
#include <string.h>
using namespace std;

int array[200001];
int a[200001];
int vis[200001];

int main()
{
	int num,k,ff;memset(array,0,sizeof(array));
	cin>>k;
	for(int i=0;i<k;i++){
		cin>>num;a[i]=num;
		if(!array[num]){
			array[num]=1;
		}
		else {
			array[num]++;
		}
	}
	int j=1;
	for(int i=0;i<k;i++){
		if(array[a[i]]>1){
			while(array[j]!=0)j++;
			if(a[i]<=j&&!vis[a[i]]==0){
				vis[a[i]]++;continue;
			}
			ff++;
			array[a[i]]--;
			array[j]++;
			a[i]=j;
		}
	}
	cout<<ff<<endl;
	for(int i=0;i<k;i++)cout<<a[i]<<" ";cout<<endl;
	return 0;
 }


/*
思路：
int exists[maxn]; 记录初始每种号码的数量，>1表示有自身重复数字
vector<int> toAdd 缺少的数字
map<int, bool> hasDel 在树中表示已经删除
算法：滑动仓,贪心
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;

bool comp(const int &a,const int &b)
{
    return a>b;
}
bool del[maxn];
int num[maxn];
int exists[maxn];
int main(){
	memset(exists, 0, sizeof(exists));
	memset(del, false, sizeof del);
	map<int, bool> 	hasDel;
	hasDel.clear();
	int n;
	cin >>n;
	for(int i=0; i<n; i++){
		cin >>num[i];
		exists[num[i]]++;
	}

	vector<int> toAdd;
	toAdd.clear();
	for(int i=1; i<=n; i++){
		if(!exists[i])
			toAdd.push_back(i);
	}
	sort(toAdd.begin(), toAdd.end(), comp);
	for(int i=0; i<n; i++){
		if(exists[num[i]]>1){
			del[num[i]] = true;
		}
	}
	int ans = toAdd.size();
	//slide
	for(int i=0; i<n; i++){
	   // cout <<i<<" "<<del[num[i]] <<" "<< hasDel.count(num[i])<<" "<<toAdd.back()<<" "<<num[i]<<'\n';
		if(del[num[i]] && !toAdd.empty()&&exists[num[i]]>1&&(hasDel.count(num[i])>0||toAdd.back()<num[i])){
		//	hasDel[num[i]] = true;
		    exists[num[i]]--;
			num[i] = toAdd.back();
			toAdd.pop_back();
			continue;

		}
		if(del[num[i]]) {
		    hasDel[num[i]] = true;;
		    //cout <<"dfd "<<num[i] <<'\n';
		}

	}

	cout <<ans <<'\n';
	for(int i=0; i<n; i++){
		cout <<num[i] <<' ';
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>

using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int maxn = 200000 + 10;
int num[maxn];
int tim[maxn];
int flag[maxn];
int n;
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &num[i]);
        tim[num[i]]++;
    }
    int ans = 0;
    int p;
    for (p = 1; tim[p] != 0; p++);
    for (int i = 0; i < n; i++)
    {
        if (tim[num[i]] > 1)
        {
            if (flag[num[i]] || num[i] > p)
            {
                ans++;
                tim[num[i]]--;
                num[i] = p;
                tim[p]++;
                for (; tim[p] != 0; p++);
            }
            else
            {
                flag[num[i]]++;
            }
        }
    }
    printf("%d\n", ans);
    for (int i = 0; i < n; i++)
    {
        if (i)
        {
            printf(" ");
        }
        printf("%d", num[i]);
    }
    printf("\n");
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>

#include <cstring>
#include <string>
#include <sstream>

#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>

#include <queue>
#include <utility>
#include <set>
#include <stack>
#include <vector>
#include <map>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)

#define mp make_pair

#define pb push_back
#define ff first
#define ss second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;

	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}

inline void ini(int& x){
	x = getnum<int>();
}

inline void scani(int& x){
	scanf("%d",&x);
}

//end of macro

const int N = 2e5 + 5;

int n;
int arr[N];
queue<int>pos[N];

bool locked[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    REP(i,1,n){
    	cin>>arr[i];
    	pos[arr[i]].push(i);
	}

    int change = 0;
    int j = 1;
    REP(i,1,n){
    	if(pos[i].size() == 0){
    		while(locked[j] || pos[arr[j]].size() == 1){
    			j++;
			}
			//printf("change pos %d to %d\n",j,i);
    		change++;
    		locked[j] = true;
    		pos[arr[j]].pop();
    		arr[j++] = i;
		}else{
			locked[pos[i].front()] = true;
			while(!pos[i].empty())pos[i].pop();
		}
	}

	cout << change << endl;
	REP(i,1,n)cout << arr[i] << " ";

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>

#include <cstring>
#include <string>
#include <sstream>

#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>

#include <queue>
#include <utility>
#include <set>
#include <stack>
#include <vector>
#include <map>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)

#define mp make_pair

#define pb push_back
#define ff first
#define ss second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;

	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}

inline void ini(int& x){
	x = getnum<int>();
}

inline void scani(int& x){
	scanf("%d",&x);
}

//end of macro

const int N = 2e5 + 5;

int n;
int arr[N];
queue<int>pos[N];

bool locked[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    REP(i,1,n){
    	cin>>arr[i];
    	pos[arr[i]].push(i);
	}

    int change = 0;
    int j = 1;
    REP(i,1,n){
    	if(pos[i].size() == 0){
    		while(locked[j] || pos[arr[j]].size() == 1){
    			j++;
			}
			//printf("change pos %d to %d\n",j,i);
    		change++;
    		locked[j] = true;
    		if(!pos[arr[j]].empty())pos[arr[j]].pop();
    		arr[j++] = i;
		}else{
			locked[pos[i].front()] = true;
			while(!pos[i].empty())pos[i].pop();
		}
	}

	cout << change << endl;
	REP(i,1,n)cout << arr[i] << " ";

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>

#include <cstring>
#include <string>
#include <sstream>

#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>

#include <queue>
#include <utility>
#include <set>
#include <stack>
#include <vector>
#include <map>

#define YOU using
#define DONT namespace
#define SAY std

YOU DONT SAY;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)

#define mp make_pair

#define pb push_back
#define ff first
#define ss second

const int OO = 1e9;
const ll INF = 1e18;

const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}

#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;

	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}

inline void ini(int& x){
	x = getnum<int>();
}

inline void scani(int& x){
	scanf("%d",&x);
}

//end of macro

const int N = 2e5 + 5;

int n;
int arr[N];
queue<int>pos[N];

bool locked[N];
bool expendable[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    REP(i,1,n){
    	cin>>arr[i];
    	pos[arr[i]].push(i);
	}

	REP(i,1,n){
		if(pos[arr[i]].size() > 1)expendable[i] = true;
	}

    int change = 0;
    int j = 1;
    REP(i,1,n){
    	if(pos[i].size() == 0){
    		while(locked[j] || !expendable[j])j++;
    		change++;
    		locked[j] = true;
    		arr[j++] = i;
		}else{
			while(pos[i].front() < j)pos[i].pop();
			locked[pos[i].front()] = true;
		}
	}

	cout << change << endl;
	REP(i,1,n)cout << arr[i] << " ";

    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=2e5+6;
int a[MAXN];
int cnt[MAXN];
bool vis[MAXN];
int main(){
    int n;
    int sum=0;
    memset(vis,false,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        int x=a[i];
        cnt[x]++;
    }
    int   ans=0;
    int j=1;
    for(int i=1;i<=n;i++){
        while((vis[j]||cnt[j])&&j<=n)   j++;
        printf("%d %d\n",i,j);
        if(cnt[a[i]]>=1)
        {
            if(vis[a[i]])
            {
                a[i]=j;
                ans++;
                vis[j]=1;
                cnt[a[i]]--;
            }
            else{
                if(cnt[a[i]]==1)  continue;
                else{
                    if(a[i]>j){
                        a[i]=j;
                        ans++;
                        vis[j]=1;
                        cnt[a[i]]--;
                    }
                    else{
                        vis[a[i]]=1;
                        cnt[a[i]]--;
                    }
                }
            }
        }
     //   if(sum+ans==n)
       //     break;
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        printf("%d",a[i]);
        if(i!=n)  printf(" ");
        else   printf("\n");
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=2e5+6;
int a[MAXN];
int cnt[MAXN];
bool vis[MAXN];
int main(){
    int n;
    int sum=0;
    memset(vis,false,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        int x=a[i];
        cnt[x]++;
    }
    int   ans=0;
    int j=1;
    for(int i=1;i<=n;i++){
        while((vis[j]||cnt[j])&&j<=n)   j++;
     //   printf("%d %d   %d\n",i,j,cnt[a[i]]);
        if(cnt[a[i]]>=1)
        {
            if(vis[a[i]])
            {
                cnt[a[i]]--;
                a[i]=j;
                ans++;
                vis[j]=1;
            }
            else{
                if(cnt[a[i]]==1)  continue;
                else{
                    if(a[i]>j){
                        cnt[a[i]]--;
                        a[i]=j;
                        ans++;
                        vis[j]=1;
                    }
                    else{
                        vis[a[i]]=1;
                        cnt[a[i]]--;
                    }
                }
            }
        }
     //   if(sum+ans==n)
       //     break;
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        printf("%d",a[i]);
        if(i!=n)  printf(" ");
        else   printf("\n");
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=2e5+6;
int a[MAXN];
int cnt[MAXN];
bool vis[MAXN];
int main(){
    int n;
    int sum=0;
    memset(vis,false,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        int x=a[i];
        cnt[x]++;
    }
    int   ans=0;
    int j=1;
    for(int i=1;i<=n;i++){
        while((vis[j]||cnt[j])&&j<=n)   j++;
       // printf("%d %d\n",i,j);
        if(cnt[a[i]]>=1)
        {
            if(vis[a[i]])
            {
                a[i]=j;
                ans++;
                vis[j]=1;
                cnt[a[i]]--;
            }
            else{
                if(cnt[a[i]]==1)  continue;
                else{
                    if(a[i]>j){
                        a[i]=j;
                        ans++;
                        vis[j]=1;
                        cnt[a[i]]--;
                    }
                    else{
                        vis[a[i]]=1;
                        cnt[a[i]]--;
                    }
                }
            }
        }
     //   if(sum+ans==n)
       //     break;
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        printf("%d",a[i]);
        if(i!=n)  printf(" ");
        else   printf("\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>

using namespace std;

typedef vector<long> vi;

int main()
{
    long n, tmp;
    cin >> n;
    vi a(n+1, 0);
    stack <long> ds;
    vi dss(n+1, 0);
    for (int i=1; i<=n; ++i)
    {
        cin >> dss[i];
        ++a[dss[i]];
    }

    for (int i=n; i>=1; --i)
        if (a[i]==0)
            ds.push(i);
    cout << ds.size() << '\n';
    for (int i=1; i<=n; ++i)
        if (ds.empty() || a[dss[i]]==1 || (dss[i]<ds.top() && a[dss[i]]>0))
        {
            cout << dss[i] << ' ';
            a[dss[i]]=0;
        }
        else
        {
            cout << ds.top() << ' ';
            --a[dss[i]];
            ds.pop();
        }
}


#include <iostream>
#include <vector>
#include <stack>

using namespace std;

typedef vector<long> vi;

int main()
{
    long n, tmp;
    cin >> n;
    vi a(n+1, 0);
    stack <long> ds;
    vi dss(n+1, 0);
    for (int i=1; i<=n; ++i)
    {
        cin >> dss[i];
        ++a[dss[i]];
    }

    for (int i=n; i>=1; --i)
        if (a[i]==0)
            ds.push(i);
    cout << ds.size() << '\n';
    for (int i=1; i<=n; ++i)
        if (ds.empty() || a[dss[i]]==1 || dss[i]<ds.top())
        {
            cout << dss[i] << ' ';
            a[dss[i]]=0;
        }
        else
        {
            cout << ds.top() << ' ';
            --a[dss[i]];
            ds.pop();
        }
}


#include <iostream>
#include <vector>
#include <stack>

using namespace std;

typedef vector<long> vi;

int main()
{
    long n, tmp;
    cin >> n;
    vi a(n+1, 0);
    stack <long> ds;
    vi dss(n+1, 0);
    for (int i=1; i<=n; ++i)
    {
        cin >> dss[i];
        a[dss[i]]=1;
    }

    for (int i=n; i>=1; --i)
        if (a[i]==0)
            ds.push(i);
    cout << ds.size() << '\n';
    for (int i=1; i<=n; ++i)
        if (ds.empty() || a[dss[i]]==1 && dss[i]<ds.top())
        {
            cout << dss[i] << ' ';
            a[dss[i]]=0;
        }
        else
        {
            cout << ds.top() << ' ';
            ds.pop();
        }
}


#include <iostream>
#include <vector>
#include <stack>

using namespace std;

typedef vector<long> vi;

int main()
{
    long n, tmp;
    cin >> n;
    vi a(n+1, 0);
    stack <long> ds;
    vi dss(n+1, 0);
    for (int i=1; i<=n; ++i)
    {
        cin >> dss[i];
        ++a[dss[i]];
    }

    for (int i=n; i>=1; --i)
        if (a[i]==0)
            ds.push(i);
    cout << ds.size() << '\n';
    for (int i=1; i<=n; ++i)
        if (ds.empty() || a[dss[i]]==1 || (dss[i]<ds.top() && a[dss[i]]!=0))
        {
            cout << dss[i] << ' ';
            a[dss[i]]=0;
        }
        else
        {
            cout << ds.top() << ' ';
            --a[dss[i]];
            ds.pop();
        }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MAXN = 5e4+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;

template <class T>
inline bool read(T &ret) {
   char c; int sgn;
   if(c=getchar(),c==EOF) return 0; //EOF
   while(c!='-'&&(c<'0'||c>'9')) c=getchar();
   sgn=(c=='-')?-1:1;
   ret=(c=='-')?0:(c-'0');
   while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
   ret*=sgn;
   return 1;
}

int vis[200005] = {0}, num[200005];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n, x;
    read(n);
    int cnt = 0;
    for(int i = 1; i <= n; ++i) {
        read(x);
        num[i] = x;
        if(vis[x] == 0) {
            //cout << x << endl;
            cnt++;
            vis[x] = i;
        }
    }
    //cout << cnt << endl;
    int tt = 1;
    printf("%d\n", n - cnt);
    for(int i = 1; i <= n; ++i) {
        if(vis[num[i]] == i) printf("%d ", num[i]);
        else {
            while(vis[tt]) tt++;
            printf("%d ", tt);
            tt++;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MAXN = 5e4+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;

template <class T>
inline bool read(T &ret) {
   char c; int sgn;
   if(c=getchar(),c==EOF) return 0; //EOF
   while(c!='-'&&(c<'0'||c>'9')) c=getchar();
   sgn=(c=='-')?-1:1;
   ret=(c=='-')?0:(c-'0');
   while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
   ret*=sgn;
   return 1;
}

int vis[200005] = {0}, num[200005];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n, x;
    read(n);
    int cnt = 0;
    for(int i = 0; i < n; ++i) {
        read(x);
        num[i] = x;
        if(vis[x] == 0) {
            cnt++;
            vis[x] = i;
        }
    }
    int tt = 1;
    printf("%d\n", n - cnt);
    for(int i = 0; i < n; ++i) {
        if(vis[num[i]] == i) printf("%d ", num[i]);
        else {
            while(vis[tt]) tt++;
            printf("%d ", tt);
            tt++;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int MAXN = 5e4+5;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;

template <class T>
inline bool read(T &ret) {
   char c; int sgn;
   if(c=getchar(),c==EOF) return 0; //EOF
   while(c!='-'&&(c<'0'||c>'9')) c=getchar();
   sgn=(c=='-')?-1:1;
   ret=(c=='-')?0:(c-'0');
   while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
   ret*=sgn;
   return 1;
}

int vis[200005] = {0}, num[200005];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    int n, x;
    read(n);
    int cnt = 0;
    for(int i = 1; i <= n; ++i) {
        read(x);
        num[i] = x;
        if(vis[x] == 0) cnt++;
        vis[x]++;
    }
    //cout << cnt << endl;
    int tt = 1;
    printf("%d\n", n - cnt);
    for(int i = 1; i <= n; ++i) {
        if(vis[num[i]] > 1) {
            while(vis[tt]) tt++;
            if(tt < num[i]) {
                vis[num[i]]--;
                printf("%d ", tt);
                tt++;
            }
            else {
                vis[num[i]] = 0;
                printf("%d ", num[i]);
            }
        }
        else if(vis[num[i]] == 1) {
            printf("%d ", num[i]);
        }
        else {
            while(vis[tt]) tt++;
            printf("%d ", tt);
            tt++;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	/*
	ifstream cin("input.txt");
    ofstream cout("output.txt");
    */

    int n;
	cin>>n;
	int count[n+1]={0};
	int curr[n+1]={0};
	int ans[n+1]={0};
	int print[n+1]={0};
	for(int i=1;i<=n;i++)
	{
		cin>>curr[i];
		count[curr[i]]++;
	}
	for(int i=1;i<=n;i++){
		if(count[curr[i]] == 1){
			ans[i] = curr[i];
			print[curr[i]] = 1;
		}
	}
	int min = 1;
	for(int i=1;i<=n;i++){
		if(ans[i]==0){
			if(count[curr[i]]==1){
				if(print[curr[i]]!=1){
					ans[i] = curr[i];
					count[curr[i]] = 0;
					print[curr[i]] = 1;
					continue;
				}
			}
			while(( print[min]!=0 || min!=curr[i]) && (print[min]!=0 || count[min]!=0))
				min++;
			ans[i] = min;
			count[curr[i]]--;
			print[min] = 1;
		}
	}
	int myans = 0;
	for(int i=1;i<=n;i++)
	if(curr[i]!=ans[i])
	myans++;
	cout<<myans<<endl;
	for(int i=1;i<=n;i++)
	cout<<ans[i]<<" ";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	/*
	ifstream cin("input.txt");
    ofstream cout("output.txt");
    */

    int n;
	cin>>n;
	int count[n+1]={0};
	int curr[n+1]={0};
	int ans[n+1]={0};
	int print[n+1]={0};
	for(int i=1;i<=n;i++)
	{
		cin>>curr[i];
		count[curr[i]]++;
	}
	for(int i=1;i<=n;i++){
		if(count[curr[i]] == 1){
			ans[i] = curr[i];
			print[curr[i]] = 1;
		}
	}
	int min = 1;
	for(int i=1;i<=n;i++){
		if(ans[i]==0){
			if(count[curr[i]]==1){
				if(print[curr[i]]!=1){
					ans[i] = curr[i];
					print[curr[i]] = 1;
					continue;
				}
			}
			/*
			while(( print[min]!=0 || min!=curr[i]) && (print[min]!=0 || count[min]!=0))
			*/
			while(true){
				if(min>n)
					break;
				if(print[min] == 0 && count[min] == 0)
					break;
				min++;
			}
			if(print[curr[i]]==0){
				if(min>curr[i]){
					ans[i] = curr[i];
					print[curr[i]]=1;
					continue;
				}
			}
			ans[i] = min;
			print[min] = 1;
		}
	}
	int myans = 0;
	for(int i=1;i<=n;i++)
	if(curr[i]!=ans[i])
	myans++;
	cout<<myans<<endl;
	for(int i=1;i<=n;i++)
	cout<<ans[i]<<" ";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	/*
	ifstream cin("input.txt");
    ofstream cout("output.txt");
    */

    int n;
	cin>>n;
	int count[n+1]={0};
	int curr[n+1]={0};
	int ans[n+1]={0};
	int print[n+1]={0};
	for(int i=1;i<=n;i++)
	{
		cin>>curr[i];
		count[curr[i]]++;
	}
	for(int i=1;i<=n;i++){
		if(count[curr[i]] == 1){
			ans[i] = curr[i];
			print[curr[i]] = 1;
		}
	}
	int min = 1;
	for(int i=1;i<=n;i++){
		if(ans[i]==0){
			if(count[curr[i]]==1){
				if(print[curr[i]]!=1){
					ans[i] = curr[i];
					print[curr[i]] = 1;
					continue;
				}
			}
			/*
			while(( print[min]!=0 || min!=curr[i]) && (print[min]!=0 || count[min]!=0))
			*/
			while(true){
				if(min>n)
					break;
				if(print[min] == 0 && count[min] == 0)
					break;
				min++;
			}
			if(print[curr[i]]==0){
				if(min>curr[i]){
					ans[i] = curr[i];
					print[curr[i]]=1;
					count[curr[i]] = -1;
					continue;
				}
			}
			ans[i] = min;
			print[min] = 1;
			count[curr[i]]--;
		}
	}
	int myans = 0;
	for(int i=1;i<=n;i++)
	if(curr[i]!=ans[i])
	myans++;
	cout<<myans<<endl;
	for(int i=1;i<=n;i++)
	cout<<ans[i]<<" ";
}


#include<bits/stdc++.h>
using namespace std;

struct PT {
    int x,y;
    PT(int X=0,int Y=0):x(X),y(Y){}
    PT operator+(const PT &A)const {
        return PT(x+A.x,y+A.y);
    }
    PT operator-(const PT &A)const {
        return PT(x-A.x,y-A.y);
    }
    PT operator*(int a)const{
        return PT(x*a,y*a);
    }
    PT operator/(const int &A)const {
        return PT(x/A,y/A);
    }
    int operator^(const PT &A)const {
        return x*A.y-y*A.x;
    }
		int len(){return x*x+y*y;}
    PT norm()const {
        return PT(-y,x);
    }
}t[1010],s[1010];
bool cmp(const PT &a,const PT &b)
{
        return (a.x<b.x)||((a.x==b.x)&&(a.y<b.y));
}
bool cmp1(PT a,PT b)
{
        return (a.len())>(b.len());
}
int cross(PT x,PT a,PT b){
    return (a-x)^(b-x);
}
int len(PT a,PT b){return (a-b).len();}
int top,ma;
void convex_hull(int n){
    sort(t,t+n,cmp);
    top=0;
    for(int i=0;i<n;i++){
        while(top>=2&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
    for(int i=n-2,k=top+1;i>=0;i--){
        while(top>=k&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
		top--;
}
int ans[10];
int tmp[10];
void dfs(int now,int n,int total,int size,int where)
{
		if(now==n)
		{
				if(ma<total)
				{
						ma=total;
						for(int i=0;i<n;i++) ans[i]=tmp[i];
				}
				return;
		}
		for(int i=where;i<size;i++)
		{
				tmp[now]=i;
				int _total=total;
				for(int j=0;j<now;j++) _total+=len(s[i],s[tmp[j]]);
				dfs(now+1,n,_total,size,i);
		}
}
void special(int n,int k)
{
		int a=n/2;
		int b=a*a*4*k*k;
		printf("%d\n",b);
		while(a--) printf("%d 0\n",k);
		a=n/2;
		while(a--) printf("%d 0\n",-k);
		return;
}
int main()
{
		int n,k;
		scanf("%d%d",&n,&k);
		if(n%2==0)
		{
				special(n,k);
				return 0;
		}
		int now=0;
		t[now++]=PT(k,0);
		t[now++]=PT(-k,0);
		for(int i=-k+1;i<=k-1;i++)
			for(int j=1;j<=k;j++)
			{
					if((i*i+j*j)<=k*k && (i*i+(j+1)*(j+1))>k*k)
				{
							t[now++]=PT(i,j);
							t[now++]=PT(i,-j);
							break;
					}
			}
		top=0;
		convex_hull(now);
		ma=0;
		dfs(0,n,0,top,0);
		printf("%d\n",ma);
		for(int i=0;i<n;i++) printf("%d %d\n",s[ans[i]].x,s[ans[i]].y);
}


#include<bits/stdc++.h>
using namespace std;

struct PT {
    int x,y;
    PT(int X=0,int Y=0):x(X),y(Y){}
    PT operator+(const PT &A)const {
        return PT(x+A.x,y+A.y);
    }
    PT operator-(const PT &A)const {
        return PT(x-A.x,y-A.y);
    }
    PT operator*(int a)const{
        return PT(x*a,y*a);
    }
    PT operator/(const int &A)const {
        return PT(x/A,y/A);
    }
    int operator^(const PT &A)const {
        return x*A.y-y*A.x;
    }
		int len(){return x*x+y*y;}
    PT norm()const {
        return PT(-y,x);
    }
}t[1010],s[1010];
bool cmp(const PT &a,const PT &b)
{
        return (a.x<b.x)||((a.x==b.x)&&(a.y<b.y));
}
bool cmp1(PT a,PT b)
{
        return (a.len())>(b.len());
}
int cross(PT x,PT a,PT b){
    return (a-x)^(b-x);
}
int len(PT a,PT b){return (a-b).len();}
int top,ma;
void convex_hull(int n){
    sort(t,t+n,cmp);
    top=0;
    for(int i=0;i<n;i++){
        while(top>=2&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
    for(int i=n-2,k=top+1;i>=0;i--){
        while(top>=k&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
		top--;
}
int ans[10];
int tmp[10];
void dfs(int now,int n,int total,int size,int where)
{
		if(now==n)
		{
				if(ma<total)
				{
						ma=total;
						for(int i=0;i<n;i++) ans[i]=tmp[i];
				}
				return;
		}
		for(int i=where;i<size;i++)
		{
				tmp[now]=i;
				int _total=total;
				for(int j=0;j<now;j++) _total+=len(s[i],s[tmp[j]]);
				dfs(now+1,n,_total,size,i);
		}
}
void special(int n,int k)
{
		int a=n/2;
		int b=a*a*4*k*k;
		printf("%d\n",b);
		while(a--) printf("%d 0\n",k);
		a=n/2;
		while(a--) printf("%d 0\n",-k);
		return;
}
int main()
{
		int n,k;
		scanf("%d%d",&n,&k);
		if(n%2==0)
		{
				special(n,k);
				return 0;
		}
		int now=0;
		t[now++]=PT(k,0);
		t[now++]=PT(-k,0);
		for(int i=-k+1;i<=k-1;i++)
			for(int j=1;j<=k;j++)
			{
					if((i*i+j*j)<=k*k && (i*i+(j+1)*(j+1))>k*k)
				{
							t[now++]=PT(i,j);
							t[now++]=PT(i,-j);
							break;
					}
			}
		top=0;
		convex_hull(now);
		ma=0;
		dfs(0,n,0,min(top,18),0);
		printf("%d\n",ma);
		for(int i=0;i<n;i++) printf("%d %d\n",s[ans[i]].x,s[ans[i]].y);
}


#include<bits/stdc++.h>
using namespace std;

struct PT {
    int x,y;
    PT(int X=0,int Y=0):x(X),y(Y){}
    PT operator+(const PT &A)const {
        return PT(x+A.x,y+A.y);
    }
    PT operator-(const PT &A)const {
        return PT(x-A.x,y-A.y);
    }
    PT operator*(int a)const{
        return PT(x*a,y*a);
    }
    PT operator/(const int &A)const {
        return PT(x/A,y/A);
    }
    int operator^(const PT &A)const {
        return x*A.y-y*A.x;
    }
		int len(){return x*x+y*y;}
    PT norm()const {
        return PT(-y,x);
    }
}t[1010],s[1010];
bool cmp(const PT &a,const PT &b)
{
        return (a.x<b.x)||((a.x==b.x)&&(a.y<b.y));
}
int cross(PT x,PT a,PT b){
    return (a-x)^(b-x);
}
int len(PT a,PT b){return (a-b).len();}
int top,ma;
void convex_hull(int n){
    sort(t,t+n,cmp);
    top=0;
    for(int i=0;i<n;i++){
        while(top>=2&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
    for(int i=n-2,k=top+1;i>=0;i--){
        while(top>=k&&cross(s[top-2],s[top-1],t[i])<=0)top--;
        s[top++]=t[i];
    }
		top--;
}
int calc(vector<PT>&vec)
{
		int size=vec.size();
		int ans=0;
		for(int i=0;i<size;i++)
			for(int j=i+1;j<size;j++)
				ans+=len(vec[i],vec[j]);
		return ans;
}
vector<PT>ans;
int cnt[50];
void dfs(int now,int rest,int size)
{
		if(rest==0)
		{
				vector<PT>tmp;
				for(int i=0;i<size;i++)
					for(int j=0;j<cnt[i];j++)
						tmp.push_back(s[i]);
				int c=calc(tmp);
				if(c>ma)
				{
						ma=c;
						ans=tmp;
				}
				return;
		}
		if(now==size) return;
		for(int r=0;r<=rest;r++)
		{
				cnt[now]=r;
				dfs(now+1,rest-r,size);
				cnt[now]=0;
		}
}
void special(int n,int k)
{
		int a=n/2;
		int b=a*a*4*k*k;
		printf("%d\n",b);
		while(a--) printf("%d 0\n",k);
		a=n/2;
		while(a--) printf("%d 0\n",-k);
		return;
}
int main()
{
		int n,k;
		scanf("%d%d",&n,&k);
		if(n%2==0)
		{
				special(n,k);
				return 0;
		}
		int now=0;
		t[now++]=PT(k,0);
		t[now++]=PT(-k,0);
		for(int i=-k+1;i<=k-1;i++)
			for(int j=1;j<=k;j++)
			{
					if((i*i+j*j)<=k*k && (i*i+(j+1)*(j+1))>k*k)
					{
							t[now++]=PT(i,j);
							t[now++]=PT(i,-j);
							break;
					}
			}
		top=0;
		convex_hull(now);
		ma=0;
		dfs(0,n,top);
		printf("%d\n",ma);
		for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].x,ans[i].y);
}


#include<stdio.h>
#include<math.h>
#include<algorithm>
struct Point{
	int x,y;
}p[3000],co[3000];
int n,m,ch[10],N,ans[10],max;
bool cmp(Point a,Point b){
	if(a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}
double cross(Point o,Point a,Point b){
	return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
void convex(){
	std::sort(p+1,p+n+1,cmp);
	int i,t;
	m=0;
	for(i=1;i<=n;i++){
		while(m>=2&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	for(i=n-1,t=m+1;i>=1;i--){
		while(m>=t&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	return;
}
void tru(){
	int norm=0,x=0,y=0,to;
	for(int k=1;k<=N;k++){
		norm+=(co[ch[k]].x*co[ch[k]].x+co[ch[k]].y*co[ch[k]].y);
		x+=co[ch[k]].x;
		y+=co[ch[k]].y;
	}
	to=N*norm-(x*x+y*y);
	if(to>max){
		max=to;
		for(int k=1;k<=N;k++){
			ans[k]=ch[k];
		}
	}
	return;
}
void dfs(int x,int y){
	if(x==N+1){
		tru();
		return;
	}
	x++;
	for(int k=y;k<m-1;k++){
		if(x==1&&k>(m-2)/4) break;
		ch[x]=k;
		dfs(x,k);
	}
	return;
}
int main(){
	int k,i,r;
	scanf("%d%d",&N,&r);
	/*if(N%2==0){
		printf("%d\n",N*N*r*r);
		for(k=1;k<=N;k++){
			if(k<=N/2) printf("%d %d\n",r,0);
			else printf("%d %d\n",-r,0);
		}
		return 0;
	}*/
	for(k=-r;k<=r;k++){
		for(i=-r;i<=r;i++){
			if(k*k+i*i<=r*r){
				n++;
				p[n].x=k;
				p[n].y=i;
			}
		}
	}
	convex();
	dfs(0,0);
	printf("%d\n",max);
	for(k=1;k<=N;k++) printf("%d %d\n",co[ans[k]].x,co[ans[k]].y);
}


#include<stdio.h>
#include<math.h>
#include<algorithm>
struct Point{
	int x,y;
}p[3000],co[3000];
int n,m,ch[10],N,ans[10],max;
bool cmp(Point a,Point b){
	if(a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}
double cross(Point o,Point a,Point b){
	return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
void convex(){
	std::sort(p+1,p+n+1,cmp);
	int i,t;
	m=0;
	for(i=1;i<=n;i++){
		while(m>=2&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	for(i=n-1,t=m+1;i>=1;i--){
		while(m>=t&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	return;
}
void tru(){
	int norm=0,x=0,y=0,to;
	for(int k=1;k<=N;k++){
		norm+=(co[ch[k]].x*co[ch[k]].x+co[ch[k]].y*co[ch[k]].y);
		x+=co[ch[k]].x;
		y+=co[ch[k]].y;
	}
	to=N*norm-(x*x+y*y);
	if(to>max){
		max=to;
		for(int k=1;k<=N;k++){
			ans[k]=ch[k];
		}
	}
	return;
}
void dfs(int x,int y){
	if(x==N+1){
		tru();
		return;
	}
	x++;
	for(int k=y;k<m-1;k++){
		ch[x]=k;
		dfs(x,k);
	}
	return;
}
int main(){
	int k,i,r;
	scanf("%d%d",&N,&r);
	for(k=-r;k<=r;k++){
		for(i=-r;i<=r;i++){
			if(k*k+i*i<=r*r){
				n++;
				p[n].x=k;
				p[n].y=i;
			}
		}
	}
	convex();
	dfs(0,0);
	printf("%d\n",max);
	for(k=1;k<=N;k++) printf("%d %d\n",co[ans[k]].x,co[ans[k]].y);
}


#include<stdio.h>
#include<math.h>
#include<algorithm>
struct Point{
	int x,y;
}p[3000],co[3000];
int n,m,ch[10],N,ans[10],max;
bool cmp(Point a,Point b){
	if(a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}
double cross(Point o,Point a,Point b){
	return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
void convex(){
	std::sort(p+1,p+n+1,cmp);
	int i,t;
	m=0;
	for(i=1;i<=n;i++){
		while(m>=2&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	for(i=n-1,t=m+1;i>=1;i--){
		while(m>=t&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	return;
}
void tru(){
	int norm=0,x=0,y=0,to;
	for(int k=1;k<=N;k++){
		norm+=(co[ch[k]].x*co[ch[k]].x+co[ch[k]].y*co[ch[k]].y);
		x+=co[ch[k]].x;
		y+=co[ch[k]].y;
	}
	to=N*norm-(x*x+y*y);
	if(to>max){
		max=to;
		for(int k=1;k<=N;k++){
			ans[k]=ch[k];
		}
	}
	return;
}
void dfs(int x,int y){
	if(x==N+1){
		tru();
		return;
	}
	x++;
	for(int k=y;k<m-1;k++){
		ch[x]=k;
		dfs(x,k);
	}
	return;
}
int main(){
	int k,i,r;
	scanf("%d%d",&N,&r);
	if(N%2==0){
		printf("%d\n",N*N*r*r);
		for(k=1;k<=N;k++){
			if(k<=N/2) printf("%d %d\n",r,0);
			else printf("%d %d\n",-r,0);
		}
		return 0;
	}
	for(k=-r;k<=r;k++){
		for(i=-r;i<=r;i++){
			if(k*k+i*i<=r*r){
				n++;
				p[n].x=k;
				p[n].y=i;
			}
		}
	}
	convex();
	dfs(0,0);
	printf("%d\n",max);
	for(k=1;k<=N;k++) printf("%d %d\n",co[ans[k]].x,co[ans[k]].y);
}


#include<stdio.h>
#include<math.h>
#include<algorithm>
struct Point{
	int x,y;
}p[3000],co[3000];
int n,m,ch[10],N,ans[10],max;
bool cmp(Point a,Point b){
	if(a.x==b.x) return a.y<b.y;
	else return a.x<b.x;
}
double cross(Point o,Point a,Point b){
	return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
void convex(){
	std::sort(p+1,p+n+1,cmp);
	int i,t;
	m=0;
	for(i=1;i<=n;i++){
		while(m>=2&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	for(i=n-1,t=m+1;i>=1;i--){
		while(m>=t&&cross(co[m-2],co[m-1],p[i])<=0) m--;
		co[m]=p[i];
		m++;
	}
	return;
}
void tru(){
	int norm=0,x=0,y=0,to;
	for(int k=1;k<=N;k++){
		norm+=(co[ch[k]].x*co[ch[k]].x+co[ch[k]].y*co[ch[k]].y);
		x+=co[ch[k]].x;
		y+=co[ch[k]].y;
	}
	to=N*norm-(x*x+y*y);
	if(to>max){
		max=to;
		for(int k=1;k<=N;k++){
			ans[k]=ch[k];
		}
	}
	return;
}
void dfs(int x,int y){
	if(x==N+1){
		tru();
		return;
	}
	x++;
	for(int k=y;k<m-1;k++){
		ch[x]=k;
		dfs(x,k);
	}
	return;
}
int main(){
	int k,i,r;
	scanf("%d%d",&N,&r);
	/*if(N%2==0){
		printf("%d\n",N*N*r*r);
		for(k=1;k<=N;k++){
			if(k<=N/2) printf("%d %d\n",r,0);
			else printf("%d %d\n",-r,0);
		}
		return 0;
	}*/
	for(k=-r;k<=r;k++){
		for(i=-r;i<=r;i++){
			if(k*k+i*i<=r*r){
				n++;
				p[n].x=k;
				p[n].y=i;
			}
		}
	}
	convex();
	ch[1]=0;
	dfs(1,0);
	printf("%d\n",max);
	for(k=1;k<=N;k++) printf("%d %d\n",co[ans[k]].x,co[ans[k]].y);
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

typedef struct P { int x,y; P() {} P(int x,int y):x(x),y(y) {} } P;
bool operator<(const P &a,const P &b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; }
const P operator-(const P &a,const P &b) { return P(a.x-b.x,a.y-b.y); }
const int operator^(const P &a,const P &b) { return a.x*b.y-a.y*b.x; }
int side(const P &a,const P &b,const P &c) { int ret=(b-a)^(c-a); return ret<0?-1:ret>0?+1:0; }

int n,r;
P p[61*61]; int np;

P h[61*61]; int nh;

void calchull() {
	sort(p,p+np);
	REP(i,np) {
		while(nh>=2&&side(h[nh-2],h[nh-1],p[i])<=0) --nh;
		h[nh++]=p[i];
	}
	for(int i=np-2;i>=0;--i) {
		while(nh>=2&&side(h[nh-2],h[nh-1],p[i])<=0) --nh;
		h[nh++]=p[i];
	}
	--nh;
	//printf("%d\n",nh); REP(i,nh) printf("(%d,%d)",h[i].x,h[i].y); puts("");
}

P cur[8]; int ncur;
P ret[8]; int nret; int retval;

void go(int at,int sx,int sxx,int sy,int syy,int curval) {
	if(ncur>=n) {
		if(curval>retval) { nret=ncur; retval=curval; REP(i,ncur) ret[i]=cur[i]; }
		return;
	}
	FOR(i,at,nh) {
		cur[ncur++]=h[i];
		int x=h[i].x,y=h[i].y;
		go(i,sx+x,sxx+x*x,sy+y,syy+y*y,curval+(x*x+y*y)*(ncur-1)-2*x*sx-2*y*sy+sxx+syy);
		--ncur;
	}
}

void run() {
	scanf("%d%d",&n,&r);
	np=0; FORE(x,-r,+r) FORE(y,-r,+r) if(x*x+y*y<=r*r) p[np].x=x,p[np].y=y,++np;
	//printf("%d\n",np);
	calchull();
	go(0,0,0,0,0,0);
	printf("%d\n",retval); REP(i,nret) printf("%d %d\n",ret[i].x,ret[i].y);

}

int main() {
	run();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

struct point {
    int x, y;
    point (int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
};

int N, R, M, ans, pos[10], rec[10];
vector<point> vec;

inline int dis (int x, int y) {
    return x * x + y * y;
}

inline bool cmp (const point& a, const point& b) {
    return dis(a.x, a.y) > dis(b.x, b.y);
}

void init () {
    scanf("%d%d", &N, &R);
    for (int i = -R; i <= R; i++) {
        for (int j = -R; j <= R; j++)  {
            if (i * i + j * j <= R * R)
                vec.push_back(point(i, j));
        }
    }

    ans = 0;
    M = min((int)vec.size(), 18);
    sort(vec.begin(), vec.end(), cmp);
}

void dfs (int d, int f, int s) {
    if (d == N) {
        if (s > ans) {
            ans = s;
            memcpy(rec, pos, sizeof(pos));
        }
        return;
    }

    for (int i = f; i < M; i++) {
        int add = 0;
        for (int j = 0; j < d; j++)
            add += dis(vec[pos[j]].x - vec[i].x, vec[pos[j]].y - vec[i].y);
        pos[d] = i;
        dfs(d + 1, i, s + add);
    }
}

int main () {
    init();
    dfs(0, 0, 0);
    printf("%d\n", ans);
    for (int i = 0; i < N; i++)
        printf("%d %d\n", vec[rec[i]].x, vec[rec[i]].y);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3600;

#define endl '\n'

struct node{
	int x,y,d;
};

const node zero={0,0,0};
node A[N],ans[10],ntmp[10];
int cnt,maxn,n,M;

int dist(const node &a,const node &b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(const node &a,const node &b){
	return a.d>b.d;
}

void dfs(int rank,int next,int sum){
	if(rank==n){
		if(sum>maxn){
			maxn=sum;
			memcpy(ans,ntmp,sizeof(ntmp));
		}
		return;
	}
	int tmp;
	for(int i=next;i<=M;i++){
		if(i<M) ntmp[rank]=A[i];
		else ntmp[rank]=zero;
		tmp=0;
		for(int j=0;j<rank;j++){
			tmp+=dist(ntmp[j],ntmp[rank]);
		}
		dfs(rank+1,i,sum+tmp);
	}
}

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cout.setf(ios::fixed),cout.precision(3);
	int r,R2;
	cin>>n>>r;
	R2=r*r;
	cnt=0;
	for(int i=-r;i<=r;i++){
		for(int j=-r;j<=r;j++){
			if(i*i+j*j<=R2) {
				A[cnt].x=i;
				A[cnt].y=j;
				A[cnt].d=dist(A[cnt],zero);
				cnt++;
			}
		}
	}
	sort(A,A+cnt,cmp);
	M=min(cnt,20);

	maxn=0;
	dfs(0,0,0);
	cout<<maxn<<endl;
	for(int i=0;i<n;i++){
		cout<<ans[i].x<<' '<<ans[i].y<<endl;
	}
	return 0;
}
/*
暴力。
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3600;

#define endl '\n'

struct node{
	int x,y,d;
};

const node zero={0,0,0};
node A[N],ans[10],ntmp[10];
int cnt,maxn,n,M;

int dist(const node &a,const node &b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(const node &a,const node &b){
	return a.d>b.d;
}

void dfs(int rank,int next,int sum){
	if(rank==n){
		if(sum>maxn){
			maxn=sum;
			memcpy(ans,ntmp,sizeof(ntmp));
		}
		return;
	}
	int tmp;
	for(int i=next;i<=M;i++){
		if(n<M) ntmp[rank]=A[i];
		else ntmp[rank]=zero;
		tmp=0;
		for(int j=0;j<rank;j++){
			tmp+=dist(ntmp[j],ntmp[rank]);
		}
		dfs(rank+1,i,sum+tmp);
	}
}

int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cout.setf(ios::fixed),cout.precision(3);
	int r,R2;
	cin>>n>>r;
	R2=r*r;
	cnt=0;
	for(int i=-r;i<=r;i++){
		for(int j=-r;j<=r;j++){
			if(i*i+j*j<=R2) {
				A[cnt].x=i;
				A[cnt].y=j;
				A[cnt].d=dist(A[cnt],zero);
				cnt++;
			}
		}
	}
	sort(A,A+cnt,cmp);
	M=min(cnt,20);
	maxn=0;
	dfs(0,0,0);
	cout<<maxn<<endl;
	for(int i=0;i<n;i++){
		cout<<ans[i].x<<' '<<ans[i].y<<endl;
	}
	return 0;
}


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <functional>
#define MOD 1000000007
#define MAX ((1<<30)-1)
#define MAX2 ((1ll<<62)-1)
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef set<int>::iterator siit;

int n, r;
pii a[10000];
int x[10000][2];
int xi;

int ai;

int b[10000][2];
int bi;

int dab[10000][2];

int ma;

void calc()
{
	int i, j;
	int sum=0;
	for(i=0;i<bi;i++)
	{
		for(j=i+1;j<bi;j++)
		{
			sum+=(b[i][0]-b[j][0])*(b[i][0]-b[j][0])+(b[i][1]-b[j][1])*(b[i][1]-b[j][1]);
		}
	}
	if(ma < sum)
	{
		ma=sum;
		for(i=0;i<bi;i++) dab[i][0]=b[i][0], dab[i][1]=b[i][1];
	}
}

void back(int now, int mi)
{
	int i, j;
	if(now == 0)
	{
		calc();
		return;
	}
	for(i=mi;i<ai;i++)
	{
		b[bi][0]=a[i].first, b[bi][1]=a[i].second;
		bi++;
		back(now-1, i);
		bi--;
	}
}

int ccw(pii x, pii y, pii z)
{
	return (y.first-x.first)*(z.second-x.second)-(y.second-x.second)*(z.first-x.first);
}

bool cmp(pii x, pii y)
{
	return ccw(a[0], x, y) > 0;
}

pii stack[100];
int stacki;

int main()
{
	int i, j, k;
	scanf("%d%d", &n, &r);
	int now=0;
	for(i=-r;i<=r;i++)
	{
		for(j=-r;j<=r;j++)
		{
			if(i*i+j*j <= r*r)
			{
				a[ai++]=pii(i, j);
			}
		}
	}
	sort(a, a+ai);
	sort(a+1, a+ai, cmp);
	stack[0]=a[0];
	stacki=1;
	for(i=1;i<ai;i++)
	{
		while(stacki >= 2 && ccw(stack[stacki-2], stack[stacki-1], a[i]) <= 0)
		{
			stacki--;
		}
		stack[stacki++]=a[i];
	}
	for(i=0;i<stacki;i++) a[i]=stack[i];
	ai=stacki;
	back(n, 0);
	printf("%d\n", ma);
	for(i=0;i<n;i++) printf("%d %d\n", dab[i][0], dab[i][1]);
	return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <functional>
#define MOD 1000000007
#define MAX ((1<<30)-1)
#define MAX2 ((1ll<<62)-1)
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef set<int>::iterator siit;

int n, r;
pii a[10000];
int x[10000][2];
int xi;

int ai;

int b[10000][2];
int bi;

int dab[10000][2];

int ma;

int nowsum;

void calc()
{
	int i, j;
	if(ma < nowsum)
	{
		ma=nowsum;
		for(i=0;i<bi;i++) dab[i][0]=b[i][0], dab[i][1]=b[i][1];
	}
}

void back(int now, int mi)
{
	int i, j;
	if(now == 0)
	{
		calc();
		return;
	}
	for(i=mi;i<ai;i++)
	{
		int tmp=0;
		b[bi][0]=a[i].first, b[bi][1]=a[i].second;
		bi++;
		for(j=0;j<bi;j++) tmp+=(b[bi-1][0]-b[j][0])*(b[bi-1][0]-b[j][0])+(b[bi-1][1]-b[j][1])*(b[bi-1][1]-b[j][1]);
		nowsum+=tmp;
		back(now-1, i);
		nowsum-=tmp;
		bi--;
	}
}

int ccw(pii x, pii y, pii z)
{
	return (y.first-x.first)*(z.second-x.second)-(y.second-x.second)*(z.first-x.first);
}

bool cmp(pii x, pii y)
{
	return ccw(a[0], x, y) > 0;
}

pii stack[100];
int stacki;

int main()
{
	int i, j, k;
	scanf("%d%d", &n, &r);
	int now=0;
	for(i=-r;i<=r;i++)
	{
		for(j=-r;j<=r;j++)
		{
			if(i*i+j*j <= r*r)
			{
				a[ai++]=pii(i, j);
			}
		}
	}
	sort(a, a+ai);
	sort(a+1, a+ai, cmp);
	stack[0]=a[0];
	stacki=1;
	for(i=1;i<ai;i++)
	{
		while(stacki >= 2 && ccw(stack[stacki-2], stack[stacki-1], a[i]) <= 0)
		{
			stacki--;
		}
		stack[stacki++]=a[i];
	}
	for(i=0;i<stacki;i++) a[i]=stack[i];
	ai=stacki;
	back(n, 0);
	printf("%d\n", ma);
	for(i=0;i<n;i++) printf("%d %d\n", dab[i][0], dab[i][1]);
	return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <functional>
#define MOD 1000000007
#define MAX ((1<<30)-1)
#define MAX2 ((1ll<<62)-1)
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef set<int>::iterator siit;

int n, r;
int a[100][2];

int ai;

int b[100][2];
int bi;

int dab[100][2];

int ma;

void back(int now)
{
	int i, j;
	if(now == 0)
	{
		int sum=0;
		for(i=0;i<bi;i++)
		{
			for(j=i+1;j<bi;j++)
			{
				sum+=(b[i][0]-b[j][0])*(b[i][0]-b[j][0])+(b[i][1]-b[j][1])*(b[i][1]-b[j][1]);
			}
		}
		if(ma < sum)
		{
			ma=sum;
			for(i=0;i<bi;i++) dab[i][0]=b[i][0], dab[i][1]=b[i][1];
		}
		return;
	}
	for(i=0;i<ai;i++)
	{
		b[bi][0]=a[i][0], b[bi][1]=a[i][1];
		b[bi+1][0]=a[i][0], b[bi+1][1]=-a[i][1];
		bi+=2;
		back(now-1);
		bi-=2;
	}
}

int main()
{
	int i, j, k;
	scanf("%d%d", &n, &r);
	int now=0;
	for(i=r;i>=0;i--)
	{
		for(j=now;j<=r;j++)
		{
			if(i*i+j*j <= r*r)
			{
				if(j == now && i*i+(j+1)*(j+1) <= r*r) continue;
				a[ai][0]=i, a[ai++][1]=j;
			}
			else break;
		}
		now=j-1;
	}
	for(i=0;i<ai-1;i++)
	{
		a[i+ai][0]=-a[i][0];
		a[i+ai][1]=a[i][1];
	}
	ai+=ai-1;
	b[0][0]=-r, b[0][1]=0, bi=1;
	if(n%2 == 0) b[1][0]=r, b[1][1]=0, bi=2;
	back((n-1)/2);
	printf("%d\n", ma);
	for(i=0;i<n;i++) printf("%d %d\n", dab[i][0], dab[i][1]);
	return 0;
}
//*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int>vi;
typedef vector<ll>VI;
typedef pair<ll,ll>pii;
typedef pair<pii,int>Pii;
#define rep(i,a,b) for (int i=a;i<=b;++i)
#define dep(i,a,b) for (int i=a;i>=b;i--)
#define de(a) cout<<#a<<" = "<<a<<endl
#define dd(a) cout<<#a<<" = "<<a<<" "
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#pragma GCC optimize(2)
const int N=1e5+10;
const ll mod=1e18+7;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b&1)s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
void init() {ios::sync_with_stdio(false); cin.tie(0); }
const double PI=3.1415926535897932;

int n,r;
vector<pii> v;
int ans1[N],ans2[N];
ll ans=0;

bool cmp(pii x,pii y) {
	return x.fi*x.fi+x.se*x.se>y.fi*y.fi+y.se*y.se;
}

ll dis(int i,int j) {
	return  (v[i].fi-v[j].fi)*(v[i].fi-v[j].fi)+(v[i].se-v[j].se)*(v[i].se-v[j].se);
}

void dfs(int p,int k,ll now) {
	//dd(p); dd(k); de(now);
	if (k==n) {
		if (now>ans) {
			ans=now;
			rep(i,1,n) ans2[i]=ans1[i];
		}
		return ;
	}
	rep(i,p,min(17,sz(v)-1)) {
		rep(j,1,k) now+=dis(ans1[j],i);
		ans1[k+1]=i;
		dfs(i,k+1,now);
		rep(j,1,k) now-=dis(ans1[j],i);
	}
}

int main() {
	int t; scanf("%d",&t);
	while (t--) {
	    scanf("%d%d",&n,&r);
	    v.clear();
	    ans=0;
	    rep(i,0,1000) ans1[i]=0;
		rep(i,-r,r)
	    rep(j,-r,r) {
	    	if (i*i+j*j<=r*r) v.pb(mp(i,j));
	    }
	    sort(v.begin(),v.end(),cmp);
	    dfs(0,0,0);
	    printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef vector<int>vi;
typedef vector<ll>VI;
typedef pair<ll,ll>pii;
typedef pair<pii,int>Pii;
#define rep(i,a,b) for (int i=a;i<=b;++i)
#define dep(i,a,b) for (int i=a;i>=b;i--)
#define de(a) cout<<#a<<" = "<<a<<endl
#define dd(a) cout<<#a<<" = "<<a<<" "
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#pragma GCC optimize(2)
const int N=1e5+10;
const ll mod=1e18+7;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b&1)s=s*a%mod;a=a*a%mod;b>>=1;}return s;}
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
void init() {ios::sync_with_stdio(false); cin.tie(0); }
const double PI=3.1415926535897932;

int n,r;
vector<pii> v;
int ans1[N],ans2[N];
ll ans=0;

bool cmp(pii x,pii y) {
	return x.fi*x.fi+x.se*x.se>y.fi*y.fi+y.se*y.se;
}

ll dis(int i,int j) {
	return  (v[i].fi-v[j].fi)*(v[i].fi-v[j].fi)+(v[i].se-v[j].se)*(v[i].se-v[j].se);
}

void dfs(int p,int k,ll now) {
	//dd(p); dd(k); de(now);
	if (k==n) {
		if (now>ans) {
			ans=now;
			rep(i,1,n) ans2[i]=ans1[i];
		}
		return ;
	}
	rep(i,p,min(17,sz(v)-1)) {
		rep(j,1,k) now+=dis(ans1[j],i);
		ans1[k+1]=i;
		dfs(i,k+1,now);
		rep(j,1,k) now-=dis(ans1[j],i);
	}
}

int main() {
	init();
    cin>>n>>r;
    rep(i,-r,r)
    rep(j,-r,r) {
    	if (i*i+j*j<=r*r) v.pb(mp(i,j));
    }
    sort(v.begin(),v.end(),cmp);
    dfs(0,0,0);
    cout<<ans<<"\n";
    rep(i,1,n) cout<<v[ans2[i]].fi<<" "<<v[ans2[i]].se<<"\n";
	return 0;
}


//lxrtxdy!
#include<bits/stdc++.h>
using namespace std;
struct lsg{
	int x,y;
}a[100000];
int n,t,ans,kk,z[100],zz[100];
int doit(int x,int y)
{
	return(a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y);
}
int dfs(int d,int dd,int sum)
{
	if(d==n+1)
	{
		ans=sum;
		for(int i=1;i<=n;i++)zz[i]=z[i];
		return 0;
	}
	for(int i=dd;i<=kk;i++)
	{
		int sum1=sum;
		for(int j=1;j<d;j++)sum1+=doit(z[j],i);
		z[d]=i;
		if(sum1+(n*n-d*d)*4*t*t>ans)dfs(d+1,i,sum1);
	}
}
bool pd(lsg x,lsg y)
{
	return x.x*x.x+x.y*x.y>y.y*y.y+y.x*y.x;
}
int main(void)
{
	ios::sync_with_stdio(false);
	cin>>n>>t;
	for(int i=-t;i<=t;i++)
	for(int j=-t;j<=t;j++)if(i*i+j*j<=t*t)a[++kk].x=i,a[kk].y=j;
	sort(a+1,a+1+kk,pd);
	kk=min(kk,20);
	dfs(1,1,0);
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<a[zz[i]].x<<' '<<a[zz[i]].y<<endl;
	return 0;
}

//orzlxr!


#include<bits/stdc++.h>
using namespace std;
struct lsg{
	int x,y;
}a[100000];
int n,t,ans,kk,z[100],zz[100];
int doit(int x,int y)
{
	return(a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y);
}
int dfs(int d,int dd,int sum)
{
	if(d==n+1)
	{
		ans=sum;
		for(int i=1;i<=n;i++)zz[i]=z[i];
		return 0;
	}
	for(int i=dd;i<=kk;i++)
	{
		int sum1=sum;
		for(int j=1;j<d;j++)sum1+=doit(z[j],i);
		z[d]=i;
		if(sum1+(n*n-d*d)*4*t*t>ans)dfs(d+1,i,sum1);
	}
}
bool pd(lsg x,lsg y)
{
	return x.x*x.x+x.y*x.y>y.y*y.y+y.x*y.x;
}
int main(void)
{
	ios::sync_with_stdio(false);
	cin>>n>>t;
	for(int i=-t;i<=t;i++)
	for(int j=-t;j<=t;j++)if(i*i+j*j<=t*t)a[++kk].x=i,a[kk].y=j;
	sort(a+1,a+1+kk,pd);
	kk=min(kk,20);
	dfs(1,1,0);
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)cout<<a[zz[i]].x<<' '<<a[zz[i]].y<<endl;
	return 0;
}


// luogu-judger-enable-o2
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXN 10000

using namespace std;

struct Point
{
    int x,y;
    Point(){}
    Point(int _x,int _y):x(_x),y(_y){}
}points[MAXN],o;

int tot=0,n,r,m; //m=候选的点

inline int dist(Point a,Point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

bool cmp(Point a,Point b)
{
    return dist(a,o)>dist(b,o);
}

void init()
{
    scanf("%d%d",&n,&r);
    for(int i=-r;i<=r;i++)
        for(int j=-r;j<=r;j++)
            if(dist(Point(i,j),o)<=r*r)
                points[++tot]=Point(i,j);
    sort(points+1,points+tot+1,cmp);
    m=min(18,max(n,tot)); //!!!!取前18个距离圆心最远的点,若找到的在半径为r的圆内的点个数不到n，剩下的点用(0,0)补齐！
}

int ans[MAXN],maxans;

int numofpoint[MAXN],stack[MAXN],top=0;

void DFS(int x,int used,int sum) //搜到第x个可行点，已经选了used个点，当前的两两距离平方和为sum
{
    if(used==n)
    {
        if(maxans<sum)
        {
            maxans=sum;
            memcpy(ans,stack,sizeof(stack));
        }
        return;
    }
    for(int i=x;i<=m;i++)
    {
        stack[++top]=i;
        int tmp=0;
        for(int j=1;j<top;j++)
            tmp+=dist(points[stack[j]],points[i]);
        DFS(i,used+1,sum+tmp); //!!!!!
        top--;
    }
}

int main()
{
    init();
    DFS(1,0,0);
    printf("%d\n",maxans);
    for(int i=1;i<=n;i++)
        printf("%d %d\n",points[ans[i]].x,points[ans[i]].y);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

struct point {
    int x, y;
    point (int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
};

int N, R, M, ans, pos[10], rec[10];
vector<point> vec;

 int dis (int x, int y) {
    return x * x + y * y;
}

inline bool cmp (const point& a, const point& b) {
    return dis(a.x, a.y) > dis(b.x, b.y);
}

void init () {
    scanf("%d%d", &N, &R);
    for (int i = -R; i <= R; i++) {
        for (int j = -R; j <= R; j++)  {
            if (i * i + j * j <= R * R)
                vec.push_back(point(i, j));
        }
    }

    ans = 0;
    M = min((int)vec.size(), 18);
    sort(vec.begin(), vec.end(), cmp);
}

void dfs (int d, int f, int s) {
    if (d == N) {
        if (s > ans) {
            ans = s;
            memcpy(rec, pos, sizeof(pos));
        }
        return;
    }

    for (int i = f; i < M; i++) {
        int add = 0;
        for (int j = 0; j < d; j++)
            add += dis(vec[pos[j]].x - vec[i].x, vec[pos[j]].y - vec[i].y);
        pos[d] = i;
        dfs(d + 1, i, s + add);
    }
}

int main () {
    init();
    dfs(0, 0, 0);
    printf("%d\n", ans);
    for (int i = 0; i < N; i++)
        printf("%d %d\n", vec[rec[i]].x, vec[rec[i]].y);
    return 0;
}


// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define Love using
#define U namespace
#define RsXxy std
#define re register
Love U RsXxy;

struct Point{
	int x, y;
	Point(int x = 0,int y = 0){
		this->x = x;
		this->y = y;
	}
};

int N, R, M, ans, pos[10], rec[10];
vector<Point> vec;

inline int dis(int x,int y){
	return x * x + y * y;
}

inline bool cmp(const Point &a,const Point &b){
	return dis(a.x,a.y) > dis(b.x,b.y);
}

inline void init(){
	scanf("%d%d", &N, &R);
	for(int re i = -R;i <= R; ++i)
		for(int re j = -R;j <= R; ++j)
			if(i * i + j * j <= R * R) vec.push_back(Point(i, j));
	ans = 0;
	M = min((int)vec.size(),18);
	sort(vec.begin(),vec.end(),cmp);
}

inline void dfs (int d, int f, int s){
	if(d == N){
		if(s > ans) {ans = s; memcpy(rec, pos, sizeof(pos));}
		return ;
	}

	for(int re i = f;i < M; ++i){
		int add = 0;
		for(int re j = 0;j < d;++j)
			add += dis(vec[pos[j]].x - vec[i].x, vec[pos[j]].y - vec[i].y);
		pos[d] = i;
		dfs(d + 1, i, s + add);
	}
}

inline void Write(int x){
	if(x < 0) putchar('-'),x = -x;
	if(x > 9) Write(x/10);
	putchar(x % 10 + '0');
}

int main(){
	init();
	dfs(0, 0, 0);
	printf("%d\n", ans);
	for(int re i = 0;i < N; ++i)
		Write(vec[rec[i]].x),putchar(' '),Write(vec[rec[i]].y),putchar('\n');
	return 0;
}


//
//  main.cpp
//  J - Roland and Rose
//
//  Created by 李承忱 on 15/8/18.
//  Copyright (c) 2015年 李承忱. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n,r;
int ans=0;

struct node{
    int x;
    int y;
    node(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    bool operator==(node &a)
    {
        if(x==a.x&&y==a.y) return true;
        return false;
    }
    void operator=(node &a)
    {
        x=a.x;
        y=a.y;
    }
};

vector<node> point;
node p[10];
node ansp[10];
node zero;
int sqofdis(node &a,node &b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

void dfs(int step,int num)
{
    if(step>=n)
    {

        if(num>ans)
        {
            ans=num;
            for(int i=0;i<n;i++)
                ansp[i]=p[i];
        }
        return;
    }
    for(vector<node>::iterator t=point.begin();t!=point.end();t++)
    {
        int um=0;
        p[step]=*t;
        for(int k=0;k<step;k++)
            um+=sqofdis(p[step],p[k]);
        dfs(step+1,num+um);

    }
    return;
}

int main()
{
    cin>>n>>r;

    for(int i=-r;i<=r;i++)
    {
        for(int j=-r;j<=r;j++)
        {
            node t(i,j);
            if(sqofdis(t,zero)<=r*r) point.push_back(t);
        }
    }
    //sort(point.begin(),point.end());
    dfs(0,0);
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ansp[i].x<<" "<<ansp[i].y<<endl;
    }
    return 0;
}


//
//  main.cpp
//  J - Roland and Rose
//
//  Created by 李承忱 on 15/8/18.
//  Copyright (c) 2015年 李承忱. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n,r;
int ans=0;

struct node{
    int x;
    int y;
    node(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    bool operator==(node &a)
    {
        if(x==a.x&&y==a.y) return true;
        return false;
    }
    void operator=(node &a)
    {
        x=a.x;
        y=a.y;
    }
};

vector<node> point;
node p[10];
node ansp[10];
node zero;
int size=0;

int sqofdis(node &a,node &b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

void dfs(int step,int num)
{
    if(step>=n)
    {

        if(num>ans)
        {
            ans=num;
            for(int i=0;i<n;i++)
                ansp[i]=p[i];
        }
        return;
    }
    int j=0;
    for(vector<node>::iterator t=point.begin();t!=point.end()&&j<size;t++,j++)
    {
        int um=0;
        p[step]=*t;
        for(int k=0;k<step;k++)
            um+=sqofdis(p[step],p[k]);
        dfs(step+1,num+um);

    }
    return;
}

bool cmp(node &a,node &b)
{
    return sqofdis(a,zero)>sqofdis(b,zero);
}

int main()
{
    cin>>n>>r;

    for(int i=-r;i<=r;i++)
    {
        for(int j=-r;j<=r;j++)
        {
            node t(i,j);
            if(sqofdis(t,zero)<=r*r) point.push_back(t);
        }
    }
    sort(point.begin(),point.end(),cmp);
    size=(int)point.size()>18?18:(int)point.size();
    dfs(0,0);
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ansp[i].x<<" "<<ansp[i].y<<endl;
    }
    return 0;
}


//
//  main.cpp
//  J - Roland and Rose
//
//  Created by 李承忱 on 15/8/18.
//  Copyright (c) 2015年 李承忱. All rights reserved.
//

#include <iostream>
using namespace std;

int n,r;
int ans=0;

struct node{
    int x;
    int y;
    node(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    bool operator==(node &a)
    {
        if(x==a.x&&y==a.y) return true;
        return false;
    }
    void operator=(node &a)
    {
        x=a.x;
        y=a.y;
    }
};

node p[10];
node ansp[10];
node zero;
int sqofdis(node &a,node &b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

void dfs(int step,int num)
{
    if(step>=n)
    {

        if(num>ans)
        {
            ans=num;
            for(int i=0;i<n;i++)
                ansp[i]=p[i];
        }
        return;
    }
    for(int i=-r;i<=r;i++)
    {
        for(int j=-r;j<=r;j++)
        {
            node t(i,j);
            int um=0;
            if(sqofdis(t,zero)>r*r) continue;
            p[step]=t;
            for(int k=0;k<step;k++)
                um+=sqofdis(p[step],p[k]);
            dfs(step+1,num+um);
        }
    }
    return;
}

int main()
{
    cin>>n>>r;
    dfs(0,0);
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ansp[i].x<<" "<<ansp[i].y<<endl;
    }
    return 0;
}


//
//  main.cpp
//  J - Roland and Rose
//
//  Created by 李承忱 on 15/8/18.
//  Copyright (c) 2015年 李承忱. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;

int n,r;
int ans=0;

struct node{
    int x;
    int y;
    node(int a=0,int b=0)
    {
        x=a;
        y=b;
    }
    bool operator==(node &a)
    {
        if(x==a.x&&y==a.y) return true;
        return false;
    }
    //void operator=(node &a)
    //{
    //    x=a.x;
    //    y=a.y;
    //}
};

vector<node> point;
node p[10];
node ansp[10];
node zero;
int size=0;

int sqofdis(node &a,node &b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

void dfs(int step,int pos,int num)
{
    if(step>=n)
    {

        if(num>ans)
        {
            ans=num;
            for(int i=0;i<n;i++)
                ansp[i]=p[i];
        }
        return;
    }

    for(int j=pos;j<size;j++)
    {
        int um=0;
        p[step]=point[j];
        for(int k=0;k<step;k++)
            um+=sqofdis(p[step],p[k]);
        dfs(step+1,j,num+um);
    }
    return;
}

bool cmp(const node &a, const node &b)
{
    return a.x*a.x+a.y*a.y>b.x*b.x+b.y*b.y;
}

int main()
{
    cin>>n>>r;

    for(int i=-r;i<=r;i++)
    {
        for(int j=-r;j<=r;j++)
        {
            node t(i,j);
            if(sqofdis(t,zero)<=r*r) point.push_back(t);
        }
    }
    sort(point.begin(),point.end(),cmp);
    size=(int)point.size()>20?20:(int)point.size();
    dfs(0,0,0);
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<ansp[i].x<<" "<<ansp[i].y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define N 1020
#define ll long long
#define pdd pair<int, int>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int dis(pdd a, pdd b = pdd(0, 0)) {
	return (a.first - b.first) * (a.first - b.first)
		+ (a.second - b.second) * (a.second - b.second);
}
int n, k;
pdd aa[500];
pdd st[50];
pdd as[50];
int ans, top;
void dfs(int x, int now) {
	if (x == n+1) {
		if (now > ans) {
			ans = now;
			for (int i = 1; i <= n; i++)
				as[i] = st[i];
		}
		return;
	}
	for (int i = 1; i <= top; i++) {
		st[x] = aa[i];
		int sum = 0;
		for (int j = 1; j < x; j++)
			sum += dis(st[j], st[x]);
		dfs(x + 1, now + sum);
	}
}
int main(int argc, char const *argv[]) {
	cin >> n >> k;
	for (int i = -k; i <= k; i++)
		for (int j = -k; j <= k; j++)
			if (dis(pdd(i, j)) <= k*k)
				aa[++top] = pdd(i, j);
	dfs(1, 0);
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		printf("%d %d\n", as[i].first, as[i].second);
	return 0;
}


#include <bits/stdc++.h>
#define N 1020
#define ll long long
#define pdd pair<int, int>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int dis(pdd a, pdd b = pdd(0, 0)) {
	return (a.first - b.first) * (a.first - b.first)
		+ (a.second - b.second) * (a.second - b.second);
}
int n, k;
pdd aa[50020];
pdd st[50020];
pdd as[50020];
int ans, top;
void dfs(int x, int now) {
	if (x == n+1) {
		if (now > ans) {
			ans = now;
			for (int i = 1; i <= n; i++)
				as[i] = st[i];
		}
		return;
	}
	if (now + k*k*(n-x) < ans)
		return;
	for (int i = 1; i <= top; i++) {
		st[x] = aa[i];
		int sum = 0;
		for (int j = 1; j < x; j++)
			sum += dis(st[j], st[x]);
		dfs(x + 1, now + sum);
	}
}
int main(int argc, char const *argv[]) {
	cin >> n >> k;
	for (int i = -k; i <= k; i++)
		for (int j = -k; j <= k; j++)
			if (dis(pdd(i, j)) <= k*k)
				aa[++top] = pdd(i, j);
	dfs(1, 0);
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		printf("%d %d\n", as[i].first, as[i].second);
	return 0;
}


#include <bits/stdc++.h>
#define N 1020
#define ll long long
#define pdd pair<int, int>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int dis(pdd a, pdd b = pdd(0, 0)) {
	return (a.first - b.first) * (a.first - b.first)
		+ (a.second - b.second) * (a.second - b.second);
}
int n, k;
pdd aa[50020];
pdd st[50020];
pdd as[50020];
int ans, top;
void dfs(int x, int now, int last) {
	// printf("%d %d\n", x, now);
	if (x == n+1) {
		if (now > ans) {
			ans = now;
			for (int i = 1; i <= n; i++)
				as[i] = st[i];
		}
		return;
	}
	if (now + 4*k*k*(n*(n-1)/2-(x-1)*(x-2)/2) <= ans)
		return;
	for (int i = last; i <= top; i++) {
		st[x] = aa[i];
		int sum = 0;
		for (int j = 1; j < x; j++)
			sum += dis(st[j], st[x]);
		dfs(x + 1, now + sum, i);
	}
}
bool cmp(const pdd &a, const pdd &b) {
	return dis(a) > dis(b);
}
int main(int argc, char const *argv[]) {
	cin >> n >> k;
	for (int i = -k; i <= k; i++)
		for (int j = -k; j <= k; j++)
			if (dis(pdd(i, j)) <= k*k)
				aa[++top] = pdd(i, j);
	sort(aa+1, aa+top+1, cmp);
	top = min(top, 18);
	dfs(1, 0, 1);
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		printf("%d %d\n", as[i].first, as[i].second);
	return 0;
}


#include <bits/stdc++.h>
#define N 1020
#define ll long long
#define pdd pair<int, int>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
	while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return f?x:-x;
}
int dis(pdd a, pdd b = pdd(0, 0)) {
	return (a.first - b.first) * (a.first - b.first)
		+ (a.second - b.second) * (a.second - b.second);
}
int n, k;
pdd aa[500];
pdd st[50];
pdd as[50];
int ans, top;
void dfs(int x, int now, int last) {
	if (x == n+1) {
		if (now > ans) {
			ans = now;
			for (int i = 1; i <= n; i++)
				as[i] = st[i];
		}
		return;
	}
	for (int i = last+1; i <= top; i++) {
		st[x] = aa[i];
		int sum = 0;
		for (int j = 1; j < x; j++)
			sum += dis(st[j], st[x]);
		dfs(x + 1, now + sum, i);
	}
}
int main(int argc, char const *argv[]) {
	cin >> n >> k;
	for (int i = -k; i <= k; i++)
		for (int j = -k; j <= k; j++)
			if (dis(pdd(i, j)) <= k*k)
				aa[++top] = pdd(i, j);
	dfs(1, 0, 0);
	printf("%d\n", ans);
	for (int i = 1; i <= n; i++)
		printf("%d %d\n", as[i].first, as[i].second);
	return 0;
}