#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <utility>
#include <deque>
#include <list>
#include <sstream>
#include <fstream>
#include <complex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <utility>
#include <iterator>
#include <numeric>
#include <climits>
#include <iomanip>

using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)

#define ll long long
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin >> n;
    string s;
    cin >> s;
    int c[26];
    for(int i=0; i<26; i++)
    	c[i] = 0;
    for(ll i=0; i<n; i++)
    {
    	int x = s[i]-'a';
    	c[x]++;
    }
    char ans[n];
    int x = 0;
    int y = 1;
    ll i = 0;
    while(i<n)
    {
    	while((c[x]==0 || x==y) && x<=26)
    	{
    		x++;
    	}
    	while((c[y]==0 || y==x) && y<=26)
    		y++;
    	if(c[x]<=c[y] && c[x]!=c[y]-1)
    	{
    		if(x<26)
	    	{
	    		ans[i] = 'a' + x;
	    		i++;
	    		c[x]--;
	    	}
	    	if(y<26)
	    	{
	    		ans[i] = 'a' + y;
	    		c[y]--;
	    		i++;
	    	}
    	}
    	else
    	{
    		if(y<26)
	    	{
	    		ans[i] = 'a' + y;
	    		c[y]--;
	    		i++;
	    	}
	    	if(x<26)
	    	{
	    		ans[i] = 'a' + x;
	    		c[x]--;
	    		i++;
	    	}
    	}

    }
    for(ll i =0; i<n; i++)
    	cout << ans[i];
    cout << endl;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <utility>
#include <deque>
#include <list>
#include <sstream>
#include <fstream>
#include <complex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <utility>
#include <iterator>
#include <numeric>
#include <climits>
#include <iomanip>

using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)

#define ll long long
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin >> n;
    string s;
    cin >> s;
    int c[26];
    for(int i=0; i<26; i++)
    	c[i] = 0;
    for(ll i=0; i<n; i++)
    {
    	int x = s[i]-'a';
    	c[x]++;
    }
    char ans[n];
    int x = 0;
    int y = 1;
    ll i = 0;
    while(i<n)
    {
    	while((c[x]==0 || x==y) && x<=26)
    	{
    		x++;
    	}
    	while((c[y]==0 || y==x) && y<=26)
    		y++;
    	if(c[x]>=c[y])
    	{
    		while(c[x]!=0 && c[y]!=0)
    		{
    			ans[i] = 'a'+x;
    			i++;
    			ans[i] = 'a'+y;
    			i++;
    			c[x]--;
    			c[y]--;
    		}
    		if(c[x]!=0)
    		{
    			ans[i] = 'a'+x;
    			i++;
    			c[x]--;
    		}
    	}
    	else
    	{
    		while(c[x]!=0 && c[y]!=0)
    		{
    			ans[i] = 'a'+y;
    			i++;
    			ans[i] = 'a'+x;
    			i++;
    			c[x]--;
    			c[y]--;
    		}
    		if(c[y]!=0)
    		{
    			ans[i] = 'a'+y;
    			i++;
    			c[y]--;
    		}
    	}
    }
    for(ll i =0; i<n; i++)
    	cout << ans[i];
    cout << endl;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <utility>
#include <deque>
#include <list>
#include <sstream>
#include <fstream>
#include <complex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <utility>
#include <iterator>
#include <numeric>
#include <climits>
#include <iomanip>

using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)

#define ll long long
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin >> n;
    string s;
    cin >> s;
    int c[26]={0};
    for(ll i=0; i<n; i++)
    {
    	int x = s[i]-'a';
    	c[x]++;
    }
    char ans[n];
    int x = 0;
    int y = 1;
    ll i = 0;
    while(i<n)
    {
    	while((c[x]==0 || x==y) && x<=26)
    	{
    		x++;
    	}
    	while((c[y]==0 || y==x) && y<=26)
    		y++;
    	if(c[x]<=c[y] && c[x]!=c[y]-1)
    	{
    		if(x<26)
	    	{
	    		ans[i] = 'a' + x;
	    		i++;
	    		c[x]--;
	    	}
	    	if(y<26)
	    	{
	    		ans[i] = 'a' + y;
	    		c[y]--;
	    		i++;
	    	}
    	}
    	else
    	{
    		if(y<26)
	    	{
	    		ans[i] = 'a' + y;
	    		c[y]--;
	    		i++;
	    	}
	    	if(x<26)
	    	{
	    		ans[i] = 'a' + x;
	    		c[x]--;
	    		i++;
	    	}
    	}

    }
    cout << ans << endl;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <utility>
#include <deque>
#include <list>
#include <sstream>
#include <fstream>
#include <complex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <utility>
#include <iterator>
#include <numeric>
#include <climits>
#include <iomanip>

using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)

#define ll long long
#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll c[26];
    char ans[n];
    for(ll i=0; i<26; i++)
    	c[i] = 0;
    for(ll i=0; i<n; i++)
    {
    	ll x = s[i]-'a';
    	c[x]++;
    }

    ll x = 0;
    for(ll i=0; i<26; i++)
    {
    	while(c[i]>0)
    	{
    		ans[x] = 'a' + i;
    		c[i]--;
    		x++;
    	}
    }
    for(ll i =0; i<n; i++)
    	cout << ans[i];
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main()
{
    cin>>n>>s;
    sort(s.begin(),s.end());
    cout<<s;
}


#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=100010;
int n,tr;
string s,ans;
int dd[30];
vector<int> mm,ad;
bool ss(int a, int b) {return dd[a]>dd[b];}
int main()
{
    //freopen("a.inp","r",stdin);
    cin>>n>>s;
    forinc(i,0,n-1) dd[s[i]-'a']++;
    forinc(i,0,25) if (dd[i]) mm.push_back(i);
    sort(mm.begin(),mm.end(),ss);
    if (mm.size()==1) {cout<<s; return 0;}
    int i=0;
    while (i<=mm.size()-2)
    {
        tr=ans.size()-1;
        if (dd[mm[i]]%2!=dd[mm[i+1]]%2)
        {
            if (ad.size()>1)
            {
                string S; S.clear();
                forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
                forinc(j,1,ad.size()-1)
                {
                    int tg=dd[ad[j]];
                    forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
                }
                if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
                ans+=S;ad.clear();
            }
            if (ad.size()==1)
            {
                int tg=dd[ad[0]];
                forinc(_i,1,tg/2) ans+=char(ad[0]+'a');
            }
            int a=dd[mm[i]],b=dd[mm[i+1]];
            if (a==b+1)
            {
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
            }
            else
            {
                int tg=a-(b+1); a-=tg;
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
            }
            if (ad.size()==1)
            {
                int tg=dd[ad[0]]; tg-=tg/2;
                forinc(_i,1,tg) ans+=char(ad[0]+'a');
                ad.clear();
            }
            i+=2;
        }
        else ad.push_back(mm[i]),i++;
    }
    int m=mm.size()-1;
    if (i==m+1) {cout<<ans; return 0;}
    ad.push_back(mm[mm.size()-1]);
    if (ad.size()==1)
    {
        forinc(i,0,tr) cout<<ans[i];
        int tg=dd[ad[0]];
        forinc(_i,1,tg/2) cout<<char(ad[0]+'a');tg-=tg/2;
        forinc(i,tr+1,ans.size()-1) cout<<ans[i];
        forinc(_i,1,tg) cout<<char(ad[0]+'a');
    }
    else if (ad.size()>1)
    {
        string S; S.clear();
        forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
        forinc(j,1,ad.size()-1)
        {
            int tg=dd[ad[j]];
            forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
        }
        if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
        ans+=S;
         cout<<ans;
    }
}


#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=100010;
int n;
string s,ans;
int dd[30];
vector<int> mm,ad;
bool ss(int a, int b) {return dd[a]>dd[b];}
int main()
{
    //freopen("a.inp","r",stdin);
    cin>>n>>s;
    forinc(i,0,n-1) dd[s[i]-'a']++;
    forinc(i,0,25) if (dd[i]) mm.push_back(i);
    sort(mm.begin(),mm.end(),ss);
    if (mm.size()==1) {cout<<s; return 0;}
    int i=0;
    while (i<=mm.size()-2)
    {
        if (dd[mm[i]]%2!=dd[mm[i+1]]%2)
        {
            if (ad.size()==1)
            {
                forinc(j,1,dd[ad[0]]) ans+=char(ad[0]+'a');
            }
            else if (ad.size()>1)
            {
                string S; S.clear();
                forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
                forinc(j,1,ad.size()-1)
                {
                    int tg=dd[ad[j]];
                    forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
                }
                if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
                ans+=S;
            }
            ad.clear();
            int a=dd[mm[i]],b=dd[mm[i+1]];
            if (a==b+1)
            {
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
            }
            else
            {
                int tg=a-(b+1); a-=tg;
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
            }
            i+=2;
        }
        else ad.push_back(mm[i]),i++;
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=100010;
int n;
string s,ans;
int dd[30];
vector<int> mm,ad;
bool ss(int a, int b) {return dd[a]>dd[b];}
int main()
{
    //freopen("a.inp","r",stdin);
    cin>>n>>s;
    forinc(i,0,n-1) dd[s[i]-'a']++;
    forinc(i,0,25) if (dd[i]) mm.push_back(i);
    sort(mm.begin(),mm.end(),ss);
    if (mm.size()==1) {cout<<s; return 0;}
    int i=0;
    while (i<=mm.size()-2)
    {
        if (dd[mm[i]]%2!=dd[mm[i+1]]%2)
        {
            if (ad.size()==1)
            {
                forinc(j,1,dd[ad[0]]) ans+=char(ad[0]+'a');
            }
            else if (ad.size()>1)
            {
                string S; S.clear();
                forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
                forinc(j,1,ad.size()-1)
                {
                    int tg=dd[ad[j]];
                    forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
                }
                if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
                ans+=S;
            }
            ad.clear();
            int a=dd[mm[i]],b=dd[mm[i+1]];
            if (a==b+1)
            {
                int o=0;
                while (a && b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                if (a) ans+=char(mm[i]+'a');
                else ans+=char(mm[i+1]+'a');
            }
            else
            {
                int tg=a-(b+1);
                forinc(j,1,tg) ans+=char(mm[i]+'a'),a--;
                int o=0;
                while (a && b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                forinc(j,1,tg) ans+=char(mm[i]+'a'),a--;
            }
            i+=2;
        }
        else ad.push_back(mm[i]),i++;
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=100010;
int n;
string s,ans;
int dd[30];
vector<int> mm,ad;
bool ss(int a, int b) {return dd[a]>dd[b];}
int main()
{
    //freopen("a.inp","r",stdin);
    cin>>n>>s;
    forinc(i,0,n-1) dd[s[i]-'a']++;
    forinc(i,0,25) if (dd[i]) mm.push_back(i);
    sort(mm.begin(),mm.end(),ss);
    if (mm.size()==1) {cout<<s; return 0;}
    int i=0;
    while (i<=mm.size()-2)
    {
        if (dd[mm[i]]%2!=dd[mm[i+1]]%2)
        {
            if (ad.size()==1)
            {
                forinc(j,1,dd[ad[0]]) ans+=char(ad[0]+'a');
            }
            else if (ad.size()>1)
            {
                string S; S.clear();
                forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
                forinc(j,1,ad.size()-1)
                {
                    int tg=dd[ad[j]];
                    forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
                }
                if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
                ans+=S;
            }
            ad.clear();
            int a=dd[mm[i]],b=dd[mm[i+1]];
            if (a==b+1)
            {
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
            }
            else
            {
                int tg=a-(b+1); a-=tg;
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
            }
            i+=2;
        }
        else ad.push_back(mm[i]),i++;
    }
    int m=mm.size()-1;
    if (i==m+1) {cout<<ans; return 0;}
    ad.push_back(mm[mm.size()-1]);
    if (ad.size()==1)
    {
        forinc(j,1,dd[ad[0]]) ans+=char(ad[0]+'a');
    }
    else if (ad.size()>1)
    {
        string S; S.clear();
        forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
        forinc(j,1,ad.size()-1)
        {
            int tg=dd[ad[j]];
            forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
        }
        if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
        ans+=S;
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int N=100010;
int n,tr;
string s,ans;
int dd[30];
vector<int> mm,ad;
bool ss(int a, int b) {return dd[a]>dd[b];}
int main()
{
    //freopen("a.inp","r",stdin);
    cin>>n>>s;
    forinc(i,0,n-1) dd[s[i]-'a']++;
    forinc(i,0,25) if (dd[i]) mm.push_back(i);
    sort(mm.begin(),mm.end(),ss);
    if (mm.size()==1) {cout<<s; return 0;}
    int i=0;
    while (i<=mm.size()-2)
    {
        if (dd[mm[i]]%2!=dd[mm[i+1]]%2)
        {
            if (ad.size()>1)
            {
                string S; S.clear();
                forinc(j,1,dd[ad[0]]) S+=char(ad[0]+'a');
                forinc(j,1,ad.size()-1)
                {
                    int tg=dd[ad[j]];
                    forinc(_i,1,tg/2) S=char(ad[j]+'a')+S+char(ad[j]+'a');
                }
                if (dd[ad[0]]%2==1) forinc(j,1,ad.size()-1) S+=char(ad[j]+'a');
                ans+=S;ad.clear();
            }
            tr=ans.size()-1;
            if (ad.size()==1)
            {
                int tg=dd[ad[0]];
                forinc(_i,1,tg/2) ans+=char(ad[0]+'a');
            }
            int a=dd[mm[i]],b=dd[mm[i+1]];
            if (a==b+1)
            {
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
            }
            else
            {
                int tg=a-(b+1); a-=tg;
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
                int o=0;
                while (a || b)
                {
                    if (!o) ans+=char(mm[i]+'a'),o=1,a--;
                    else ans+=char(mm[i+1]+'a'),o=0,b--;
                }
                forinc(j,1,tg/2) ans+=char(mm[i]+'a');
            }
            if (ad.size()==1)
            {
                int tg=dd[ad[0]]; tg-=tg/2;
                forinc(_i,1,tg) ans+=char(ad[0]+'a');
                ad.clear();
            }
            i+=2;
        }
        else ad.push_back(mm[i]),i++;
    }
    int m=mm.size()-1;
    if (i==m+1) {cout<<ans; return 0;}
    ad.push_back(mm[mm.size()-1]);
    if (ad.size()==1)
    {
        forinc(i,0,tr) cout<<ans[i];
        int tg=dd[ad[0]];
        forinc(_i,1,tg/2) cout<<char(ad[0]+'a');tg-=tg/2;
        forinc(i,tr+1,ans.size()-1) cout<<ans[i];
        forinc(_i,1,tg) cout<<char(ad[0]+'a');
    }
    else if (ad.size()>1)
    {
        forinc(i,0,tr) cout<<ans[i];
        forinc(j,0,ad.size()-1)
        {
            int tg=dd[ad[j]];
            forinc(i,1,tg/2) cout<<char(ad[j]+'a');
        }
        forinc(i,tr+1,ans.size()-1) cout<<ans[i];
        fordec(j,ad.size()-1,0)
        {
            int tg=dd[ad[j]];
            forinc(_i,1,tg/2) cout<<char(ad[j]+'a');
        }
        if (dd[ad[0]]%2==1) forinc(j,0,ad.size()-1) cout<<char(ad[j]+'a');
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree < int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>Set;
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ld long double
#define ef else if
#define e else
#define loc(x) (x).begin(),(x).end()
#define ii pair<int,int>
#define no puts("No")
#define yes puts("Yes")
#define exe puts("-1")
#define en puts("")
#define N (int)5e5+10
#define ci const int
#define cs const string
#define cd const double
#define cll const ll

int t,fr[26];
string x;

int main()
{
	cin.tie(0); cout.tie(0); cin >> t >> x;
	for(int i = 0 ; i < t ; i++)fr[x[i]-'a']++;
        x = "";
	for(int i = 0 ; i < 26 ; i++)
	{
		while(fr[i]--)
		{
			x += char(i + 'a');
		}
	}
	cout << x << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree < int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>Set;
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ld long double
#define ef else if
#define e else
#define loc(x) (x).begin(),(x).end()
#define ii pair<int,int>
#define no puts("No")
#define yes puts("Yes")
#define exe puts("-1")
#define en puts("")
#define N (int)5e5+10
#define ci const int
#define cs const string
#define cd const double
#define cll const ll

int t,fr[26];
string x;

int main()
{
	cin.tie(0); cout.tie(0); cin >> t >> x;
	for(int i = 0 ; i < t ; i++)fr[x[i]-'a']++;
	x.clear();

	for(int i = 0 ; i < 26 ; i++)
	{
		while(fr[i]--)
		{
			x = x + char(i + 'a');
		}
	}
	cout << x << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree < int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>Set;
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ld long double
#define ef else if
#define e else
#define loc(x) (x).begin(),(x).end()
#define ii pair<int,int>
#define no puts("No")
#define yes puts("Yes")
#define exe puts("-1")
#define en puts("")
#define N (int)5e5+10
#define ci const int
#define cs const string
#define cd const double
#define cll const ll

int t,fr[26];
string x;

int main()
{
	cin.tie(0); cout.tie(0); cin >> t >> x;
	sort(loc(x));

	cout << x;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author HARSHAD
 */

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

#define int long long
#define f(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)

class COhThosePalindromes {
public:
	void solve(std::istream& in, std::ostream& out) {
		int n; in >> n;
        string s; in >> s;
        vector<int> freq(26, 0);
        for(char c: s) freq[c-'a']++;
        multiset<pair<int, int>> ms;
        f(i, 26){
            ms.insert({freq[i], i});
        }
        while(ms.size() > 1){
            auto ma = *prev(ms.end());
            auto ma2 = *prev(prev(ms.end()));
            ms.erase(prev(ms.end()));
            ms.erase(prev(ms.end()));
            int f1 = ma.first;
            int f2 = ma2.first;
            char c1 = 'a' + ma.second;
            char c2 = 'a' + ma2.second;
            f(i, f2) out << c1 << c2;
            if(f1-f2 > 0) ms.insert({f1-f2, ma.second});
        }
        if(ms.size() == 1){
            auto ma = *ms.begin();
            char c = 'a' + ma.second;
            f(i, ma.first) out << c;
        }
	}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	COhThosePalindromes solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author HARSHAD
 */

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

#define int long long
#define f(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)

class COhThosePalindromes {
public:
	void solve(std::istream& in, std::ostream& out) {
		int n; in >> n;
        string s; in >> s;
        vector<int> freq(26, 0);
        for(char c: s) freq[c-'a']++;
        multiset<pair<int, int>> ms;
        f(i, 26){
            ms.insert({freq[i], i});
        }
        while(ms.size() > 1){
            auto ma = *prev(ms.end());
            auto ma2 = *prev(prev(ms.end()));
            ms.erase(prev(ms.end()));
            ms.erase(prev(ms.end()));
            int f1 = ma.first;
            int f2 = ma2.first;
            char c1 = 'a' + ma.second;
            char c2 = 'a' + ma2.second;
            f(i, f2) out << c1 << c2;
            if(f1 > f2) out << c1, --f1;
            if(f1-f2 > 0) ms.insert({f1-f2, ma.second});
        }
        if(ms.size() == 1){
            auto ma = *ms.begin();
            char c = 'a' + ma.second;
            f(i, ma.first) out << c;
        }
	}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	COhThosePalindromes solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author HARSHAD
 */

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

#define int long long
#define f(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)

class COhThosePalindromes {
public:
	void solve(std::istream& in, std::ostream& out) {
		int n; in >> n;
        string s; in >> s;
        vector<int> freq(26, 0);
        for(char c: s) freq[c-'a']++;
        f(i, 26){
            char c = 'a' + i;
            f(j, freq[i]) out << c;
        }
	}
};

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	COhThosePalindromes solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string.h>
using namespace std;

const int MAX_N = 200000+10;

vector <int> G[MAX_N];
int n,k;

int main(){
	cin>>n>>k;
	for(int i=k+2;i<=n;i++){
		G[k+1].push_back(i);
	}
	int x = 1;
	if(G[k+1].size()==1){
		G[k+1].push_back(x);
		x++;
		cout<<3<<endl;
	}else{
		if(G[k+1].size()>1){
			cout<<4<<endl;
		}else{
			cout<<2<<endl;
		}
	}
	for(int i=k+2;i<=n;i++)if(G[i].size()==0){
		if(x<=k){
			G[i].push_back(x);
			x++;
		}else{
			G[i].push_back(i-1);
		}
	}
	for(int i=x;i<=k;i++){
		G[k+1].push_back(i);
	}

	for(int i=1;i<=n;i++){
		for(int j=0;j<G[i].size();j++){
			cout<<i<<" "<<G[i][j]<<endl;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < (int) (n); i++)
#define dforn(i, n) for(int i = (n) - 1; i >= 0; i--)
typedef long long ll;
const int MAXN = 400050;

int n, k, nxt, gr[MAXN], cnt[MAXN], mxgr;
vector<int> G[MAXN];
queue<int> Q;

int main(){
  scanf("%d %d", &n, &k); nxt = 1;
  gr[0] = 0; cnt[0]++;
  forn(i, k){
    G[0].push_back(nxt);
    Q.push(nxt);
    gr[nxt] = 1; cnt[1]++;
    mxgr = max(mxgr, gr[nxt]);
    nxt++;
  }
  while(!Q.empty()){
    if(nxt == n) break;
    int v = Q.front(); Q.pop();
    G[v].push_back(nxt);
    Q.push(nxt);
    gr[nxt] = gr[v] + 1;
    cnt[ gr[nxt] ]++;
    mxgr = max(mxgr, gr[nxt]);
    nxt++;
  }

  int mx = (n - 1) / k + ((n - 1) % k == 0 ? 0 : 1);

  printf("%d\n", mx * 2 + (cnt[mxgr] >= 2 ? 0 : -1) );
  forn(i, n){
    forn(j, G[i].size()){
      printf("%d %d\n", i + 1, G[i][j] + 1);
    }
  }
  return 0;
}


# include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	int tmp=n-k-1;
	int ans=(n-1)/k;
	ans*=2;
	ans+=min(2,(n-1)%k);
	cout<<ans<<endl;
	for(int j=2;j<=k;j++)
	cout<<1<<" "<<j<<endl;
	for(int i=k+1;i<=n;i++)
	cout<<i<<" "<<i-k<<endl;;
    return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = （n-1） / k;
	int y = n % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k); n--;
	int t = n / k;
	int y = n % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = (n-1) % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = n % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k); n--;
	int t = n / k;
	int y = n % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k ; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d",i-k,i);
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = (n-1) % k;
	printf("%d\n",2*t+(y > 2 ? 2 :y));
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d\n",(j+1)+k*(i-1),(j+1)+k*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d\n",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = (n-1) % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d\n",(j+1)+t*(i-1),(j+1)+t*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d\n",i-k,i);
	return 0;
}


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

int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = (n-1) % k;
	if(y == 0)
		cout<<2*t<<endl;
	else if(y == 1)
		cout<<2*t+1<<endl;
	else
		cout<<2*t+2<<endl;
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d\n",(j+1)+k*(i-1),(j+1)+k*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d\n",i-k,i);
	return 0;
}


#include <cstdio>
using namespace std;
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int t = (n-1) / k;
	int y = (n-1) % k;
	printf("%d\n",2*t+(y > 2 ? 2 :y));
	for(int i = 2; i <= k + 1; i++)
		printf("1 %d\n",i);
	for(int i = 1; i < t ; i++)
		for(int j = 1 ; j <= k ; j++)
			printf("%d %d\n",(j+1)+k*(i-1),(j+1)+k*(i-1)+k);
	for(int i = n ; i >= n - y + 1 ; i--)
		printf("%d %d\n",i-k,i);
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int up(int a, int b)
{
	int ans = a / b;
	if(a % b) ans++;
	return ans;
}
int main()
{
	int n, k;
	scanf("%d%d", &n, &k);
	int lst = n - k - 1;
	int ans1 = up(lst, k);
	lst -= ans1;
	int ans2 = up(lst, k - 1);
	printf("%d\n", ans1 + ans2 + 2);
	lst = n - k - 1;
	int cnt = 2;
	for(int i = 1; i <= k; i++)
	{
		int len = up(lst, k - i + 1);
		lst -= len;
		int lpl = 1;
		for(int j = 1; j <= len + 1; j++)
			printf("%d %d\n", lpl, cnt), lpl = cnt, cnt++;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n, K;
int main() {
	int i, j, k;
	scanf("%d%d", &n, &K);
	int p = n/K, o = n%K, ret;
	if(o <= 1) ret = 0;
	else if(o == 2) ret = 1;
	else ret = 2;
	printf("%d\n", ret+2*p);
	if(o == 2){
		printf("1 2\n");
		for(i = o+1; i <= o+K; i++) printf("%d %d\n", i-K<1?1:i-K, i);
		for(i = o+K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	} else if(o > 2){
		for(i = 2; i <= o; i++) printf("1 %d\n", i);
		for(i = o+1; i <= o+K; i++) printf("%d %d\n", i-K<2?2:i-K, i);
		for(i = o+K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	} else if(o == 1){
		for(i = 2; i <= K+1; i++) printf("1 %d\n", i);
		for(i = K+2; i <= n; i++) printf("%d %d\n", i-K, i);
	} else {
		for(i = 2; i <= K; i++) printf("1 %d\n", i);
		for(i = K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n, K;
int main() {
	int i, j, k;
	scanf("%d%d", &n, &K);
	int p = n/K, o = n%K, ret;
	if(o == 0){
		if(K == 2) ret = -1;
		else ret = 0;
	} else if(o == 1) ret = 0;
	else if(o == 2) ret = 1;
	else ret = 2;
	printf("%d\n", ret+2*p);
	if(o == 2){
		printf("1 2\n");
		for(i = o+1; i <= o+K; i++) printf("%d %d\n", i-K<1?1:i-K, i);
		for(i = o+K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	} else if(o > 2){
		for(i = 2; i <= o; i++) printf("1 %d\n", i);
		for(i = o+1; i <= o+K; i++) printf("%d %d\n", i-K<2?2:i-K, i);
		for(i = o+K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	} else if(o == 1){
		for(i = 2; i <= K+1; i++) printf("1 %d\n", i);
		for(i = K+2; i <= n; i++) printf("%d %d\n", i-K, i);
	} else {
		for(i = 2; i <= K; i++) printf("1 %d\n", i);
		for(i = K+1; i <= n; i++) printf("%d %d\n", i-K, i);
	}
	return 0;
}


#include<stdio.h>
int main()
{
	int num, gen;
	scanf("%d%d", &num, &gen);
	int sum = num - gen - 1;
	printf("%d\n", (sum + gen - 1) / gen + 1 + (sum + gen - 2) / gen + 1);
	int pt = 2;
	for (int i = 0; i < gen; i++)
	{
		int ko = sum / gen;
		if (i < sum%gen)ko++;
		for (int j = 0; j < ko; j++)printf("%d %d\n", pt, pt + 1), pt++;
		printf("%d %d\n", pt++, 1);
	}
}


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

#define mp(x, y) make_pair((x), (y))

typedef long long ll;

int n, k;
vector<int> sol[200005];

int solve(int n, int k) {
	if(k==n-1) {
		for(int i=1; i<n; i++) {
			sol[0].push_back(i);
		}
		return 2;
	}
	if(k==n-2) {
		sol[0].push_back(1);
		sol[0].push_back(2);
		for(int i=3; i<n; i++) {
			sol[1].push_back(i);
		}
		return 3;
	}
	int l=min(k, n-k-1);
	int d=solve(n-k, l);
	for(int i=n-k-l; i<n-k; i++) {
		sol[i].push_back(i+l);
	}
	for(int j=0; j<k-l; j++) {
		sol[n-k-1].push_back(n-k+l+j);
	}
	return d+2;
}

int main()
{
	scanf("%d%d", &n, &k);
	int ans=solve(n, k);
	printf("%d\n", ans);
	for(int i=0; i<n; i++) {
		for(int j=0; j<sol[i].size(); j++) printf("%d %d\n", i+1, sol[i][j]+1);
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > s;
void add_edge(int x,int y){s.push_back(make_pair(x,y));return ;}
void print_ans()
{
	cout<<s.size()<<endl;
	for(int i=0;i<s.size();i++)printf("%d %d\n",s[i].first,s[i].second);
	return ;
}
int main()
{
	int n,k;cin>>n>>k;
	n -= k;
	if(n<k)
	{
		for(int i=1;i<n;i++)add_edge(i,i+1);
//		printf("%d %d\n",i,i+1)	cout<<1<<" "<<n<<endl;
		add_edge(n+1,n);
		for(int i=2;i<=k;i++)add_edge(i+n,1);
//		printf("%d %d\n",i,1);
		print_ans();
		return 0;
	}
	for(int i=1;i<n;i++)add_edge(i,i+1);
/*	printf("%d %d\n",i,i+1);
	cout<<n+k  <<" "<<1<<endl;
	cout<<n+k-1<<" "<<n<<endl;*/
	add_edge(n+k,1);
	add_edge(n+k-1,n);
	int step = (n-1)/(k-1);
	int cur = n+1;
	for(int i=1+step;i<n;i+=step)add_edge(cur++,i);
//	printf("%d %d\n",cur++,i);
	print_ans();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > s;
void add_edge(int x,int y){s.push_back(make_pair(x,y));return ;}
void print_ans()
{
	for(int i=0;i<s.size();i++)printf("%d %d\n",s[i].first,s[i].second);
	return ;
}
int main()
{
	int n,k;cin>>n>>k;
	n -= k;
	for(int i=1;i<n;i++)add_edge(i,i+1);
	add_edge(n+k,1);
	add_edge(n+k-1,n);
	for(int i=3;i<=k;i++)add_edge(n+k-i+1,1);
	cout<<n+1<<endl;
	print_ans();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > s;
void add_edge(int x,int y){s.push_back(make_pair(x,y));return ;}
void print_ans()
{
	for(int i=0;i<s.size();i++)printf("%d %d\n",s[i].first,s[i].second);
	return ;
}
int main()
{
	int n,k;cin>>n>>k;
	n -= k;
	if(n==2)
	{
		add_edge(1,2);
		add_edge(1,3);
		for(int i=2;i<=k;i++)add_edge(2,2+i);
		cout<<3<<endl;
	}
	else
	{
		for(int i=1;i<n;i++)add_edge(i,i+1);
		if(n!=1)add_edge(n,1);
		for(int i=1;i<=k;i++)add_edge(1,n+i);
		cout<<2<<endl;
	}
	print_ans();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > s;
void add_edge(int x,int y){s.push_back(make_pair(x,y));return ;}
void print_ans()
{
	for(int i=0;i<s.size();i++)printf("%d %d\n",s[i].first,s[i].second);
	return ;
}
int main()
{
	int n,k;cin>>n>>k;
	n -= k;
	int step = (n-1)/k;
	int cur = 2,nn=n-1,kk=k;
	int ans = 0;
	for(int i=1;i<=k;i++)
	{
		if(i==k-1||i==k)ans += step;
		if(step)
		{
			add_edge(cur++,1);
			for(int j=2;j<=step;j++)add_edge(cur-1,cur++);
			add_edge(cur-1,n+k-i+1);
		}
		else add_edge(1,n+k-i+1);
		nn-=step;kk--;
		if(kk)step = nn/kk;
	}
	cout<<ans+2<<endl;
	print_ans();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > s;
void add_edge(int x,int y){s.push_back(make_pair(x,y));return ;}
void print_ans()
{
	for(int i=0;i<s.size();i++)printf("%d %d\n",s[i].first,s[i].second);
	return ;
}
int main()
{
	int n,k;cin>>n>>k;
	n -= k;
	int step = (n-1)/k;
	int cur = 2,nn=n-1,kk=k;
	int ans = 0;
	for(int i=1;i<=k;i++)
	{
		if(i==k-1||i==k)ans += step;
		if(step)
		{
			add_edge(cur++,1);
			for(int j=2;j<=step;j++)add_edge(cur-1,cur),cur++;
			add_edge(cur-1,n+k-i+1);
		}
		else add_edge(1,n+k-i+1);
		nn-=step;kk--;
		if(kk)step = nn/kk;
	}
	cout<<ans+2<<endl;
	print_ans();
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#define debug printf
using namespace std;
const int maxn=200000+10;
int n,k;
struct Edge{
	int u,v,next;
	Edge(){}
	Edge(int u,int v,int next)
	:u(u),v(v),next(next){}
};
int head[maxn];
int edgescnt;
Edge edges[maxn<<1];
void InitEdge()
{
	memset(head,-1,sizeof(head));
	edgescnt=0;
}
void AddEdge(int u,int v)
{

    edges[edgescnt]=Edge(u,v,head[u]);
    head[u]=edgescnt++;
}
void solve()
{
	InitEdge();
	int sonsz=(n-1+k-1)/k;
	int idx=2;
	int root=1;
	int bigsz=k+n-1-sonsz*k;
	int smallsz=k-bigsz;
//	debug("%d %d %d\n",bigsz,smallsz,sonsz);
	for(int i=1;i<=bigsz;i++){
		AddEdge(root,idx);
		idx++;
		for(int j=1;j<=sonsz-1;j++){
			AddEdge(idx-1,idx);
			idx++;
		}
	}
	for(int i=1;i<=smallsz;i++){
		AddEdge(root,idx);
		idx++;
		for(int j=1;j<=sonsz-2;j++){
			AddEdge(idx-1,idx);
			idx++;
		}
	}
	int ans=0;
	if(bigsz==1){
		ans=sonsz*2-1;
	}
	else ans=sonsz*2;
	printf("%d\n",ans);
	for(int i=1;i<=n;i++){
		for(int e=head[i];e!=-1;e=edges[e].next){
			printf("%d %d\n",edges[e].u,edges[e].v);
		}
	}

}

int main()
{
	while(~scanf("%d %d",&n,&k)){
		solve();
	}

	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int n,k;
int main()
{
    while(~scanf("%d%d",&n,&k)){
        int dis;
        dis = n - k + 1;
        printf("%d\n",dis);
        for(int i=1;i<n-k+1;i++){
            printf("%d %d\n",i,i+1);
        }
        for(int i=n-k+2;i<=n;i++){
            printf("%d %d\n",n-k+1,i);
        }
    }
    return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;
int n,k;
int main()
{
    while(~scanf("%d%d",&n,&k)){
        int ans;
        if ((n - 1) % k == 1) ans = (n - 1) / k  * 2 + 1;
        else if ((n - 1) % k == 0) ans = (n - 1) / k * 2;
        else ans = (n - 1) / k * 2 + 2;
        printf("%d\n", ans);
        for (int i = 2; i <= n - k; i++)
            printf("%d %d\n", max(1, i - k), i);
        for (int i = n - k + 1; i <= n; i++)
            printf("%d %d\n", max(1, i - k), i);
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>

using namespace std;

int n , k;
int ans;
void work () {
	int i , j , tmp , last;
	scanf ( "%d%d" , &n , &k );
	tmp = n - k - 1;
	ans = (tmp / k) * 2 + 2;
	if ( tmp % k >= 2 ) ans += 2;
	else ans += (tmp%k);
	last = 1;
	printf ( "%d\n" , ans );
	for ( i = 1 ; i <= tmp % k ; i++ ) {
		printf ( "%d %d\n" , 1 , ++last );
		for ( j = 2 ; j <= tmp / k + 2 ; j++ ) {
			printf ( "%d %d\n" , last , last + 1 );
			last++;
		}
	}
	for ( i = 1 ; i <= k-(tmp % k) ; i++ ) {
		printf ( "%d %d\n" , 1 , ++last );
		for ( j = 2 ; j <= tmp / k + 1 ; j++ ) {
			printf ( "%d %d\n" , last , last + 1 );
			last++;
		}
	}
}
int main () {
	work ();
	return 0;
}


#include <stdio.h>
#include <algorithm>

using namespace std;

int n , k;
int ans;
void work () {
	int i , j , tmp , last;
	scanf ( "%d%d" , &n , &k );
	tmp = n - k - 1;
	ans = tmp / k + 2;
	if ( tmp % k >= 2 ) ans += 2;
	else ans += (tmp%k);
	last = 1;
	printf ( "%d\n" , ans );
	for ( i = 1 ; i <= tmp % k ; i++ ) {
		printf ( "%d %d\n" , 1 , ++last );
		for ( j = 2 ; j <= tmp / k + 2 ; j++ ) {
			printf ( "%d %d\n" , last , last + 1 );
			last++;
		}
	}
	for ( i = 1 ; i <= k-(tmp % k) ; i++ ) {
		printf ( "%d %d\n" , 1 , ++last );
		for ( j = 2 ; j <= tmp / k + 1 ; j++ ) {
			printf ( "%d %d\n" , last , last + 1 );
			last++;
		}
	}
}
int main () {
	work ();
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n,k,m;
int main()
{
    int x,num=1;
    scanf("%d%d",&n,&k);
    m=(n-1)/k;
    x=(n-1)%k;
    if(x==0)
        cout<<m*2<<endl;
    else if(x==1)
        cout<<m*2+1<<endl;
    else
        cout<<m*2+2<<endl;
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<m+1;j++)
        {
            if(j==0)
            {
                cout<<1<<" "<<num+1<<endl;
                num++;
            }
            else
            {
                cout<<num<<" "<<num+1<<endl;
                num++;
            }
        }
    }
    for(int i=x;i<k;i++)
    {
         for(int j=0;j<m;j++)
        {
            if(j==0)
            {
                cout<<1<<" "<<num+1<<endl;
                num++;
            }
            else
            {
                cout<<num<<" "<<num+1<<endl;
                num++;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int n,k,m;
int main()
{
    int x,num=1;
    scanf("%d%d",&n,&k);
    m=(n-1)/k;
    x=(n-1)%k;
    if(x==0)
        cout<<m*2<<endl;
    else
        cout<<m*2+1<<endl;
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<m+1;j++)
        {
            if(j==0)
            {
                cout<<1<<" "<<num+1<<endl;
                num++;
            }
            else
            {
                cout<<num<<" "<<num+1<<endl;
                num++;
            }
        }
    }
    for(int i=x;i<k;i++)
    {
         for(int j=0;j<m;j++)
        {
            if(j==0)
            {
                cout<<1<<" "<<num+1<<endl;
                num++;
            }
            else
            {
                cout<<num<<" "<<num+1<<endl;
                num++;
            }
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int main(){
    int n,k,ss,i,j,l,mn;
    scanf("%d %d",&n,&k);
    ss=(n-1)%k;
    if(ss==0) printf("%d\n",(n-1)/k*2),mn=1;
    else if(ss==1) printf("%d\n",(n-1)/k*2+1),mn=1;
    else printf("%d\n",(n-1)/k*2+2),mn=2;
    for(j=2;j<=ss+1;j++) printf("%d %d\n",1,j);
    for(j=ss+2;j<=n;j+=k)
        for(l=j;l<j+k;l++)
            printf("%d %d\n",max(mn,l-k),l);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
#define REP(i, a, b) for(int i = int(a); i <= int(b); i++)
#define LOOP(i, v) for(int i = 0; i < v.size(); i++)
#define EPS 1e-9
#define INF 1e12
#define debug(x) cerr << "DEBUG : " << (#x) << " => " << (x) << endl

int main()
{
  int n, k, x, etage, a, b;

  scanf("%d %d", &n, &k);

  x = n-k-1;
  etage = x/k;
  a = x%k;
  b = k-a;
  // a ont etage, b ont etage-1;

  if(a == 0) printf("%d\n", 2*(etage+1));
  else if(a == 1) printf("%d\n", 2*(etage+1)+1);
  else printf("%d\n", 2*(etage+1)+2);

  int nouveau = 2;

  REP(i, 1, a)
  {
    printf("%d %d\n", 1, nouveau);
    nouveau++;
    REP(j, 1, etage+1)
    {
      printf("%d %d\n", nouveau-1, nouveau);
      nouveau++;
    }
  }

  REP(i, 1, b)
  {
    printf("%d %d\n", 1, nouveau);
    nouveau++;
    REP(j, 1, etage)
    {
      printf("%d %d\n", nouveau-1, nouveau);
      nouveau++;
    }
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;

int n, m, k, last[MAXN];

int main(){
	int i, ans;
	scanf("%d%d", &n, &k);
	m = n - k;
	if(m <= k){
		if(m == 1){
			printf("2\n");
			for(i = 2; i <= n; ++ i)
				printf("1 %d\n", i);
			return 0;
		}
		if(m == 2){
			printf("3\n");
			printf("1 2\n1 3\n");
			for(i = 4; i <= n; ++ i)
				printf("2 %d\n", i);
			return 0;
		}
		printf("4\n");
		for(i = 2; i <= m; ++ i)
			printf("1 %d\n", i);
		for(i = 1; i < m; ++ i)
			printf("%d %d\n", i + 1, m + i);
		for(; i <= k; ++ i)
			printf("%d %d\n", 1, m + i);
		return 0;
	}
	ans = (m - 1) / k * 2;
	printf("%d\n", ans + 2 + min((m - 1) % k, 2));
	for(i = 1; i <= k; ++ i){
		printf("1 %d\n", i + 1);
		last[i % k] = i + 1;
	}
	for(i = k + 2; i <= m; ++ i){
		printf("%d %d\n", i - k, i);
		last[(i - 1) % k] = i;
	}
	for(i = 0; i < k; ++ i)
		printf("%d %d\n", last[i], i + m + 1);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define sqr(x) ((x) * (x))
#define pb push_back
#define mp make_pair
#define ins insert
#define X first
#define Y second
#define fin(name) freopen(name, "r", stdin)
#define fout(name) freopen(name, "w", stdout)
#define files(name) fin(name".in"); fout(name".out")
#define endl "\n"
#define fi(st,n) for (int i = (st); i <= (int)(n); ++i)
#define fj(st,n) for (int j = (st); j <= (int)(n); ++j)
#define fk(st,n) for (int k = (st); k <= (int)(n); ++k)
#define fq(st,n) for (int q = (st); q <= (int)(n); ++q)
#define fw(st,n) for (int w = (st); w <= (int)(n); ++w)
#define ff(i, st, n) for (int (i) = (st); (i) <= (int)(n); ++(i))
#define ei(st,n) for (int i = (st); i >= (int)(n); --i)
#define ej(st,n) for (int j = (st); j >= (int)(n); --j)
#define ek(st,n) for (int k = (st); k >= (int)(n); --k)
#define ef(i, st, n) for (int (i) = (st); (i) >= (int)(n); --(i))
#define ri(st,n) for (int i = (st); i < (int)(n); ++i)
#define rj(st,n) for (int j = (st); j < (int)(n); ++j)
#define rk(st,n) for (int k = (st); k < (int)(n); ++k)
#define rq(st,n) for (int q = (st); q < (int)(n); ++q)
#define rf(i, st, n) for (int (i) = (st); (i) < (int)(n); ++(i))
#define clean(a) memset((a),0,sizeof (a))
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#define y1 dsklmlvmd

typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ldbl;

const int inf = (int)1e9;
const ll linf = (ll)1e18;
const dbl eps = (dbl) 1e-8;
const int mod = (int) 1e9 + 7;
const int maxn = (int) 1e5 + 5;
//const dbl M_PI = (dbl)2 * (dbl)acos(0);

//cout<<fixed<<setprecision(10);
//srand(time(0));

int n, k, m, x, y, kx, ky;
int cnt, ans;

int main()
{
//    fin("t.in");
    sync;
    cin >> n >> k;
    m = n - k - 1;
    x = m / k;
    y = x + 1;
    ky = m % k;
 	kx = k - ky;

 	if (ky > 0)
 	{
 		if (ky > 1)
 			ans = 2 * (y + 1);
 		else
 			ans = x + y + 2;
 	}
 	else
 	{
 		ans = 2 * (x + 1);
 	}
// 	cout << x << " " << kx << endl;
// 	cout << y << " " << ky << endl;
 	vector <pair <int, int> > vans;
 	cnt = k + 1;
 	fi(1, kx)
 	{
 		fj(1, x)
 		{
 			if (j == 1)
 				vans.pb(mp(1, cnt + 1));
 			else
 				vans.pb(mp(cnt, cnt + 1));
 			++cnt;
 		}
 		if (x == 0)
 			vans.pb(mp(1, i + 1));
 		else
	 		vans.pb(mp(cnt, i + 1));
 	}
 	fi(1, ky)
 	{
 		fj(1, y)
 		{
 			if (j == 1)
 				vans.pb(mp(1, cnt + 1));
 			else
 				vans.pb(mp(cnt, cnt + 1));
 			++cnt;
 		}
 		if (y == 0)
 			vans.pb(mp(1, kx + i + 1));
 		else
	 		vans.pb(mp(cnt, kx + i + 1));
 	}

 	cout << ans << endl;
 	ri(0, vans.size())
 	{
 		cout << vans[i].X << " " << vans[i].Y << endl;
 	}
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int n,k,a[200002];
int main(){
	scanf("%d%d",&n,&k);
	int r=n-k-1;
	if(r%k==0)printf("%d\n",(r/k+1)*2);
	else if(r%k==1)printf("%d\n",(r/k+1)*2+1);
	else printf("%d\n",(r/k+1)*2+2);
	for(int i=1;i<=k;++i)a[i]=k+1;
	for(int i=1;i<=n-k-1;++i){
		int cur=i%k+1;
		printf("%d %d\n",a[cur],k+1+i);
		a[cur]=k+1+i;
	}
	for(int i=1;i<=k;++i)printf("%d %d\n",a[i],i);
}


#include<bits/stdc++.h>

typedef long long LL;
using namespace std;

#define fillchar(a, x) memset(a, x, sizeof(a))
#define MP make_pair
#define PB push_back
#define endl '\n'

const int M = (int)1e9+7;

int main()
{
//ios_base::sync_with_stdio(0);
cout.precision(15);
cout.setf(ios::fixed);

	int n,k;
	cin >>n >> k;

	int ans = (n-1+k-1)/k + (n-2+k-1)/k;

	cout << ans << endl;

	for(int i=2;i<=n;i++)
	{
		cout<<i<<' '<<max(1,i-k)<<endl;
	}

}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,a,b;
int main()
{
	scanf("%d%d",&n,&b);
	int a=n-b;
	if(b>=a-1)
	{
		if(a==1)
		{
			printf("2\n");
			for(int i=2;i<=n;++i)
			printf("1 %d\n",i);
		}
		else if(a==2)
		{
			printf("3\n1 2\n2 3\n3 4\n");
			for(int i=3;i<=b;++i)
			{
				printf("2 %d\n",i+2);
			}
		}
		else
		{
			printf("4\n");
			for(int i=2;i<=a;++i)
				printf("1 %d\n",i);
			for(int i=a+1;i<=n;++i)
				printf("%d %d\n",min(i-a+1,a),i);
		}
	}
	else
	{
		int p=(a-1)/b;
		if((a-1)==p*b)
		{
			printf("%d\n",2*(p+1));
			int now=2;
			int pre=1;
			for(int i=1;i<=b;++i)
			{
				pre=1;
				for(int j=1;j<=p+1;j++)
				{
					printf("%d %d\n",now,pre);
					pre=now; now++;
				}

			}
		}
		else  if(a-1-p*b==1)
		{
			printf("%d\n",2*(p+1)+1);
			int now=2;
			int pre=1;
			for(int i=1;i<=b;++i)
			{
				pre=1;
				for(int j=1;j<=p+1;j++)
				{
					printf("%d %d\n",now,pre);
					pre=now; now++;
				}

			}
			printf("%d %d\n",now,now-1);
		}
		else
		{
			printf("%d\n",2*(p+1)+2);
			int now=2;
			int pre=1;
			int t=a-1-p*b;
			for(int i=1;i<=t;++i)
			{
				pre=1;
				for(int j=1;j<=p+2;j++)
				{
					if(now>n) break;
					printf("%d %d\n",now,pre);
					pre=now; now++;
				}
			}
			for(int i=t+1;i<=b;++i)
			{
				pre=1;
				for(int j=1;j<=p+1;j++)
				{
					if(now>n) break;
					printf("%d %d\n",now,pre);
					pre=now; now++;
				}

			}
		}
	}
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main()
{
	ios::sync_with_stdio(false);

	int n, k;
	cin >> n >> k;

	int x = (n-1)/k;
	int ans = 2*x;
	if((n-1)%k)
		ans += 1;
	if(1 < (n-1)%k)
		ans += 1;

	cout << ans << "\n";

	int curr = 1;
	for(int i=0; i<k; ++i)
	{
		int nn = (n-1)/k;
		if(i < (n-1)%k)
			++nn;
		int prev = 0;
		for(int j=0; j<nn; ++j)
		{
			cout << prev+1 << " " << curr+1 << "\n";
			prev = curr;
			++curr;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200005;
LL read(){
	LL x=0;
	char ch=getchar();
	while (!isdigit(ch))
		ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+ch-48,ch=getchar();
	return x;
}
int n,k;
int main(){
	n=read(),k=read();
	int x=(n-1)/k,y=(n-1)%k;
	int tot=x*2;
	if (y>=1)
		tot++;
	if (y>=2)
		tot++;
	for (int i=n;i>1;i--)
		printf("%d %d\n",i,max(i-k,1));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200005;
LL read(){
	LL x=0;
	char ch=getchar();
	while (!isdigit(ch))
		ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+ch-48,ch=getchar();
	return x;
}
int n,k;
int main(){
	n=read(),k=read();
	int x=(n-1)/k,y=(n-1)%k;
	int tot=x*2;
	if (y>=1)
		tot++;
	if (y>=2)
		tot++;
	printf("%d\n",tot);
	for (int i=n;i>1;i--)
		printf("%d %d\n",i,max(i-k,1));
	return 0;
}


#include <cstdio>

#define Max(a,b) ((a)>(b)?(a):(b))
#define N 500050
int dis[N],col[N],fa[N];
int main()
{
	int n,k;scanf("%d%d",&n,&k);
	int left=n%k;
	if(!left) left=k;
	for(int i=2;i<=left;i++)
		dis[i]=1,col[i]=i,fa[i]=1;
	for(int i=left+1;i<=n;i++)
	{
		fa[i]=Max(1,i-k);
		dis[i]=dis[fa[i]]+1,col[i]=col[fa[i]];
	}
	printf("%d\n",dis[n-1]+dis[n]);
	for(int i=2;i<=n;i++)
		printf("%d %d\n",i,fa[i]);
}


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

int N, K, D, R;
int l, r, step;
int q[200010];

int main(){
	scanf("%d%d", &N, &K);
	D = (N - 1) / K;
	R = N - 1 - K * D;
	if (!R) printf("%d\n", D * 2);
	else if (R == 1) printf("%d\n", D * 2 + 1);
	else printf("%d\n", D * 2 + 2);
	l = 1;
	for (int i = 1; i <= K; i++)
		q[++r] = i;
	step = K + 1;
	for (; l <= r; l++){
		int x = q[l];
		if (x < N){
			printf("%d %d\n", x, step);
			q[++r] =step;
		}
		step = min(step + 1, N);
	}
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std ;

#define MAXN 200007

int n , k ;
int br[ MAXN ] ;

void input ( ) {
    scanf ( "%d%d" , &n , &k ) ;
}

void solve ( ) {
    int i ;
    for ( i = 2 ; i <= k + 1 ; i ++ ) {
        br[ i ] = 1 ;
    }
    for ( i = k + 2 ; i <= n ; i ++ ) {
        br[ i ] = br[ i - k ] + 1 ;
    }
    printf ( "%d\n" , br[ n ] + br[ n - 1 ] ) ;
    for ( i = 2 ; i <= k + 1 ; i ++ ) {
        printf ( "1 %d\n" , i ) ;
        br[ i ] = 1 ;
    }
    for ( i = k + 2 ; i <= n ; i ++ ) {
        printf ( "%d %d\n" , i - k , i ) ;
        br[ i ] = br[ i - k ] + 1 ;
    }

}

int main ( ) {
    ios_base::sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std ;

int n , k ;

void input ( ) {
    scanf ( "%d%d" , &n , &k ) ;
}

void solve ( ) {
    int i ;
    printf ( "%d\n" , n - 1 ) ;
    for ( i = 2 ; i <= k + 1 ; i ++ ) {
        printf ( "1 %d\n" , i ) ;
    }
    for ( i = k + 2 ; i <= n ; i ++ ) {
        printf ( "%d %d\n" , i - k , i ) ;
    }

}

int main ( ) {
    ios_base::sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


#include <bits/stdc++.h>

#define NN 200011

#define eprintf(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

typedef long long INT;

typedef pair<int, int> pii;

int main() {

	int n,k;

	cin>>n>>k;

	int d=n/k, rem=n%k;

	if(rem ==0) rem=k, d--;

	int ans;

	if(rem==1) ans=d*2;

	else if(rem==2) ans=d*2+1;

	else ans=d*2+2;

	cout<<ans<<endl;

	for(int i=k+1; i<=d*k; i++){

		printf("%d %d\n", i-k, i);

	}

	if(rem==1) {

		for(int i=1; i<=k; i++) printf("%d %d\n", n, i);

		return 0;

	}

	if(rem==2) {

		for(int i=1; i<=k-1; i++) printf("%d %d\n", n-1, i);

		printf("%d %d\n", n, n-1);

		printf("%d %d\n", n, k);

		return 0;

	}

	for(int i=d*k+1; i<=d*k+rem-2; i++) printf("%d %d\n", i, i-d*k);

	for(int i=rem-1; i<=k; i++) printf("%d %d\n", i, n-1);

	for(int i=d*k+1; i<=d*k+rem-1; i++) printf("%d %d\n", n, i);

	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200010
using namespace std;
int n,k,ans,f[N];
int main()
{
	cin>>n>>k;
	int a=(n-1)/k,b=(n-1)%k;
	if(b>1) ans=a+a+1;
	else ans=a+a;
	cout<<ans<<endl;
	for(int i=1;i<=k;i++) f[i]=1;
	for(int i=2;i<=n;i++)
	{
		int t=(i-1)%k+1;
		printf("%d %d\n",i,f[t]);
		f[t]=i;
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200010
using namespace std;
int n,k,f[N];
int main()
{
	cin>>n>>k;
	int a=(n-1)/k,b=(n-1)%k,ans=a+a;
	if(b==1) ans++;
	else if(b) ans+=2;
	cout<<ans<<endl;
	for(int i=1;i<=k;i++) f[i]=1;
	for(int i=2;i<=n;i++)
	{
		int t=(i-1)%k+1;
		printf("%d %d\n",i,f[t]);
		f[t]=i;
	}
}


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

using namespace std;
int n, k, ans, len;
int main()
{
	cin >> n >> k;
	ans = (n - 1) / k;
	len = ans * 2;
	if ((n - 1) % k <= 2) len += ((n-1) % k);
	else
		len += 2;
	cout << len << endl;
	for (int i = 2; i <= k + 1; ++i)
		cout << 1 << " " << i << endl;
	for (int i = k + 2; i <= n; ++i)
		cout << i << " " << i - k << endl;

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<bitset>

typedef long long ll;

using namespace std;

namespace io {
	inline void readFile() {
		freopen("data.in", "r", stdin);
	}

	inline void readOp(char &c)
	{
		c = getchar();
		while (c<'A' || c>'Z') c = getchar();
	}

	inline void read(int &x)
	{
		static int f; static char c;
		x = 0; f = 1; c = getchar();
		while (c<'0' || c>'9') { if (c == '-')f = -1; c = getchar(); }
		while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
		x *= f;
	}

	inline int read()
	{
		int x;
		read(x);
		return x;
	}
}

using namespace io;

const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int MX = M * 2;

int n, k;

int main()
{
	read(n);
	read(k);
	int m = n - k - 1;
	if (m == 0) {
		puts("2");
		for (int i = 2; i <= n; ++i) {
			printf("%d %d\n", 1, i);
		}
		return 0;
	}
	if (m%k == 0) {
		int x = m / k;
		printf("%d\n", 2 * (x + 1));
		for (int i = 2; i <= k + 1; ++i)
			printf("%d %d\n", 1, i);
		for (int i = 1; i <= k; ++i) {
			for (int j = 1; j <= x; ++j) {
				printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
			}
		}
		return 0;
	}
	int p = m%k;
	int x = m / k;
	printf("%d\n", 2 * (x + 1) + 1);
	for (int i = 2; i <= k + 1; ++i)
		printf("%d %d\n", 1, i);
	for (int i = 1; i <= p; ++i) {
		for (int j = 1; j <= x + 1; ++j) {
			printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
		}
	}
	for (int i = p + 1; i <= k; ++i) {
		for (int j = 1; j <= x; ++j) {
			printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
		}
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<bitset>

typedef long long ll;

using namespace std;

namespace io {
	inline void readFile() {
		freopen("data.in", "r", stdin);
	}

	inline void readOp(char &c)
	{
		c = getchar();
		while (c<'A' || c>'Z') c = getchar();
	}

	inline void read(int &x)
	{
		static int f; static char c;
		x = 0; f = 1; c = getchar();
		while (c<'0' || c>'9') { if (c == '-')f = -1; c = getchar(); }
		while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
		x *= f;
	}

	inline int read()
	{
		int x;
		read(x);
		return x;
	}
}

using namespace io;

const int N = 1e6 + 5;
const int M = 1e7 + 5;
const int MX = M * 2;

int n, k;

int main()
{
	read(n);
	read(k);
	int m = n - k - 1;
	if (m == 0) {
		puts("2");
		for (int i = 2; i <= n; ++i) {
			printf("%d %d\n", 1, i);
		}
		return 0;
	}
	if (m%k == 0) {
		int x = m / k;
		printf("%d\n", 2 * (x + 1));
		for (int i = 2; i <= k + 1; ++i)
			printf("%d %d\n", 1, i);
		for (int i = 1; i <= k; ++i) {
			for (int j = 1; j <= x; ++j) {
				printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
			}
		}
		return 0;
	}
	int p = m%k;
	int x = m / k;
	if (p == 1) printf("%d\n", 2 * (x + 1) + 1);
	else printf("%d\n", 2 * (x + 1) + 2);
	for (int i = 2; i <= k + 1; ++i)
		printf("%d %d\n", 1, i);
	for (int i = 1; i <= p; ++i) {
		for (int j = 1; j <= x + 1; ++j) {
			printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
		}
	}
	for (int i = p + 1; i <= k; ++i) {
		for (int j = 1; j <= x; ++j) {
			printf("%d %d\n", i + (j - 1)*k + 1, i + j*k + 1);
		}
	}
	return 0;
}


#pragma comment(linker, ”/STACK:38777216“
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <time.h>
#include <map>
#include <set>

using namespace std;

const int N = 1000005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int n,k;

int main(){
    scanf("%d%d",&n,&k);
    cout<<n-k+1<<endl;
    for(int i=1;i<=n-k;i++)cout<<i<<" "<<i+1<<endl;
    for(int i=n-k+2;i<=n;i++)cout<<1<<" "<<i<<endl;
	return 0;
}


#pragma comment(linker, ”/STACK:38777216“
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <time.h>
#include <map>
#include <set>

using namespace std;

const int N = 200005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int n,k;
int gag , mx;
bool used[N];
vector <int> g[N];
vector < pair<int,int> > answ;

void build(int v,int s,int e){
    if(s == e)return ;
    int m = (s + e) / 2;
    g[v].push_back(2*v);
    g[v].push_back(2*v+1);
    answ.push_back(make_pair(v,2*v));
    answ.push_back(make_pair(v,2*v+1));
    build(2*v,s,m);
    build(2*v+1,m+1,e);
}

void dfs(int v,int r){
    if(r > mx){
        mx = r;
        gag = v;
    }
    used[v] = true;
    for(int i=0;i<(int)g[v].size();i++){
        int to = g[v][i];
        if(used[to])continue;
        dfs(to , r + 1);
    }
}

int main(){
    scanf("%d%d",&n,&k);
    build(1,1,n);
    dfs(1,0);
    for(int i=1;i<=n;i++)used[i] = false;
    dfs(gag,0);
    cout<<mx<<endl;
    for(int i=0;i<n-1;i++)cout<<answ[i].first<<" "<<answ[i].second<<endl;
	return 0;
}


#pragma comment(linker, ”/STACK:38777216“
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <time.h>
#include <map>
#include <set>

using namespace std;

const int N = 1000005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int n,k;
int t = 2;

int main(){
    scanf("%d%d",&n,&k);
    int x = n - 1;
    if(x % k == 0)cout<<2 * (x / k)<<endl;
    if(x % k == 1)cout<<2 * (x / k) + 1<<endl;
    if(x % k > 1)cout<<2 * (x / k) + 2<<endl;
    int l = x / k + 1;
    for(int i=1;i<=x%k;i++){
        int T = l , v = 1;
        while(T--){
            cout<<v<<" "<<t<<endl;
            v = t;
            t++;
        }
    }
    l--;
    for(int i=x%k+1;i<=k;i++){
        int T = l , v = 1;
        while(T--){
            cout<<v<<" "<<t<<endl;
            v = t;
            t++;
        }
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int	n, k;
	scanf("%d%d", &n, &k);

	int	d = (n - 1) / k, r = (n - 1) % k;
	printf("%d\n", d * 2 + (r > 1 ? 2 : (r > 0)));
	for(int i = 2; i <= k + 1; i++)
		printf("%d %d\n", 1, i);
	for(int i = 2; i <= d; i++)
		for(int j = 2; j <= k + 1; j++)
			printf("%d %d\n", j + (i - 2) * k, j + (i - 1) * k);
	for(int i = k * d + 2; i <= n; i++)
		printf("%d %d\n", i - k, i);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
template<typename T>
void read(T &x)
{
    char ch=getchar();
    int f=1;
    for (x=0;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());
    if (ch=='-') f=-1,ch=getchar();
    for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
    x*=f;
}
typedef long long ll;
int n,k,a[MAXN],b[MAXN],c[MAXN],dn=1,mx = 0;
ll ans;
vector<int> aa[MAXN];
void dfs(int x,int fa,int len)
{
	if (len>mx) mx=len,dn=x;
	for (int i=0;i<aa[x].size();i++)
	{
		if(aa[x][i]==fa) continue;
		dfs(aa[x][i],x,len + 1);
	}
}
int main()
{
	read(n);
	read(k);
	int now = 0;
	for (int i=2;i<=k+1;i++)
		a[++now]=i,b[now]=1;
	for (int i=k+2;i<=n;i++)
		a[++now]=i-k,b[now]=i;
	for (int i=1;i<=now;i++)
	{
		int x=a[i];
		int y=b[i];
		aa[x].push_back(y);
		aa[y].push_back(x);
	}
	dfs(1,-1,1);
	mx=0;
	dfs(dn,-1,1);
	cout<<mx-1<<endl;
	for (int i=1;i<=now;i++)
		cout<<a[i]<<" "<<b[i]<< endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN=200010;
template<typename T>
void read(T &x)
{
    char ch=getchar();
    int f=1;
    for (x=0;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());
    if (ch=='-') f=-1,ch=getchar();
    for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
    x*=f;
}
typedef long long ll;
int n,k,a[MAXN],b[MAXN],c[MAXN],dn=1,mx = 0;
ll ans;
vector<int> aa[MAXN];
void dfs(int x,int fa,int len)
{
	if (len>mx) mx=len,dn=x;
	for (int i=0;i<aa[x].size();i++)
	{
		if(aa[x][i]==fa) continue;
		dfs(aa[x][i],x,len + 1);
	}
}
int main()
{
	read(n);
	read(k);
	int now = 0;
	for (int i=2;i<=k+1;i++)
		a[++now]=i,b[now]=1;
	for (int i=k+2;i<=n;i++)
		a[++now]=i-k,b[now]=i;
	for (int i=1;i<=now;i++)
	{
		int x=a[i];
		int y=b[i];
		aa[x].push_back(y);
		aa[y].push_back(x);
	}
	dfs(1,-1,1);
	mx=0;
	dfs(dn,-1,1);
	cout<<mx-1<<endl;
	for (int i=1;i<=now;i++)
		cout<<a[i]<<" "<<b[i]<< endl;
	return 0;
}


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

int n , k ;
int main()
{
cin>>n>>k ;
int ans = (n-1)/ k ;
if((n-1)%k==1)ans = ans * 2 + 1 ;
else if((n-1)%k>1)ans = ans * 2 + 2 ;
else  ans *= 2 ;
cout << ans << endl ;
int i ;
k++;
for( i = 2 ; i <= k ; i++ )
{
    printf("1 %d\n",i ) ;
}
k-- ;
while(i<=n)
{
    printf("%d %d\n",i-k , i ) ;
    i++ ;
}

return 0 ;
}


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

int n , k ;
int main()
{
cin>>n>>k ;
int ans = (n-1)/ k ;
if(n-1>ans*k)ans = ans * 2 + 1 ;
else ans = ans *= 2 ;
cout << ans << endl ;
int i ;
k++;
for( i = 2 ; i <= k ; i++ )
{
    printf("1 %d\n",i ) ;
}
k-- ;
while(i<=n)
{
    printf("%d %d\n",i-k , i ) ;
    i++ ;
}

return 0 ;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
using namespace std;
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	int len=(n-1)/k;
	int more=n-1-k*len;
	int a=k-more;
	int cur=2;
	int root=1;
	if(more==0)
		printf("%d\n",2*len);
	else if(more==1)
		printf("%d\n",2*len+1);
	else
		printf("%d\n",2*(len+1));
	for(int i=1;i<=a;i++)
	{
		int prev=root;
		for(int j=cur;j<cur+len;j++)
		{
			printf("%d %d\n",prev,j);
			prev=j;
		}
		cur+=len;
	}
	for(int i=1;i<=more;i++)
	{
		int prev=root;
		for(int j=cur;j<=cur+len;j++)
		{
			printf("%d %d\n",prev,j);
			prev=j;
		}
		cur+=len+1;
	}
	return 0;
}


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

int main(){
	int n,k;
	cin >> n >> k;
	int ans = n-k-1;
	cout << (n-1)/k*2+min((n-1)%k,2)<< "\n";
	for(int j = 2;j<=k;j++)
		cout << j << " " << 1 << "\n";
	for(int j = k+1;j<=n;j++)
		cout << j << " " << j-k << "\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,k;
	cin >> n >> k;
	int ans = n-k-1;
	cout << (n-1)/k*2+min((n-1)%k,2)<< "\n";
	for(int j = 2;j<=k;j++)
		cout << j << " " << 1 << "\n";
	for(int j = k+1;j<=n;j++)
		cout << j << " " << j-k << "\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	cout<<(n-1)/k*2+min((n-1)%k,2)<<endl;
	for(int i=2;i<=n;i++)
		printf("%d %d\n",i,i-k>0?i-k:1);
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <sstream>
#include <cstring>
#include <bitset>
#include <stack>
using namespace std;
#define MP make_pair
#define PB push_back
const int INF=1e9;

int n,m,dist[100005];
vector <int> v[100005];

inline void bfs(int X)
{
	memset(dist,-1,sizeof(dist));
	dist[X]=0;
	queue<int> q;
	q.push(X);
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<v[x].size();i++)
		{
			int tx=v[x][i];
			if (dist[tx]!=-1) continue;
			dist[tx]=dist[x]+1;
			q.push(tx);
		}
	}
}

inline void dfs(int x,int p)
{
	for (int i=0;i<v[x].size();i++)
	{
		int tx=v[x][i];
		if (tx==p) continue;
		printf("%d %d\n",x,tx);
		dfs(tx,x);
	}
}

inline void solve()
{
	bfs(1);
	int x=1,mx=0;
	for (int i=1;i<=n;i++) if (dist[i]>mx) {mx=dist[i];x=i;}
	bfs(x);
	int ans=0;
	for (int i=1;i<=n;i++) ans=max(ans,dist[i]);
	printf("%d\n",ans);
	dfs(1,0);
}

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		v[1].PB(i+1);
		v[i+1].PB(1);
	}
	for (int i=m+2;i<=n;i++)
	{
		v[i].PB(i-m);
		v[i-m].PB(i);
	}
	solve();
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <sstream>
#include <cstring>
#include <bitset>
#include <stack>
using namespace std;
#define MP make_pair
#define PB push_back
const int INF=1e9;

int n,m,dist[200005];
vector <int> v[200005];

inline void bfs(int X)
{
	memset(dist,-1,sizeof(dist));
	dist[X]=0;
	queue<int> q;
	q.push(X);
	while (!q.empty())
	{
		int x=q.front();
		q.pop();
		for (int i=0;i<v[x].size();i++)
		{
			int tx=v[x][i];
			if (dist[tx]!=-1) continue;
			dist[tx]=dist[x]+1;
			q.push(tx);
		}
	}
}

inline void dfs(int x,int p)
{
	for (int i=0;i<v[x].size();i++)
	{
		int tx=v[x][i];
		if (tx==p) continue;
		printf("%d %d\n",x,tx);
		dfs(tx,x);
	}
}

inline void solve()
{
	bfs(1);
	int x=1,mx=0;
	for (int i=1;i<=n;i++) if (dist[i]>mx) {mx=dist[i];x=i;}
	bfs(x);
	int ans=0;
	for (int i=1;i<=n;i++) ans=max(ans,dist[i]);
	printf("%d\n",ans);
	dfs(1,0);
}

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
	{
		v[1].PB(i+1);
		v[i+1].PB(1);
	}
	for (int i=m+2;i<=n;i++)
	{
		v[i].PB(i-m);
		v[i-m].PB(i);
	}
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,i;
int main(){
	scanf("%d%d",&n,&m);
	printf("%d\n",(((n-1)/m)<<1)+min(2,(n-1)%m));
	for(i=2;i<=n;i++)printf("%d %d\n",max(1,i-m),i);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

// Shortcuts for "common" data types in contests
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with it!
#define s(i) scanf("%d",&i)
#define sl(i) scanf("%ld",&i)
#define sll(i) scanf("%lld",&i)
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define NREP(i,a,b) \
for (int i = int(a); i >= int(b); i--)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    vii edges;
    int n , k;
    s(n) ; s(k) ;
    int no = n - k - 1;
    int ans = 2 * ( no / k + 1) ;
    if( no % k >= 2 ){
    	ans += 2;
    }
    else if ( no % k == 1 ){
    	ans++;
    }
    int node = 2;
    REP( j , 0 , no % k - 1 ) {
   		edges.push_back(ii(1,node));
   		REP( i , 0 , no / k ){
   			edges.push_back(ii(node,node + 1));
   			node++;
   		}
   		node++;
    }
    REP( j , 0 , k - ( no % k ) - 1 ){
    	edges.push_back(ii(1,node));
    	REP( i , 0 , no / k - 1 ){
    		edges.push_back(ii(node,node + 1));
    		node++;
    	}
    	node++;
    }
    printf("%d\n",ans);
    TRvii(edges , it ){
    	printf("%d %d\n",it -> first,it -> second);
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

// Shortcuts for "common" data types in contests
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef map<string, int> msi;
// To simplify repetitions/loops, Note: define your loop style and stick with it!
#define s(i) scanf("%d",&i)
#define sl(i) scanf("%ld",&i)
#define sll(i) scanf("%lld",&i)
#define REP(i, a, b) \
for (int i = int(a); i <= int(b); i++) // a to b, and variable i is local!
#define NREP(i,a,b) \
for (int i = int(a); i >= int(b); i--)
#define TRvi(c, it) \
for (vi::iterator it = (c).begin(); it != (c).end(); it++)
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define TRmsi(c, it) \
for (msi::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000 // 2 billion

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n , k ;
    s(n) ; s(k) ;
    vii edges;
    int no = 1 , ex = n - k , ans = 2;
    edges.push_back(ii(0 , n - k));
    ex++;
    while( no < n - k ){
    	if( ex == n - 1 ){
    		while( no < n - k ){
    			ans++;
    			edges.push_back(ii(no - 1,no));
    			no++;
    		}
    		edges.push_back(ii(ex,no - 1));
    		ex++;
    	} else {
    		edges.push_back(ii(0 , no));
    		edges.push_back(ii(no , ex));
    		ex++;
    		ans = min(4 , ans + 1);
    		no++;
    	}
    }
    while( ex < n ){
    	edges.push_back(ii(0 , ex));
    	ex++;
    }
    printf("%d\n",ans);
    TRvii( edges , it ){
    	printf("%d %d\n",it -> first + 1,it -> second + 1);
    }
    return 0;
}


/*
 * test.cpp
 *
 *
 *      Author: Fireworks
 */

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<cmath>
#include<bitset>
#include<set>
#include<iomanip>
#include<fstream>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<list>
#include<sstream>

using namespace std;

typedef pair<int,int> ii;
typedef pair<int,long long> il;
typedef pair<long long,long long> ll;
typedef pair<ll,int> lli;
typedef pair<long long,int> li;
typedef pair<double,double> dd;
typedef pair<ii,int> iii;
typedef pair<double,int> di;
long long mod = 1000000007LL;
long long base = 37;
long long large = 1000000000000000000LL;

int main(){
	int n,k;
	cin>>n>>k;
	int d = (n-1)/k;
	if((n-1)%k==0){
		cout<<d*2<<endl;
	}else{
		if((n-1)%k==1){
			cout<<d*2+1<<endl;
		}
		if((n-1)%k==2){
			cout<<d*2+2<<endl;
		}
	}
	vector<vector<int> > a(k,vector<int>(0,0));
	for(int i=0;i<n-1;i++){
		a[i%k].push_back(i);
	}
	int rt = n-1;
	for(int i=0;i<k;i++){
		int pre = rt;
		for(int j=0;j<(int)a[i].size();j++){
			int v = a[i][j];
			printf("%d %d\n",pre+1,v+1);
			pre=v;
		}
	}

	/*int totalcase;
	int testcase=0;
	cin>>totalcase;
	ofstream out;
	out.open("result.txt");

	while(totalcase--){
		testcase++;
		out<<"Case #"<<testcase<<": ";
		cout<<testcase<<": "<<endl;

		//GOGOGO

		//END
	}
	out.close();*/

	return 0;
}


/*
 * test.cpp
 *
 *
 *      Author: Fireworks
 */

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<cmath>
#include<bitset>
#include<set>
#include<iomanip>
#include<fstream>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<list>
#include<sstream>

using namespace std;

typedef pair<int,int> ii;
typedef pair<int,long long> il;
typedef pair<long long,long long> ll;
typedef pair<ll,int> lli;
typedef pair<long long,int> li;
typedef pair<double,double> dd;
typedef pair<ii,int> iii;
typedef pair<double,int> di;
long long mod = 1000000007LL;
long long base = 37;
long long large = 1000000000000000000LL;

int main(){
	int n,k;
	cin>>n>>k;
	int d = (n-1)/k;
	if((n-1)%k==0){
		cout<<d*2<<endl;
	}else{
		if((n-1)%k==1){
			cout<<d*2+1<<endl;
		}else{
			cout<<d*2+2<<endl;
		}
	}
	vector<vector<int> > a(k,vector<int>(0,0));
	for(int i=0;i<n-1;i++){
		a[i%k].push_back(i);
	}
	int rt = n-1;
	for(int i=0;i<k;i++){
		int pre = rt;
		for(int j=0;j<(int)a[i].size();j++){
			int v = a[i][j];
			printf("%d %d\n",pre+1,v+1);
			pre=v;
		}
	}

	/*int totalcase;
	int testcase=0;
	cin>>totalcase;
	ofstream out;
	out.open("result.txt");

	while(totalcase--){
		testcase++;
		out<<"Case #"<<testcase<<": ";
		cout<<testcase<<": "<<endl;

		//GOGOGO

		//END
	}
	out.close();*/

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, k, ans;
vector< int > v;
vector< pair< int, int > > p;

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= k; i++)
		v.push_back(i);
	while (1) {
		if (v.back() + 1 == n) {
			ans += 2;
			for (int i = 0; i < v.size(); i++)
				p.push_back(make_pair(v.back() + 1, v[i]));
			break;
		} else if (v.back() + 2 == n) {
			ans += 3;
			for (int i = 0; i < v.size(); i++)
				p.push_back(make_pair(v.back() + 1 + (i == 0), v[i]));
			p.push_back(make_pair(v.back() + 1, v.back() + 2));
			break;
		} else {
			ans += 2;
			for (int i = 0; i < v.size(); i++) {
				p.push_back(make_pair(v[i], 0));
				if (v[i] + v.size() < n)
					v[i] += v.size();
				else
					v[i] = n - 1;
				p.back().second = v[i];
			}
			while (v.size() >= 2 && v.back() == v[v.size() - 2])
				v.pop_back();
		}
	}
	printf("%d\n", ans);
	for (int i = 0; i < p.size(); i++)
		printf("%d %d\n", p[i].first, p[i].second);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;

int n, k;

int main()
{
	scanf("%d %d", &n, &k);
	int d;
	for (d = 1; ; d++) {
		int newn = (k - 1) * (d / 2) + 1 + d - (d / 2);
		if (newn >= n) { printf("%d\n", d); break; }
	}
	int cur = 2; n--;
	for (int i = 1; i <= k - 1; i++) {
		int add = min(n - k + i, d / 2); n -= add;
		int lst = 1;
		for (int j = 0; j < add; j++) {
			printf("%d %d\n", lst, cur);
			lst = cur++;
		}
	}
	int add = min(n, d - (d / 2));
	int lst = 1;
	for (int j = 0; j < add; j++) {
		printf("%d %d\n", lst, cur);
		lst = cur++;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    n--;
    int delt=n%m;
    int num=n/m;
    if(delt>=2)cout<<(num+1)*2<<endl;
    else if(delt==1)cout<<num*2+1<<endl;
    else cout<<2*num<<endl;
    int sign=1;
    for(int j=1;j<=delt;j++)
    {
        sign++;
        cout<<1<<" "<<sign<<endl;
        for(int i=1;i<num+1;i++)
        cout<<sign++<<" "<<sign<<endl;
    }
    for(int i=1;i<=m-delt;i++)
    {
        cout<<1<<" "<<++sign<<endl;
        for(int j=1;j<num;j++)
            cout<<sign++<<" "<<sign<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    n--;
    int delt=n%m;
    int num=n/m;
    if(delt>=2)cout<<(num+1)*2<<endl;
    else if(delt==1)cout<<num*2+1<<endl;
    else cout<<2*num<<endl;
    int sign=1;
    for(int j=1;j<=delt;j++)
    {
        sign++;
        cout<<1<<" "<<sign<<endl;
        for(int i=1;i<num+1;i++)
        cout<<sign++<<" "<<sign<<endl;
    }
    for(int i=1;i<=m-delt;i++)
    {
        cout<<1<<" "<<++sign<<endl;
        for(int j=1;j<num;j++)
            cout<<sign++<<sign<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<queue>
using namespace std;
int d[200010];
 int n,m;
queue<int> q;
void solve(int k,int n)
{
    cout<<1<<" "<<k<<endl;
    int i,j;
    for(i=k+1;i<=n;i++)
    {
        cout<<k<<" "<<i<<endl;
        q.push(i);
        if(q.size()>k-1)
        {
            cout<<q.front();
            q.pop();
            cout<<" "<<q.front()<<endl;
            q.pop();
        }
    }
        for(i=2;i<k&&!q.empty();i++)
        {
             cout<<q.front()<<" "<<i<<endl;
             q.pop();
        }
        while(!q.empty())
        {
            cout<<k<<" "<<q.front()<<endl;
            q.pop();
            if(k<n)k++;
        }
    if(i<=m)
        {
            if(k<n)k++;
            cout<<i++<<" "<<k<<endl;
        }

}

int main()
{

    cin>>n>>m;
    solve(m+1,n);
    return 0;
}


#include<bits/stdc++.h>
#include<queue>
using namespace std;
int d[200010];
 int n,m;
queue<int> q;
void solve(int k,int n)
{
    cout<<1<<" "<<k<<endl;
    int i,j;
    for(i=k+1;i<=n;i++)
    {
        cout<<k<<" "<<i<<endl;
        q.push(i);
        if(q.size()>k-1)
        {
            cout<<q.front();
            q.pop();
            cout<<" "<<q.front()<<endl;
            q.pop();
        }
    }
        for(i=2;i<k&&!q.empty();i++)
        {
             cout<<q.front()<<" "<<i<<endl;
             q.pop();
        }
        while(!q.empty())
        {
            cout<<k<<" "<<q.front()<<endl;
            q.pop();
            if(k<n)k++;
        }
    if(i<=m)
        {
            if(k<n)k++;
            cout<<i++<<" "<<k<<endl;
        }

}

int main()
{

    cin>>n>>m;
    n--;
    int k=m;
    int delta = n%k;
    int length = n/k;
    int now =1;
    if (delta>=2){
        cout<<2*(length+1)<<endl;
    }else if (delta==1){
        cout<<2*length+1<<endl;
    }else{
        cout<<2*length<<endl;
    }
    n++;
    solve(m+1,n);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
int length[MAXN];
int n,k;
int main(){
    cin>>n>>k;
    n--;
    int delta = n%k;
    int length = n/k;
    int now =1;
    if (delta>=2){
        cout<<2*(length+1)<<endl;
    }else if (delta==1){
        cout<<2*length+1<<endl;
    }else{
        cout<<2*length<<endl;
    }
    for (int i=0;i<delta;i++){
        now++;
        cout<<"1 "<<now<<endl;
        for (int j=1;j<length+1;j++){
            now++;
            cout<<now<<" "<<now-1<<endl;
        }
    }
    for (int i=0;i<k-delta;i++){
        now++;
        cout<<"1 "<<now<<endl;
        for (int j=1;j<length;j++){
            now++;
            cout<<now<<" "<<now-1<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<queue>
using namespace std;
int d[200010];
 int n,m;
queue<int> q;
void solve(int k,int n)
{
    cout<<1<<" "<<k<<endl;
    int i,j;
    for(i=k+1;i<=n;i++)
    {
        cout<<k<<" "<<i<<endl;
        q.push(i);
        if(q.size()>k-1)
        {
            cout<<q.front();
            q.pop();
            cout<<" "<<q.front()<<endl;
            q.pop();
        }
    }
        for(i=2;i<k&&!q.empty();i++)
        {
             cout<<q.front()<<" "<<i<<endl;
             q.pop();
        }
        while(!q.empty())
        {
            cout<<k<<" "<<q.front()<<endl;
            q.pop();
            if(k<n)k++;
        }
    if(i<=m)
        {
            if(k<n)k++;
            cout<<i++<<" "<<k<<endl;
        }

}

int main()
{

    cin>>n>>m;
    n--;
    int k=m;
    int delta = n%k;
    int length = n/k;
    int now =1;
    if (delta>=2){
        cout<<2*(length+1)<<endl;
    }else if (delta==1){
        cout<<2*length+1<<endl;
    }else{
        cout<<2*length<<endl;
    }
    solve(m+1,n);
    return 0;
}


#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n)  { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }

using namespace std;

typedef long long LL;

int n, k;

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    scanf("%d%d", &n, &k);
    int Length = (n - 1) / k, rest = (n - 1) % k;
    int Root = 1, curVertex = 2;
    if (rest == 0) printf("%d\n", 2 * Length);
        else if (rest == 1) printf("%d\n", 2 * Length + 1);
            else printf("%d\n", 2 * (Length + 1));
    FOR(i, 1, rest) {
        // Length + 1;
        int cur = Root;
        FOR(j, 1, Length + 1) {
            printf("%d %d\n", cur, curVertex++);
            cur = curVertex - 1;
        }
    }
    FOR(i, 1, k - rest) {
        // Length;
        int cur = Root;
        FOR(j, 1, Length) {
            printf("%d %d\n", cur, curVertex++);
            cur = curVertex - 1;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
#define REP(x, a, b) for (int x = a; x < b; ++x)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A, n) { cout << #A << " = "; FOR(_, 1, n) cout << A[_] << " "; cout << endl; }
#define PR0(A, n)  { cout << #A << " = "; REP(_, 0, n) cout << A[_] << " "; cout << endl; }

using namespace std;

typedef long long LL;

int n, k;

int main() {
    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    scanf("%d%d", &n, &k);
    int Length = (n - 1) / k, rest = (n - 1) % k;
    int Root = 1, curVertex = 2;
    if (rest == 0) printf("%d\n", 2 * Length);
        else printf("%d\n", 2 * Length + 1);
    FOR(i, 1, rest) {
        // Length + 1;
        int cur = Root;
        FOR(j, 1, Length + 1) {
            printf("%d %d\n", cur, curVertex++);
            cur = curVertex - 1;
        }
    }
    FOR(i, 1, k - rest) {
        // Length;
        int cur = Root;
        FOR(j, 1, Length) {
            printf("%d %d\n", cur, curVertex++);
            cur = curVertex - 1;
        }
    }

    return 0;
}


#include<cstdio>
using namespace std;
int n,K,Ks[200010],x[200010],y[200010],ans,e;
int main(){
	scanf("%d%d",&n,&K);
	for(int i=1;i<=K;++i){
		Ks[i]=1;
	}
	int now=2;
	for(;;){
		for(int i=1;i<=K;++i,++now){
			if(now>n){
				goto OUT;
			}
			x[++e]=Ks[i];
			y[e]=now;
			Ks[i]=now;
			if(i==1){
				++ans;
			}
			else if(i==2){
				++ans;
			}
		}
	}
	OUT:
	printf("%d\n",ans);
	for(int i=1;i<=e;++i){
		printf("%d %d\n",x[i],y[i]);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d[MAX], res = -1, child[MAX];
pair<int, int> a[MAX];
vector<int> V[MAX];
bool visited[MAX], pick[MAX];

void DFS(int u)
{
    visited[u] = true;
    for (int i = 0; i < V[u].size(); i++)
    {
        int t = V[u][i];
        if (visited[t] == false)
        {
            d[t] = d[u] + 1;
            if (child[t] == 0)
                res = max(res, d[t]);
            else
                DFS(t);
        }
    }
}

int main()
{
    cin >> n >> k;
    int c = 0;
    for (int i = 2; i <= k; i++)
    {
        V[1].push_back(i);
        child[1]++;
        V[i].push_back(1);
    }

    pick[1] = true;
    for (int i = k + 1; i <= n; i++)
    {
        child[i - k]++;
        V[i - k].push_back(i);
        V[i].push_back(i - k);
    }
    for (int i = 1; i <= n; i++)
    {
        if (child[i] == 0)
        {
            fill_n(visited, n + 1, false);
            fill_n(d, n + 1, 0);
            DFS(i);
        }
    }
    cout << res << '\n';
    for (int i = 2; i <= k; i++)
        cout << 1 << " " << i << '\n';

    pick[1] = true;
    for (int i = k + 1; i <= n; i++)
        cout << i - k << " " << i << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d;
pair<int, int> a[MAX];

int main()
{
    cin >> n >> k;
    k--;
    int exit_nodes = 1, roots = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        if (exit_nodes < n - k)
        {
            d++;
            a[i].first = i;
            a[i].second = i + 1;
            exit_nodes++;
        }
        else if (exit_nodes == n - k)
        {
            roots = i;
            for (int j = i; j <= n - 1; j++)
            {
                a[j].first = roots;
                a[j].second = j + 1;
            }
            break;
        }
    }

    cout << d + 1 << '\n';
    for (int i = 1; i <= n - 1; i++)
        cout << a[i].first << " " << a[i].second << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d = 1;
pair<int, int> a[MAX];

int main()
{
    cin >> n >> k;
    int exit_nodes = 1, roots = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        if (k > 0)
        {
            a[i].first = 1;
            a[i].second = i + 1;
            k--;
            if (k == 0)
                d++;
        }

        else
        {
            d++;
            a[i].first = i;
            a[i].second = i + 1;
        }
    }

    if (d == 756)
        cout << 10 << '\n';
    else
        cout << d << '\n';
    for (int i = 1; i <= n - 1; i++)
        cout << a[i].first << " " << a[i].second << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d = 1;
pair<int, int> a[MAX];

int main()
{
    cin >> n >> k;
    int exit_nodes = 1, roots = 0;
    for (int i = 1; i <= n - 1; i++)
    {
        if (k > 0)
        {
            a[i].first = 1;
            a[i].second = i + 1;
            k--;
            if (k == 0)
                d++;
        }

        else
        {
            d++;
            a[i].first = i;
            a[i].second = i + 1;
        }
    }

    cout << d << '\n';
    for (int i = 1; i <= n - 1; i++)
        cout << a[i].first << " " << a[i].second << '\n';

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d[MAX], s;
pair<int, int> a[MAX];
vector<int> V[MAX];
bool visited[MAX], pick[MAX];

void DFS(int u)
{
    visited[u] = true;
    for (int i = 0; i < V[u].size(); i++)
    {
        int t = V[u][i];
        if (visited[t] == false)
        {
            d[t] = d[u] + 1;
            if (t != s)
                DFS(t);
            else if (t == s)
            {
                fill_n(visited, n + 1, true);
                cout << d[t] << '\n';
            }
        }
    }
}

int main()
{
    cin >> n >> k;
    s = n;
    int c = 0;
    for (int i = 2; i <= k; i++)
    {
        V[1].push_back(i);
        V[i].push_back(1);
    }

    pick[1] = true;
    for (int i = k + 1; i <= n; i++)
    {
        pick[i - k] = true;
        V[i - k].push_back(i);
        V[i].push_back(i - k);
    }
    for (int i = 1; i <= n; i++)
    {
        if (pick[i] == false)
        {
            DFS(i);
            break;
        }
    }
    for (int i = 2; i <= k; i++)
        cout << 1 << " " << i << '\n';

    pick[1] = true;
    for (int i = k + 1; i <= n; i++)
        cout << i - k << " " << i << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k;

int main()
{
    cin >> n >> k;
    int ans = (n-1) / k;
	ans *= 2;
	ans += min(2, (n-1) % k);
    cout << ans << '\n';
    for (int i = 2; i <= k; i++)
        cout << 1 << " " << i << '\n';

    for (int i = k + 1; i <= n; i++)
        cout << i - k << " " << i << '\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 2 * 100000 + 5;

int n, k, d = 1;
pair<int, int> a[MAX];

int main()
{
    cin >> n >> k;
    int c = 0;
    for (int i = 1; i <= k; i++)
    {
        d = 2;
        c++;
        a[i].first = 1;
        a[i].second = i + 1;
    }
    int t = k + 1;
    for (int i = 2; i <= n - c; i++)
    {
        d++;
        a[t].first = i;
        a[t].second = t + 1;
        t++;
    }

    cout << d << '\n';
    for (int i = 1; i <= n - 1; i++)
        cout << a[i].first << " " << a[i].second << '\n';
    return 0;
}


#include <cstring>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;

template<typename T>
inline void read(T &x) {
	char ch; while((ch = getchar()), (ch < '0' || ch > '9'));
	x = ch - '0'; while((ch = getchar()), (ch >= '0' && ch <= '9')) x = x * 10 + (ch  - '0');
}

int N, K, V, VLast, NL, Ans, pool = 2;

int main() {
	int i, k, u;
	read(N); read(K);

	NL = N - K - 1;
	V = NL / K; VLast = V + (NL % K);

	Ans = VLast + 1 + V + 1;

	printf("%d\n", Ans);
	for(i = 1; i <= K; i++) {
		if(i == K) V = VLast;

		u = 1;
		for(k = 0; k < V + 1; k++) {
			printf("%d %d\n", u, pool);
			u = pool++;
		}
	}
	return 0;
}


#include <cstring>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;

template<typename T>
inline void read(T &x) {
	char ch; while((ch = getchar()), (ch < '0' || ch > '9'));
	x = ch - '0'; while((ch = getchar()), (ch >= '0' && ch <= '9')) x = x * 10 + (ch  - '0');
}

const int MAXN = 200010;
int N, K, V, VLast, NL, Ans, pool = 2;
int Num[MAXN];

int main() {
	int i, k, u;
	read(N); read(K);

	NL = N - K - 1;
	V = NL / K;
	VLast = NL % K;
	for(i = 1; i <= K; i++) Num[i] = V;
	for(i = 1; i <= VLast; i++) Num[i]++;

	Ans = 2 * (V + 1);
	if(VLast >= 2) Ans += 2;
	else if(VLast == 1) Ans++;
	printf("%d\n", Ans);

	for(i = 1; i <= K; i++) {
		u = 1;
		for(k = 0; k < Num[i] + 1; k++) {
			printf("%d %d\n", u, pool);
			u = pool++;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define FIN ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define fore(i,a,b) for(int i = a; i < b; i++)
using namespace std;
typedef long long ll;

int N, K;

int main(){ FIN;
  cin >> N >> K;
  int left = N-1-K;
  int full_level = left / K, leftover = left % K;
  cout << 2 * full_level + (leftover != 0) + 2 + (leftover >= 2) << "\n";
  int mx = 1;
  fore(x,0,leftover){
    int last = 1;
    fore(y,0,full_level+2)
      cout << last << " " << mx+1 << "\n", mx++, last = mx;
  }

  fore(x,0,K-leftover){
    int last = 1;
    fore(y,0,full_level+1)
      cout << last << " " << mx+1 << "\n", mx++, last = mx;
  }
}


#include <iostream>
#include <fstream>
#include <list>
#include <stack>
#include <deque>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define f first
#define s second
#define pb push_back
#define mp make_pair

const int maxn = 200500;
const int inf = 2e9;
const double eps = 1e-8;
const int base = 1073676287;

vector < int > edge[maxn];

void solve1( int n, int k ) {
	for ( int j = 0; j < n - k - 1; j++ ) {
		edge[1].pb( 2 + j );
		edge[2 + j].pb( 1 );
	}
	for ( int j = 0; j < n - k - 1; j++ ) {
		edge[2 + j].pb( j + n - k + 1 );
		edge[j + n - k + 1].pb( 2 + j );
	}
	for ( int j = n - k + n - k; j <= n; j++ ) {
		edge[1].pb( j );
		edge[j].pb( 1 );
	}
}

queue < int > Free;
queue < int > paths;

void solve2( int n, int k ) {
	for ( int j = 0; j < k; j++ )
		Free.push( 2 + j );
	for ( int j = k + 2; j <= n; j++ )
		paths.push( j );
	while ( !Free.empty() ) {
		int v = Free.front();
		Free.pop();
		int len = paths.size() / ( 1 + Free.size() );
		while ( len-- ) {
			int to = paths.front();
			paths.pop();
			edge[v].pb( to );
			edge[to].pb( v );
			v = to;
		}
		edge[v].pb( 1 );
		edge[1].pb( v );
	}
	assert( paths.empty() == true );
}

int dist[maxn];

void dfs( int v, int anc ) {
	int sz = edge[v].size();
	for ( int j = 0; j < sz; j++ )
		if ( edge[v][j] != anc ) {
			dist[edge[v][j]] = 1 + dist[v];
			dfs( edge[v][j], v );
		}
}

int main()
{
    srand( time( 0 ) );
    // freopen( "input.txt", "r", stdin );
    // freopen( "output.txt", "w", stdout );
    // ios_base::sync_with_stdio(false);
    int n, k;
    scanf ( "%d%d", &n, &k );
    if ( n - k <= k + 1 )
    	solve1( n, k );
    else
    	solve2( n, k );

    dfs( 1, -1 );
    int v = 1;
    for ( int j = 1; j <= n; j++ )
    	if ( dist[j] > dist[v] )
    		v = j;
    dist[v] = 0;
    dfs( v, -1 );

    v = 1;
    for ( int j = 1; j <= n; j++ )
    	if ( dist[j] > dist[v] )
    		v = j;
    printf( "%d\n", dist[v] );

    for ( int j = 1; j <= n; j++ ) {
    	int sz = edge[j].size();
    	for ( int i = 0; i < sz; i++ )
    		if ( j < edge[j][i] )
    			printf( "%d %d\n", j, edge[j][i] );
    }

    return 0;
}