#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define debug(n1) cout << n1 << endl;
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

using namespace std;

const long long MAXVAL = (long long) 1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;

long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}

vector<int> pi;
vector<int> we;

int dsu_rep(int x) {
  if (pi[x] == x) return x;
  int res = dsu_rep(pi[x]);
  pi[x] = res;
  return res;
}

void dsu_union(int x, int y) {
  x = dsu_rep(x);
  y = dsu_rep(y);
  if (x == y) return;
  if (we[x] > we[y]) swap(x,y);
  pi[y] = x;
  if (we[x] == we[y]) ++we[x];
  else we[x] += we[y];
}

int bitcnt(int x) {
  int res = 0;
  while (x > 0) {
    if (x & 1) res++;
    x >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int Q = 1;
  //cin >> Q;

  while (Q-- >0) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    vector<vector<int>> g(n);
    int curind = 1;
    int curleaf = 0;
    vector<int> lastv(k, 0);
    while (curind < n) {
      int last = lastv[curleaf];
      int newv = curind;
      g[newv].push_back(last);
      lastv[curleaf] = newv;
      curleaf = (curleaf + 1) % k;
      ++curind;
    }
    int ans;
    int mm = (n - 1) % k;
    if (mm >= 2) {
      ans = 2 + 2 * ((n - 1) / k);
    } else if (mm >= 1) {
      ans = 1 + 2 * ((n - 1) / k);
    } else {
      ans = 2 * ((n - 1) / k);
    }
    cout << ans << endl;
    for (int i = 0; i < n; ++i) {
      for (int j : g[i]) {
        cout << (i + 1) << " " << j + 1 << "\n";
      }
    }
  }

  return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define  F  first
#define  S  second
#define  pb push_back
#define  mp make_pair
typedef long long int ll;
const int N=1e6+2,inf=1e9+7;
void _run();
void _exit(){cout<< "Impossible\n",exit(0);}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    _run();
    return 0;
}
void _run()
{
    int n,k;cin>>n>>k;
    int rm=n-k;
    if(rm==1)
    {
        cout<< "2\n";
        for(int i=1;i<n;i++)
            cout<< "1 "<<i+1<< "\n";
    }
    else if(rm==2)
    {
        cout<< "3\n";
        cout<< "1 2\n";
        cout<< "1 3\n";
        for(int i=1;i<=n-3;i++)
            cout<< "2 "<<i+3<< "\n";
    }
    else
    {
        int d=(rm-1)/k,r=(rm-1)%k;
        int dis=d+d+2;
        if(r==1)dis++;
        else if(r==2)dis+=2;
        cout<<dis<< "\n";
        int a=k-r,b=r,i=2;
        vector<int>lp;
        while(b--)
        {
            int cr=d+1,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        while(a--)
        {
            int cr=d,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        for(;i<=n;i++)
            cout<<i<< " "<<lp.back()<< "\n",lp.pop_back();
    }
}


#include "bits/stdc++.h"
using namespace std;
#define  F  first
#define  S  second
#define  pb push_back
#define  mp make_pair
typedef long long int ll;
const int N=1e6+2,inf=1e9+7;
void _run();
void _exit(){cout<< "Impossible\n",exit(0);}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    _run();
    return 0;
}
void _run()
{
    int n,k;cin>>n>>k;
    int rm=n-k;
    if(rm==1)
    {
        cout<< "2\n";
        for(int i=1;i<n;i++)
            cout<< "1 "<<i+1<< "\n";
    }
    else if(rm==2)
    {
        cout<< "3\n";
        cout<< "1 2\n";
        cout<< "1 3\n";
        for(int i=1;i<=n-3;i++)
            cout<< "2 "<<i+3<< "\n";
    }
    else
    {
        int d=(rm-1)/k,r=(rm-1)%k;
        int dis=d+d+2;
        if(r==1)dis++;
        else if(r==2)dis+=2;
        cout<<dis<< "\n";
        int a=k-r,b=r,i=2;
        vector<int>lp;
        while(b--)
        {
            int cr=d+1,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        while(a--)
        {
            int cr=d,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        int lst=0;
        for(;i<=n;i++)
        {
            if(lp.size())cout<<i<< " "<<lp.back()<< "\n",lst=lp.back(),lp.pop_back();
            else cout<<i<< " "<<lst<< "\n";
        }
    }
}


#include "bits/stdc++.h"
using namespace std;
#define  F  first
#define  S  second
#define  pb push_back
#define  mp make_pair
typedef long long int ll;
const int N=1e6+2,inf=1e9+7;
void _run();
void _exit(){cout<< "Impossible\n",exit(0);}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    _run();
    return 0;
}
void _run()
{
    int n,k;cin>>n>>k;
    int rm=n-k;
    if(rm==1)
    {
        cout<< "2\n";
        for(int i=1;i<n;i++)
            cout<< "1 "<<i+1<< "\n";
    }
    else if(rm==2)
    {
        cout<< "3\n";
        cout<< "1 2\n";
        cout<< "1 3\n";
        for(int i=1;i<=n-3;i++)
            cout<< "2 "<<i+3<< "\n";
    }
    else
    {
        int d=(rm-1)/k,r=(rm-1)%k;
        int dis=d+d+2;
        if(r==1)dis++;
        else if(r>=2)dis+=2;
        cout<<dis<< "\n";
        int a=k-r,b=r,i=2;
        vector<int>lp;
        while(b--)
        {
            int cr=d+1,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        while(a--)
        {
            int cr=d,prv=1,lst=0;
            for(;i<=rm&&cr--;i++)
                cout<<prv<< " "<<i<< "\n",prv=i,lst=i;
            if(lst)lp.pb(lst);
        }
        int lst=0;
        for(;i<=n;i++)
        {
            if(lp.size())cout<<i<< " "<<lp.back()<< "\n",lst=lp.back(),lp.pop_back();
            else cout<<i<< " "<<lst<< "\n";
        }
    }
}


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

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll n,k,i,j,l,x,y;

    cin>>n>>k;

    ll tmp=(n-1)/k;

    ll rem=(n-1)%k;

    if(rem==0)
    {
        cout<<2*tmp<<endl;
    }
    else
      {
        if(rem==1)
          cout<<2*tmp+1<<endl;
        else
        {
            cout<<2*tmp+2<<endl;
        }
      }

    vector<ll> v[k+2];

    for(i=0;i<k;i++)
    {
        v[i].pb(1);
    }

    j=0;

    for(i=2;i<=n;i++)
    {
        v[j].pb(i);
        j++;
        j%=k;
    }

    for(i=0;i<k;i++)
    {
        for(j=1;j<v[i].size();j++)
        {
            cout<<v[i][j]<<" "<<v[i][j-1]<<endl;
        }
    }

}


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

typedef long long ll;
#define pb push_back
#define F first
#define S second
#define ppp pair<ll,pair<ll,ll>>
#define pp pair<ll,ll>
#define all(x) (x).begin(),(x).end()
#define pi 3.14159265358

int main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

     ios_base::sync_with_stdio(false);
     cin.tie(NULL);

    ll n,k,i,j,l,x,y;

    cin>>n>>k;

    ll tmp=(n-1)/k;

    ll rem=(n-1)%k;

    if(rem==0)
    {
        cout<<2*tmp<<endl;
    }
    else
        cout<<2*tmp+1<<endl;

    vector<ll> v[k+2];

    for(i=0;i<k;i++)
    {
        v[i].pb(1);
    }

    j=0;

    for(i=2;i<=n;i++)
    {
        v[j].pb(i);
        j++;
        j%=k;
    }

    for(i=0;i<k;i++)
    {
        for(j=1;j<v[i].size();j++)
        {
            cout<<v[i][j]<<" "<<v[i][j-1]<<endl;
        }
    }

}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve()
{
    int n,exitn;
    cin>>n>>exitn;
    int nexitn=n-exitn;
    vector<pair<int,int> >v;
     for(int i=2;i<=nexitn;i++)
         v.push_back({i-1,i});
    v.push_back({1,nexitn+1});
    v.push_back({nexitn,nexitn+2});
     for(int j=nexitn+3;j<=n;j++)
      {
         v.push_back({1,j});
      }
    cout<<v.size()<<endl;
    for(auto itr:v)
        cout<<itr.first<<" "<<itr.second<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;//cin>>t;
    while(t--)
    {
    solve();
    }
return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve()
{
    int n,exitn;
    cin>>n>>exitn;
    int nexitn=n-exitn;
    vector<pair<int,int> >v;
     for(int i=2;i<=nexitn;i++)
         v.push_back({i-1,i});
    v.push_back({1,nexitn+1});
    v.push_back({nexitn,nexitn+2});
     for(int j=nexitn+3;j<=n;j++)
      {
         v.push_back({1,j});
      }
    for(auto itr:v)
        cout<<itr.first<<" "<<itr.second<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;//cin>>t;
    while(t--)
    {
    solve();
    }
return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve()
{
    int n,exitn;
    cin>>n>>exitn;
    int nexitn=n-exitn;
    vector<pair<int,int> >v;
     for(int i=2;i<=nexitn;i++)
         v.push_back({i-1,i});
    v.push_back({1,nexitn+1});
    v.push_back({nexitn,nexitn+2});
     for(int j=nexitn+3;j<=n;j++)
      {
         v.push_back({1,j});
      }
    cout<<nexitn+1<<endl;
    for(auto itr:v)
        cout<<itr.first<<" "<<itr.second<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;//cin>>t;
    while(t--)
    {
    solve();
    }
return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve()
{
    int n,exitn;
    cin>>n>>exitn;
    int depth[n+1]={0};
    vector<pair<int,int> >v;
    for(int i=2;i<=n;i++){
        int u=max(1LL,i-exitn);
        depth[i]=depth[u]+1;
        v.push_back({i,u});
    }
    cout<<depth[n]+depth[n-1]<<endl;
    for(auto itr:v)
        cout<<itr.first<<" "<<itr.second<<endl;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;//cin>>t;
    while(t--)
    {
    solve();
    }
return 0;
}


// done by me; you are mal if you are 10
#include<bits/stdc++.h>
#define NFS ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
typedef long long ll;
const int N = 2e5 + 1;
const int mod = 1e9 + 7;
long long a[N];
string en = "\n";
vector <int> g[N], e;

void dfs(int v, int dis)
{
    for (auto u : g[v])
    {
        dfs(u, dis + 1);
    }
    if (g[v].size() == 0)
    {
        e.push_back(dis);
    }
}

int main()
{
    NFS;
    int n, k;
    cin >> n >> k;
    int c = 0;
    if (n % k != 0)
    {
        c ++;
    }
    for (int i = 2; i <= k + 1; i ++)
    {
        g[1].push_back(i);
    }
    for (int i = 2; i <= n - k; i ++)
    {
        g[i].push_back(i + k);
    }
    dfs(1, 0);
    sort(e.begin(), e.end());
    cout << e[e.size() - 1] + e[e.size() - 2] << en;
    for (int i = 1; i <= n; i ++)
    {
        for (auto u : g[i])
        {
            cout << i << " " << u << en;
        }
    }
    return 0;
}


// done by me; you are mal if you are 10
#include<bits/stdc++.h>
#define NFS ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const int mod = 1e9 + 7;
long long a[N];
string en = "\n";
vector <int> g[N], e;

void dfs(int v, int dis)
{
    for (auto u : g[v])
    {
        dfs(u, dis + 1);
    }
    if (g[v].size() == 0)
    {
        e.push_back(dis);
    }
}

int main()
{
    NFS;
    int n, k;
    cin >> n >> k;
    int c = 0;
    if (n % k != 0)
    {
        c ++;
    }
    for (int i = 2; i <= k + 1; i ++)
    {
        g[1].push_back(i);
    }
    for (int i = 2; i <= n - k; i ++)
    {
        g[i].push_back(i + k);
    }
    dfs(1, 0);
    sort(e.begin(), e.end());
    cout << e[e.size() - 1] + e[e.size() - 2] << en;
    for (int i = 1; i <= n; i ++)
    {
        for (auto u : g[i])
        {
            cout << i << " " << u << en;
        }
    }
    return 0;
}


// done by me; you are mal if you are 10
#include<bits/stdc++.h>
#define NFS ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const int mod = 1e9 + 7;
long long a[N];
string en = "\n";

int main()
{
    NFS;
    int n, k;
    cin >> n >> k;
    cout << k << en;
    for (int i = 2; i <= n - k + 1; i ++)
    {
        cout << i - 1 << " " << i << en;
    }
    for (int i = n - k + 2; i <= n; i ++)
    {
        cout << n - k + 1 << " " << i << en;
    }
    return 0;
}


// done by me; you are mal if you are 10
#include<bits/stdc++.h>
#define NFS ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
typedef long long ll;
const int N = 1e5 + 1;
const int mod = 1e9 + 7;
long long a[N];
string en = "\n";

int main()
{
    NFS;
    int n, k;
    cin >> n >> k;
    cout << n - k + 1 << en;
    for (int i = 2; i <= n - k + 1; i ++)
    {
        cout << i - 1 << " " << i << en;
    }
    for (int i = n - k + 2; i <= n; i ++)
    {
        cout << n - k + 1 << " " << i << en;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
int main(int argc, char const *argv[])
{
	ll n, k;
	cin >> n >> k;
	vector<vll> chains;
	for (int i = 2; i <= k + 1; ++i)
	{
		chains.pb({1});
		chains.back().pb(i);
	}
	ll idx = 0;
	for (int i = k + 2; i <= n; ++i)
	{
		chains[idx].pb(chains[idx].back());
		chains[idx].pb(i);
		idx ++;
		idx %= k;
	}
	cout << k << '\n';
	for (int i = 0; i < chains.size(); ++i)
	{

		for (int j = 0; j < (ll)chains[i].size() - 1; j+=2)
		{
			cout << chains[i][j] << ' ' << chains[i][j + 1] << '\n';
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
int main(int argc, char const *argv[])
{
	ll n, k;
	cin >> n >> k;
	vector<vll> chains;
	for (int i = 2; i <= k + 1; ++i)
	{
		chains.pb({1});
		chains.back().pb(i);
	}
	ll idx = 0;
	for (int i = k + 2; i <= n; ++i)
	{
		chains[idx].pb(chains[idx].back());
		chains[idx].pb(i);
		idx ++;
		idx %= k;
	}
	cout << n - 1 << '\n';
	for (int i = 0; i < chains.size(); ++i)
	{

		for (int j = 0; j < (ll)chains[i].size() - 1; j+=2)
		{
			cout << chains[i][j] << ' ' << chains[i][j + 1] << '\n';
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
int main(int argc, char const *argv[])
{
	ll n, k;
	cin >> n >> k;
	vector<vll> chains;
	for (int i = 2; i <= k + 1; ++i)
	{
		chains.pb({1});
		chains.back().pb(i);
	}
	ll idx = 0, mx1 = 0, mx2 = 0;
	for (int i = k + 2; i <= n; ++i)
	{
		chains[idx].pb(chains[idx].back());
		chains[idx].pb(i);
		idx ++;
		idx %= k;
	}
	for (int i = 0; i < chains.size(); ++i)
	{
		ll sz = chains[i].size() / 2 + 1;
		if (mx1 <= sz)
		{
			mx2 = mx1;
			mx1 = sz;
		}
		else if (mx2 <= sz)
		{
			mx2 = sz;
		}
	}
	cout << mx2 + mx1 - 2 << '\n';
	for (int i = 0; i < chains.size(); ++i)
	{
		for (int j = 0; j < (ll)chains[i].size() - 1; j+=2)
		{
			cout << chains[i][j] << ' ' << chains[i][j + 1] << '\n';
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
int main(int argc, char const *argv[])
{
	ll n, k;
	cin >> n >> k;
	vector<vll> chains;
	for (int i = 2; i <= k + 1; ++i)
	{
		chains.pb({1});
		chains.back().pb(i);
	}
	ll idx = 0;
	for (int i = k + 2; i <= n; ++i)
	{
		chains[idx].pb(chains[idx].back());
		chains[idx].pb(i);
		idx ++;
		idx %= k;
	}
	for (int i = 0; i < chains.size(); ++i)
	{

		for (int j = 0; j < (ll)chains[i].size() - 1; j+=2)
		{
			cout << chains[i][j] << ' ' << chains[i][j + 1] << '\n';
		}
	}
	return 0;
}


// clang-format off
#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;

#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;

#define INF 10000000000000000
#define inf 1000000000
// #define MOD 1000000007
#define MOD 32416190071
#define PI 3.1415926535897932385

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()

#define fr(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 frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }

ld modp(ld b, ll e) {
    ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

void solve(){
    ll n, k;
    cin>>n>>k;
    vi parent(n+1,-1);
    ll z=(n-k-1)/k;
    vi roots(k,z);
    ll rem=(n-k-1)%k;
    rep(i, rem){
        roots[i]++;
    }
    if(rem==0){
        cout<<(z+1)*2<<"\n";
    }else if(rem==1){
        cout<<(z+1)*2+1<<"\n";
    }else{
        cout<<(z+1)*2+1<<"\n";
    }
    ll beg=2;
    rep(i,k){
        parent[beg]=1;
        beg++;
        rep(j,roots[i]){
            parent[beg]=beg-1;
            beg++;
        }
    }
    fr(i,2,n){
        cout<<i<<" "<<parent[i]<<"\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);   cout<<fixed;
    #ifdef godfather
        cin.exceptions(cin.failbit);
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    fr(i,1,t)
    {
        solve();
    }
    end_routine();
}


// clang-format off
#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;

#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
        tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;

#define INF 10000000000000000
#define inf 1000000000
// #define MOD 1000000007
#define MOD 32416190071
#define PI 3.1415926535897932385

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()

#define fr(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 frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(; ;i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<"| ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
    ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }

ld modp(ld b, ll e) {
    ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

void solve(){
    ll n, k;
    cin>>n>>k;
    vi parent(n+1,-1);
    ll z=(n-k-1)/k;
    vi roots(k,z);
    ll rem=(n-k-1)%k;
    rep(i, rem){
        roots[i]++;
    }
    if(rem==0){
        cout<<(z+1)*2<<"\n";
    }else if(rem==1){
        cout<<(z+1)*2+1<<"\n";
    }else{
        cout<<(z+1)*2+2<<"\n";
    }
    ll beg=2;
    rep(i,k){
        parent[beg]=1;
        beg++;
        rep(j,roots[i]){
            parent[beg]=beg-1;
            beg++;
        }
    }
    fr(i,2,n){
        cout<<i<<" "<<parent[i]<<"\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(15);   cout<<fixed;
    #ifdef godfather
        cin.exceptions(cin.failbit);
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    fr(i,1,t)
    {
        solve();
    }
    end_routine();
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

char ans[3000005];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

// -------------------------------------Code starts here---------------------------------------------------------------------

  vector<pair<int,int> >ans;
  int n,k;
  cin>>n>>k;

  int cnt=1;
  vector<int>lol[k];
  for(int i=0;i<k;i++)
    lol[i].push_back(1);

  for(int i=2;i<=n;i++)
  {
    lol[i%k].push_back(i);
  }
  for(int i=0;i<k;i++)
  {
    int sz=lol[i].size();
    for(int j=1;j<sz;j++)
      ans.push_back({lol[i][j-1],lol[i][j]});
  }
  int res=0;
  if((n-1)%k==0)
  {
    res=2*((n-1)/k);
  }
  else if((n-1)%k==1)
  {
    res=2*((n-1)/k)+1;
  }
  else
  {
    res=2*((n-1)/k)+2;
  }

  cout<<res<<'\n';
  for(auto i:ans)
    cout<<i.first<<" "<<i.second<<'\n';

// -------------------------------------Code ends here------------------------------------------------------------------
    clock_t clk;
  clk = clock();

    clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

char ans[3000005];

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

// -------------------------------------Code starts here---------------------------------------------------------------------

  vector<pair<int,int> >ans;
  int n,k;
  cin>>n>>k;
  int z=(n-1)/k;
  int cnt=1;
  vector<int>lol[z];
  for(int i=0;i<z;i++)
    lol[i].push_back(1);
  for(int i=2;i<=n;i++)
  {
    lol[i%z].push_back(i);
  }
  for(int i=0;i<z;i++)
  {
    int sz=lol[i].size();
    for(int j=1;j<sz;j++)
      ans.push_back({lol[i][j-1],lol[i][j]});
  }
  int res=0;
  if((n-1)%k==0)
  {
    res=2*((n-1)/k);
  }
  else if((n-1)%k==1)
  {
    res=2*((n-1)/k)+1;
  }
  else
  {
    res=2*((n-1)/k)+2;
  }

  cout<<res<<'\n';
  for(auto i:ans)
    cout<<i.first<<" "<<i.second<<'\n';

// -------------------------------------Code ends here------------------------------------------------------------------
    clock_t clk;
  clk = clock();

    clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
  return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=200100;
const int M=2000100;
const LL mod=1e9+7;
int main(){
	int n,k;cin>>n>>k;
	if((n-k-1)%k==0){
		int sum=(n-k-1)/k,cnt=1,c=n;
		printf("%d\n",(sum+1)*2);
		for(int i=1;i<=k;i++){
			for(int j=1;j<=sum;j++){
				if(j==1) printf("%d %d\n",1,cnt+1),cnt++;
				else printf("%d %d\n",cnt,cnt+1),cnt++;
			}
			printf("%d %d\n",cnt,c--);
		}
	}
	else{
		int sum1=(n-k-1)/k,sum2=(n-k-1)%k,cnt=1,c=n;
		if(sum2>=2) printf("%d\n",(sum1+2)*2);
		else printf("%d\n",(sum1+1)*2+1);
		for(int i=1;i<=k;i++){
			for(int j=1;j<=sum1;j++){
				if(j==1) printf("%d %d\n",1,cnt+1),cnt++;
				else printf("%d %d\n",cnt,cnt+1),cnt++;
			}
			if(sum2){
				printf("%d %d\n",cnt,cnt+1);
				cnt++;
				sum2--;
			}
			printf("%d %d\n",cnt,c--);
			if(sum1==0&&sum2==0) cnt=1;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=200100;
const int M=2000100;
const LL mod=1e9+7;
int main(){
	int n,k;cin>>n>>k;
	if((n-k-1)%k==0){
		int sum=(n-k-1)/k,cnt=1,c=n;
		printf("%d\n",(sum+1)*2);
		for(int i=1;i<=k;i++){
			for(int j=1;j<=sum;j++){
				if(j==1) printf("%d %d\n",1,cnt+1),cnt++;
				else printf("%d %d\n",cnt,cnt+1),cnt++;
			}
			printf("%d %d\n",cnt,c--);
		}
	}
	else{
		int sum1=(n-k-1)/k,sum2=(n-k-1)%k,cnt=1,c=n;
		if(sum2>=2) printf("%d\n",(sum1+2)*2);
		else printf("%d\n",(sum1+1)*2+1);
		for(int i=1;i<=k;i++){
			for(int j=1;j<=sum1;j++){
				if(j==1) printf("%d %d\n",1,cnt+1),cnt++;
				else printf("%d %d\n",cnt,cnt+1),cnt++;
			}
			if(sum2&&sum1){
				printf("%d %d\n",cnt,cnt+1);cnt++;
				sum2--;
			}
			else if(sum2) printf("%d %d\n",1,cnt+1),cnt++,sum2--;
			printf("%d %d\n",cnt,c--);//叶子
			if(sum1==0&&sum2==0) cnt=1;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

vector<pii>edges;

int main()
{

	fio;
	ll n,m,i,j,k,l,r,ind,c,d;
	cin>>n>>k;

	j=k+1;

	ll z=1,count=0;
	while(j<=n)
	{
		edges.pb(mp(j,z));
		count++;
		z=j;
		j++;
	}
	ll ans=count+1;
	for(j=2;j<=k;j++)
	{
		edges.pb(mp(n,j));
	}
	cout<<ans<<endl;
	for(i=0;i<edges.size();i++)
	{
		cout<<edges[i].fi<<" "<<edges[i].se<<endl;
	}

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

vector<pii>edges;

int main()
{

	fio;
	ll n,m,i,j,k,l,r,ind,c,d,x;
	cin>>n>>k;

	j=k+1;

    ll z=n/k;
    ll count=0,ans=2*z;
    j=2;
    vi vec,vec2;
    for(i=1;i<=k;i++)
    {
    	count=0;
    	while(count<z&&j<=n)
    	{
    		if(count==0)
    		edges.pb(mp(1,j));
    		else
    		edges.pb(mp(j-1,j));
    		j++;
    		count++;

		}
		vec2.pb(count);
		vec.pb(j-1);

	}
	if(j==n+1)
	{
		sort(vec2.begin(),vec2.end());
		ll sum=vec2[vec2.size()-1];
		if(vec2.size()>=2)
		sum+=vec2[vec2.size()-2];
		cout<<sum<<endl;
			for(i=0;i<edges.size();i++)
	{
		cout<<edges[i].fi<<" "<<edges[i].se<<endl;
	}
	return 0;

	}
	ll flag=0;
	for(i=0;i<vec.size();i++)
	{
		if(j==n+1)
		break;
		edges.pb(mp(vec[i],j));
		j++;
		flag++;

	}
	if(flag==1)
	ans++;
	else if(flag>1)
	ans+=2;
	cout<<ans<<endl;
	for(i=0;i<edges.size();i++)
	{
		cout<<edges[i].fi<<" "<<edges[i].se<<endl;
	}

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

vector<pii>edges;

int main()
{

	fio;
	ll n,m,i,j,k,l,r,ind,c,d,x;
	cin>>n>>k;

	j=k+1;

    ll z=n/k;
    ll count=0,ans=2*z;
    j=2;
    vi vec;
    for(i=1;i<=k;i++)
    {
    	count=0;
    	while(count<z)
    	{
    		edges.pb(mp(1,j));
    		j++;
    		count++;
		}
		vec.pb(j-1);

	}
	ll flag=0;
	for(i=0;i<vec.size();i++)
	{
		if(j==n+1)
		break;
		edges.pb(mp(vec[i],j));
		j++;
		flag++;

	}
	if(flag==1)
	ans++;
	else if(flag>1)
	ans+=2;
	cout<<ans<<endl;
	for(i=0;i<edges.size();i++)
	{
		cout<<edges[i].fi<<" "<<edges[i].se<<endl;
	}

}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    int count=2;
    queue<int> q;
    int dist=2;
    int temp=(n-k-1)%k;
    int temp1=(n-k-1)/k;
    if(temp==0)
        cout<<2*( 1+temp1 )<<"\n";
    else if(temp ==1)
        cout<<2*( temp1 + 1 ) + 1<<"\n";
    else if(temp>=2)
        cout<<2*( temp1 + 1 ) + 2<<"\n";
    int counter=0;
    for(int i=0;i<k;i++)
    {
        cout<<1<<" "<<count<<"\n";
        counter++;
        q.push(count);
        count++;
    }
    while(count!=n+1)
    {
        int temp2=q.front();
        q.pop();
        cout<<temp2<<" "<<count<<"\n";
        counter++;
        q.push(count);
        count++;

    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    int count=2;
    queue<int> q;
    int dist=2;
    int temp=(n-k-1)%k;
    int temp1=(n-k-1)/k;
    if(temp==0)
    {
        cout<<2*( 1+temp1 )<<"\n";
    }
    else if(temp ==1)
        cout<<2*( temp1 + 1 ) + 1<<"\n";
    else if(temp>=2)
        cout<<2*( temp1 + 1 ) + 2<<"\n";
    for(int i=0;i<k;i++)
    {
        cout<<1<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    while(count!=n+1)
    {
        int temp=q.front();
        q.pop();
        cout<<temp<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    int count=2;
    queue<int> q;
    set<int> s;
    int dist=2;
    int temp=(n-k-1)%k;
    if(temp==0)
    {
        cout<<2*((n-k-1)/k + 1)<<"\n";
    }
    else if(temp > 0)
    {
        cout<<2*((n-k-1)/k + 1) + 1<<"\n";
    }
    for(int i=0;i<k;i++)
    {
        cout<<1<<" "<<count<<"\n";
        count++;
        s.insert(count);
        q.push(count);
    }
    while(count!=n+1)
    {
        int temp=q.front();
        q.pop();
        cout<<temp<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    int count=2;
    queue<int> q;
    for(int i=0;i<k;i++)
    {
        cout<<1<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    while(count!=n+1)
    {
        int temp=q.front();
        q.pop();
        cout<<temp<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long int
using namespace std;
signed main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);cout.tie(NULL);
    int n,k;
    cin>>n>>k;
    int count=2;
    queue<int> q;
    int dist=2;
    int temp=(n-k-1)%k;
    int temp1=(n-k-1)/k;
    if(temp==0)
        cout<<2*( 1+temp1 )<<"\n";
    else if(temp ==1)
        cout<<2*( temp1 + 1 ) + 1<<"\n";
    else if(temp>=2)
        cout<<2*( temp1 + 1 ) + 2<<"\n";
    for(int i=0;i<k;i++)
    {
        cout<<1<<" "<<count<<"\n";
        q.push(count);
        count++;
    }
    while(count!=n+1)
    {
        int temp=q.front();
        q.pop();
        cout<<temp<<" "<<count<<"\n";
        count++;
        q.push(count);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ull unsigned ll
#define pb push_back
#define popb pop_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair <int,int>
#define piii pair <pii,int>
#define piiii pair <pii,pii>
#define plll pair <pair <ll, ll>, ll>
#define rep0(i,n) for(ll i=0;i<(ll)n;i++)
#define rep1(i,n) for(ll i=1;i<=(ll)n;i++)
#define rep(i,x,y) for(ll i=(ll)x;i<=(ll)y;i++)
#define reprev(i,n) for(ll i=(ll)n;i>=0;i--)
#define all(c) c.begin(),c.end()
#define print(a) rep0(i,a.size()) cout << a[i] << " "
#define hi() cout << "\nHello, I am error. Can you find me? :):):)\n"
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_eppoch().count())

using namespace __gnu_pbds;
using namespace std;
using ordered_set =  tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;

const int mod = 998244353;
const long double pi = acos(-1);

// FOR DEBUGGING
vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}

void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", ";
	else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifndef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

ll power(ll x, ll y){
	x %= mod;
	ll ans = 1;
	while(y){
		if(y&1) ans = (ans*x)%mod;
		y /= 2;
		x = (x*x)%mod;
	}
	return ans;
}

void solve(){

	int n,k;
	cin >> n >> k;
	vector <int> adj[k+1];
	if(k==2) cout << n-1 << "\n";
	else{
		if(n%k==2) cout << 2*(n/k)+1 << "\n";
		else{
			int tmp = ((n-2)/k)*k + 2;
			cout << 2*(tmp/k)+2 << "\n";
		}
	}
	rep1(i,k){
		cout << 1 << " " << i+1 << "\n";
		adj[i].pb(i+1);
	}
	int it = k+2;
	while(it<=n){
		rep1(i,k){
			cout << adj[i][adj[i].size()-1] << " " << it << "\n";
			adj[i].pb(it);
			it++;
			if(it>n) break;
		}
	}

	return;
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int TC = 1,t = 0;
	// cin >> TC;
	while(t++ < TC){
		// cout << "Case #" << t << ": ";
		solve();
		// cout << "\n";
	}

	cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

	return 0;
}


#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ull unsigned ll
#define pb push_back
#define popb pop_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair <int,int>
#define piii pair <pii,int>
#define piiii pair <pii,pii>
#define plll pair <pair <ll, ll>, ll>
#define rep0(i,n) for(ll i=0;i<(ll)n;i++)
#define rep1(i,n) for(ll i=1;i<=(ll)n;i++)
#define rep(i,x,y) for(ll i=(ll)x;i<=(ll)y;i++)
#define reprev(i,n) for(ll i=(ll)n;i>=0;i--)
#define all(c) c.begin(),c.end()
#define print(a) rep0(i,a.size()) cout << a[i] << " "
#define hi() cout << "\nHello, I am error. Can you find me? :):):)\n"
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_eppoch().count())

using namespace __gnu_pbds;
using namespace std;
using ordered_set =  tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;

const int mod = 998244353;
const long double pi = acos(-1);

// FOR DEBUGGING
vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}

void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", ";
	else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifndef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

ll power(ll x, ll y){
	x %= mod;
	ll ans = 1;
	while(y){
		if(y&1) ans = (ans*x)%mod;
		y /= 2;
		x = (x*x)%mod;
	}
	return ans;
}

void solve(){

	int n,k;
	cin >> n >> k;
	vector <int> adj[k+1];
	if(k==2) cout << n-1 << "\n";
	else{
		cout << (n+k-2)/k + 1 << "\n";
	}
	rep1(i,k){
		cout << 1 << " " << i+1 << "\n";
		adj[i].pb(i+1);
	}
	int it = k+2;
	while(it<=n){
		rep1(i,k){
			cout << adj[i][adj[i].size()-1] << " " << it << "\n";
			adj[i].pb(it);
			it++;
			if(it>n) break;
		}
	}

	return;
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int TC = 1,t = 0;
	// cin >> TC;
	while(t++ < TC){
		// cout << "Case #" << t << ": ";
		solve();
		// cout << "\n";
	}

	cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

	return 0;
}


#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ull unsigned ll
#define pb push_back
#define popb pop_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair <int,int>
#define piii pair <pii,int>
#define piiii pair <pii,pii>
#define plll pair <pair <ll, ll>, ll>
#define rep0(i,n) for(ll i=0;i<(ll)n;i++)
#define rep1(i,n) for(ll i=1;i<=(ll)n;i++)
#define rep(i,x,y) for(ll i=(ll)x;i<=(ll)y;i++)
#define reprev(i,n) for(ll i=(ll)n;i>=0;i--)
#define all(c) c.begin(),c.end()
#define print(a) rep0(i,a.size()) cout << a[i] << " "
#define hi() cout << "\nHello, I am error. Can you find me? :):):)\n"
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_eppoch().count())

using namespace __gnu_pbds;
using namespace std;
using ordered_set =  tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;

const int mod = 998244353;
const long double pi = acos(-1);

// FOR DEBUGGING
vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}

void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", ";
	else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifndef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

ll power(ll x, ll y){
	x %= mod;
	ll ans = 1;
	while(y){
		if(y&1) ans = (ans*x)%mod;
		y /= 2;
		x = (x*x)%mod;
	}
	return ans;
}

void solve(){

	int n,k;
	cin >> n >> k;
	vector <int> adj[k+1];
	rep1(i,k){
		cout << 1 << " " << i+1 << "\n";
		adj[i].pb(i+1);
	}
	int it = k+2;
	while(it<=n){
		rep1(i,k){
			cout << adj[i][adj[i].size()-1] << " " << it << "\n";
			adj[i].pb(it);
			it++;
			if(it>n) break;
		}
	}

	return;
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int TC = 1,t = 0;
	// cin >> TC;
	while(t++ < TC){
		// cout << "Case #" << t << ": ";
		solve();
		// cout << "\n";
	}

	cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

	return 0;
}


#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ull unsigned ll
#define pb push_back
#define popb pop_back
#define mt make_tuple
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair <int,int>
#define piii pair <pii,int>
#define piiii pair <pii,pii>
#define plll pair <pair <ll, ll>, ll>
#define rep0(i,n) for(ll i=0;i<(ll)n;i++)
#define rep1(i,n) for(ll i=1;i<=(ll)n;i++)
#define rep(i,x,y) for(ll i=(ll)x;i<=(ll)y;i++)
#define reprev(i,n) for(ll i=(ll)n;i>=0;i--)
#define all(c) c.begin(),c.end()
#define print(a) rep0(i,a.size()) cout << a[i] << " "
#define hi() cout << "\nHello, I am error. Can you find me? :):):)\n"
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_eppoch().count())

using namespace __gnu_pbds;
using namespace std;
using ordered_set =  tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;

const int mod = 998244353;
const long double pi = acos(-1);

// FOR DEBUGGING
vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}

void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", ";
	else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifndef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

void solve(){

	int n,k;
	cin >> n >> k;
	vector <int> adj[k+1];
	if(k==2) cout << n-1 << "\n";
	else{
		if(n%k==2) cout << 2*(n/k)+1 << "\n";
		else{
			int tmp = ((n-2)/k)*k + 2;
			cout << 2*(tmp/k)+2 << "\n";
		}
	}
	rep1(i,k){
		cout << 1 << " " << i+1 << "\n";
		adj[i].pb(i+1);
	}
	int it = k+2;
	while(it<=n){
		rep1(i,k){
			cout << adj[i][adj[i].size()-1] << " " << it << "\n";
			adj[i].pb(it);
			it++;
			if(it>n) break;
		}
	}

	return;
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int TC = 1,t = 0;
	// cin >> TC;
	while(t++ < TC){
		// cout << "Case #" << t << ": ";
		solve();
		// cout << "\n";
	}

	cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

	return 0;
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define N 100005
#define inf 1e18
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << (#x) << " = " << x << endl;
#define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl;

using namespace std;

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

/* nCr % mod
ll fact[6*N];
ll inv[6*N],invfac[6*N];
ll mod = 998244353;

void factorial()
{
    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
    inv[1] = 1;
    for(int i=2;i<=5*N + 10;i++)
    {
        fact[i] = (fact[i-1]*i)%mod;
        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
        invfac[i] = (invfac[i-1]*inv[i])%mod;
    }
}*/

vector<ll> primes;

void Sieve(int n)
{
    vector<bool> prime(n+1,true);
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int Nmax = 5e5 + 6;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;
    cin >> n >> k;
    int m = n-k;
    int leaf = n-k+1;
    int ans = 2+m-1;
    cout << ans << '\n';
    for(int i=1;i<m;i++)
    {
        cout << i << " " << i+1 << "\n";
    }
    cout << leaf << " " << n-k << "\n";
    leaf++;
    while(leaf <= n)
        cout << leaf++ << " " << 1 << "\n";
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define N 100005
#define inf 1e18
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << (#x) << " = " << x << endl;
#define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl;

using namespace std;

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

/* nCr % mod
ll fact[6*N];
ll inv[6*N],invfac[6*N];
ll mod = 998244353;

void factorial()
{
    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
    inv[1] = 1;
    for(int i=2;i<=5*N + 10;i++)
    {
        fact[i] = (fact[i-1]*i)%mod;
        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
        invfac[i] = (invfac[i-1]*inv[i])%mod;
    }
}*/

vector<ll> primes;

void Sieve(int n)
{
    vector<bool> prime(n+1,true);
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int Nmax = 5e5 + 6;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;
    cin >> n >> k;
    int m = n-k;
    int leaf = n-k+1;
    int ans = (n-1)/k*2 + min((n-1)%k,2);
    cout << ans << "\n";
    for(int i=2;i<=k+1;i++)
        cout << i << " " << 1 << "\n";
    for(int i=k+2;i<=n;i++)
        cout << i-k << " " << i << "\n";
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define N 100005
#define inf 1e18
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << (#x) << " = " << x << endl;
#define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl;

using namespace std;

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

/* nCr % mod
ll fact[6*N];
ll inv[6*N],invfac[6*N];
ll mod = 998244353;

void factorial()
{
    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
    inv[1] = 1;
    for(int i=2;i<=5*N + 10;i++)
    {
        fact[i] = (fact[i-1]*i)%mod;
        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
        invfac[i] = (invfac[i-1]*inv[i])%mod;
    }
}*/

vector<ll> primes;

void Sieve(int n)
{
    vector<bool> prime(n+1,true);
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int Nmax = 5e5 + 6;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;
    cin >> n >> k;
    int m = n-k;
    int leaf = n-k+1;
    int ans = n-leaf+1+m-1;
    cout << ans << '\n';
    for(int i=1;i<m;i++)
    {
        cout << i << " " << i+1 << "\n";
    }
    cout << leaf << " " << n-k << "\n";
    leaf++;
    while(leaf <= n)
        cout << leaf++ << " " << 1 << "\n";
}


#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define N 100005
#define inf 1e18
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cout << (#x) << " = " << x << endl;
#define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl;

using namespace std;

ll binomialCoeff(ll n,ll k)
{
    ll res = 1;
    if(k >n - k)
        k = n - k;
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

ll power(ll x,ll y)
{
    ll temp;
    if(y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

/* nCr % mod
ll fact[6*N];
ll inv[6*N],invfac[6*N];
ll mod = 998244353;

void factorial()
{
    fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
    inv[1] = 1;
    for(int i=2;i<=5*N + 10;i++)
    {
        fact[i] = (fact[i-1]*i)%mod;
        inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
        invfac[i] = (invfac[i-1]*inv[i])%mod;
    }
}*/

vector<ll> primes;

void Sieve(int n)
{
    vector<bool> prime(n+1,true);
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p])
          primes.push_back(p);
}

//******************************************************    CHECK CONSTRAINTS   ***************************************************************//

const int Nmax = 5e5 + 6;

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k;
    cin >> n >> k;
    int m = n-k;
    int leaf = n-k+1;
    for(int i=1;i<m;i++)
    {
        cout << i << " " << i+1 << "\n";
    }
    cout << leaf << " " << n-k << "\n";
    leaf++;
    while(leaf <= n)
        cout << leaf++ << " " << 1 << "\n";
}


/*input
5 3

*/
#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;

#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#define ll long long
#define ld long double
#define pii pair<int,int>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define MP make_pair
const ll INF64=4e12;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1){
            res=(res*a)%MOD;
        }
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
int arr[maxn];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,k;
    cin>>n>>k;
    --n;
    int cur=2;
    if(n%k==1) cout<<(n+k-1)/k+n/k;
    else cout<<2*((n+k-1)/k);
    cout<<'\n';
    REP(i,n%k){
        cout<<1<<' '<<cur++<<'\n';
        REP(i,(n+k-1)/k-1) cout<<cur-1<<' '<<cur++<<'\n';
    }
    REP(i,k-(n%k)){
        cout<<1<<' '<<cur++<<'\n';
        REP(i,n/k-1) cout<<cur-1<<' '<<cur++<<'\n';
    }
}


/*input
5 3

*/
#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;

#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
#define ll long long
#define ld long double
#define pii pair<int,int>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define MP make_pair
const ll INF64=4e12;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1){
            res=(res*a)%MOD;
        }
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
int arr[maxn];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n,k;
    cin>>n>>k;
    --n;
    int cur=2;
    if(n%k==1) cout<<(n+k-1)/k+n/k;
    else cout<<2*((n+k-1)/k);
    cout<<'\n';
    REP(i,n%k){
        cout<<1<<' '<<cur++<<'\n';
        REP(i,(n+k-1)/k-1) cout<<cur-1<<' '<<cur<<'\n';
    }
    REP(i,k-(n%k)){
        cout<<1<<' '<<cur++<<'\n';
        REP(i,n/k-1) cout<<cur-1<<' '<<cur<<'\n';
    }
}


//include <god>
/*                   ___    ____    ___    ___
      /\     |        |    |____|  |         /     /\
     /__\    |        |    |  \    |---     /     /__\
    /    \   |___    _|_   |   \   |___    /__   /    \
*/
#include <bits/stdc++.h>

#define 	Time 	        	printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define 	For(J,R,K)       	for(ll J=R;J<K;++J)
#define 	Rep(I,N)         	For(I,0,N)
#define 	MP 		        	make_pair
#define 	ALL(X)       		(X).begin(),(X).end()
#define 	SF 	        		scanf
#define 	PF 		        	printf
#define 	pii         		pair<long long,long long>
#define 	piii         		pair<pii , long long >
#define 	pdd         		pair<double , double>
#define 	Sort(v) 	        sort(ALL(v))
#define     GSORT(x)            sort(ALL(x), greater<typeof(*((x).begin()))>())
#define 	Test 		        freopen("a.in","r",stdin)
#define 	Testout 	        freopen("a.out","w",stdout)
#define     UNIQUE(v)           Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define 	pb 		        	push_back
#define 	Set(a,n)        	memset(a,n,sizeof(a))
#define 	MAXN 		        100000+99
#define 	EPS 		        1e-10
#define 	inf 		        1ll<<62
#define     SF                  scanf
#define     PF                  printf
#define     F                   first
#define     S                   second
#define 	vpii         		vector<pii>
#define 	vll        		    vector<ll>
#define     booste              ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;
typedef long double ld;

using namespace std;

inline ll get_num(char ch){
	if(ch == '-')return 0;
	else if(ch >= 'a' && ch <= 'z'){
		return 1 + (ch-'a');
	}
	else if(ch >= 'A' && ch <= 'Z'){
		return 27 +(ch - 'A');
	}
	else if(ch >= '0' && ch <= '9'){
		return 53 +(ch - '0');
	}
}
 int dx[] = {1,0,-1, 0} , dy[] = {0,1, 0, -1};  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
inline int GA(ll n , vector<ll>&vec){
    Rep(i ,n){ll x ;cin>>x ; vec.push_back(x);}
}
inline int GAEG(ll m ,vector<ll>vec[]){
    Rep(i ,m){ll x  , y ;cin>>x>>y ; x-- , y-- ; vec[x].push_back(y); vec[y].push_back(x);}

}

ll dis[2000001];
ll pr[2000001];
int main()
{
  //  Test;
    ll n , k;
    cin>>n>>k;
    For(i ,0, k){
        dis[i+1]=1;
    }
    ll cnt= 1;
    For(i ,k+1 , n)
    {
        dis[cnt]++;
        cnt++;
        if(cnt==k+1)
            cnt%=k;
    }
    if(cnt==2) cout<<dis[1]+dis[k]<<endl;
    else if(cnt==1)cout<<dis[k]+dis[k-1]<<endl;
    else cout<<dis[cnt-1]+dis[cnt-2]<<endl;
    Rep(i ,k){
        cout<<1<<" "<<i+2<<endl;
    }
    cnt =1;
    Rep(i ,n-k-1){
        cout<<cnt<<" "<<i+k+2<<endl;
        cnt++;
        if(cnt==k+1) cnt%=k;
    }
    return 0;

}


//include <god>
/*                   ___    ____    ___    ___
      /\     |        |    |____|  |         /     /\
     /__\    |        |    |  \    |---     /     /__\
    /    \   |___    _|_   |   \   |___    /__   /    \
*/
#include <bits/stdc++.h>

#define 	Time 	        	printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define 	For(J,R,K)       	for(ll J=R;J<K;++J)
#define 	Rep(I,N)         	For(I,0,N)
#define 	MP 		        	make_pair
#define 	ALL(X)       		(X).begin(),(X).end()
#define 	SF 	        		scanf
#define 	PF 		        	printf
#define 	pii         		pair<long long,long long>
#define 	piii         		pair<pii , long long >
#define 	pdd         		pair<double , double>
#define 	Sort(v) 	        sort(ALL(v))
#define     GSORT(x)            sort(ALL(x), greater<typeof(*((x).begin()))>())
#define 	Test 		        freopen("a.in","r",stdin)
#define 	Testout 	        freopen("a.out","w",stdout)
#define     UNIQUE(v)           Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define 	pb 		        	push_back
#define 	Set(a,n)        	memset(a,n,sizeof(a))
#define 	MAXN 		        100000+99
#define 	EPS 		        1e-10
#define 	inf 		        1ll<<62
#define     SF                  scanf
#define     PF                  printf
#define     F                   first
#define     S                   second
#define 	vpii         		vector<pii>
#define 	vll        		    vector<ll>
#define     booste              ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;
typedef long double ld;

using namespace std;

inline ll get_num(char ch){
	if(ch == '-')return 0;
	else if(ch >= 'a' && ch <= 'z'){
		return 1 + (ch-'a');
	}
	else if(ch >= 'A' && ch <= 'Z'){
		return 27 +(ch - 'A');
	}
	else if(ch >= '0' && ch <= '9'){
		return 53 +(ch - '0');
	}
}
 int dx[] = {1,0,-1, 0} , dy[] = {0,1, 0, -1};  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
inline int GA(ll n , vector<ll>&vec){
    Rep(i ,n){ll x ;cin>>x ; vec.push_back(x);}
}
inline int GAEG(ll m ,vector<ll>vec[]){
    Rep(i ,m){ll x  , y ;cin>>x>>y ; x-- , y-- ; vec[x].push_back(y); vec[y].push_back(x);}

}

ll dis[2000001];
ll pr[2000001];
int main()
{
   // Test;
    ll n , k;
    cin>>n>>k;
    For(i ,0, k){
        dis[i+1]=1;
    }
    ll cnt= 1;
    For(i ,k+1 , n)
    {
        dis[cnt]++;
        cnt++;
        if(cnt==k+1)
            cnt%=k;
    }
    if(cnt==2) cout<<dis[1]+dis[k]<<endl;
    else if(cnt==1)cout<<dis[k]+dis[k-1]<<endl;
    else cout<<dis[cnt-1]+dis[cnt-2]<<endl;
    Rep(i ,k){
        cout<<1<<" "<<i+2<<endl;
    }
    cnt =1;
    Rep(i ,n-k){
        cout<<cnt<<" "<<i+k+2<<endl;
        cnt++;
        if(cnt==k+1) cnt%=k;
    }
    return 0;

}


//include <god>
/*                   ___    ____    ___    ___
      /\     |        |    |____|  |         /     /\
     /__\    |        |    |  \    |---     /     /__\
    /    \   |___    _|_   |   \   |___    /__   /    \
*/
#include <bits/stdc++.h>

#define 	Time 	        	printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define 	For(J,R,K)       	for(ll J=R;J<K;++J)
#define 	Rep(I,N)         	For(I,0,N)
#define 	MP 		        	make_pair
#define 	ALL(X)       		(X).begin(),(X).end()
#define 	SF 	        		scanf
#define 	PF 		        	printf
#define 	pii         		pair<long long,long long>
#define 	piii         		pair<pii , long long >
#define 	pdd         		pair<double , double>
#define 	Sort(v) 	        sort(ALL(v))
#define     GSORT(x)            sort(ALL(x), greater<typeof(*((x).begin()))>())
#define 	Test 		        freopen("a.in","r",stdin)
#define 	Testout 	        freopen("a.out","w",stdout)
#define     UNIQUE(v)           Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define 	pb 		        	push_back
#define 	Set(a,n)        	memset(a,n,sizeof(a))
#define 	MAXN 		        100000+99
#define 	EPS 		        1e-10
#define 	inf 		        1ll<<62
#define     SF                  scanf
#define     PF                  printf
#define     F                   first
#define     S                   second
#define 	vpii         		vector<pii>
#define 	vll        		    vector<ll>
#define     booste              ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;
typedef long double ld;

using namespace std;

inline ll get_num(char ch){
	if(ch == '-')return 0;
	else if(ch >= 'a' && ch <= 'z'){
		return 1 + (ch-'a');
	}
	else if(ch >= 'A' && ch <= 'Z'){
		return 27 +(ch - 'A');
	}
	else if(ch >= '0' && ch <= '9'){
		return 53 +(ch - '0');
	}
}
 int dx[] = {1,0,-1, 0} , dy[] = {0,1, 0, -1};  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
inline int GA(ll n , vector<ll>&vec){
    Rep(i ,n){ll x ;cin>>x ; vec.push_back(x);}
}
inline int GAEG(ll m ,vector<ll>vec[]){
    Rep(i ,m){ll x  , y ;cin>>x>>y ; x-- , y-- ; vec[x].push_back(y); vec[y].push_back(x);}

}

ll dis[2000001];
ll pr[2000001];
int main()
{
//    Test;
    ll n , k;
    cin>>n>>k;
    For(i ,0, k){
        dis[i+1]=1;
    }
    ll cnt= 1;
    For(i ,k+1 , n)
    {
        dis[cnt]++;
        cnt++;
        if(cnt==k+1)
            cnt%=k;
    }
    if(cnt==2) cout<<dis[1]+dis[k]<<endl;
    else if(cnt==1)cout<<dis[k]+dis[k-1]<<endl;
    else cout<<dis[cnt-1]+dis[cnt-2]<<endl;
    Rep(i ,k){
        cout<<1<<" "<<i+2<<endl;
    }
    cnt =2;
    Rep(i ,n-k-1){
        cout<<i+k+2<<" "<<cnt++<<endl;
    }
    return 0;

}


//include <god>
/*                   ___    ____    ___    ___
      /\     |        |    |____|  |         /     /\
     /__\    |        |    |  \    |---     /     /__\
    /    \   |___    _|_   |   \   |___    /__   /    \
*/
#include <bits/stdc++.h>

#define 	Time 	        	printf("\nTime : %.3lf s.\n", clock()*1.0/CLOCKS_PER_SEC)
#define 	For(J,R,K)       	for(ll J=R;J<K;++J)
#define 	Rep(I,N)         	For(I,0,N)
#define 	MP 		        	make_pair
#define 	ALL(X)       		(X).begin(),(X).end()
#define 	SF 	        		scanf
#define 	PF 		        	printf
#define 	pii         		pair<long long,long long>
#define 	piii         		pair<pii , long long >
#define 	pdd         		pair<double , double>
#define 	Sort(v) 	        sort(ALL(v))
#define     GSORT(x)            sort(ALL(x), greater<typeof(*((x).begin()))>())
#define 	Test 		        freopen("a.in","r",stdin)
#define 	Testout 	        freopen("a.out","w",stdout)
#define     UNIQUE(v)           Sort(v); (v).resize(unique(ALL(v)) - (v).begin())
#define 	pb 		        	push_back
#define 	Set(a,n)        	memset(a,n,sizeof(a))
#define 	MAXN 		        100000+99
#define 	EPS 		        1e-10
#define 	inf 		        1ll<<62
#define     SF                  scanf
#define     PF                  printf
#define     F                   first
#define     S                   second
#define 	vpii         		vector<pii>
#define 	vll        		    vector<ll>
#define     booste              ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;
typedef long double ld;

using namespace std;

inline ll get_num(char ch){
	if(ch == '-')return 0;
	else if(ch >= 'a' && ch <= 'z'){
		return 1 + (ch-'a');
	}
	else if(ch >= 'A' && ch <= 'Z'){
		return 27 +(ch - 'A');
	}
	else if(ch >= '0' && ch <= '9'){
		return 53 +(ch - '0');
	}
}
 int dx[] = {1,0,-1, 0} , dy[] = {0,1, 0, -1};  // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
inline int GA(ll n , vector<ll>&vec){
    Rep(i ,n){ll x ;cin>>x ; vec.push_back(x);}
}
inline int GAEG(ll m ,vector<ll>vec[]){
    Rep(i ,m){ll x  , y ;cin>>x>>y ; x-- , y-- ; vec[x].push_back(y); vec[y].push_back(x);}

}

ll dis[2000001];
ll pr[2000001];
int main()
{
   // Test;
    ll n , k;
    cin>>n>>k;
    For(i ,0, k){
        dis[i+1]=1;
    }
    ll cnt= 1;
    For(i ,k+1 , n)
    {
        dis[cnt]++;
        cnt++;
        if(cnt==k+1)
            cnt%=k;
    }
    if(cnt==2) cout<<dis[1]+dis[k]<<endl;
    else if(cnt==1)cout<<dis[k]+dis[k-1]<<endl;
    else cout<<dis[cnt-1]+dis[cnt-2]<<endl;
    Rep(i ,k){
        cout<<1<<" "<<i+2<<endl;
    }
    cnt =2;
    Rep(i ,n-k-1){
        cout<<cnt<<" "<<i+k+2<<endl;
        cnt++;
        if(cnt==k+2) cnt=2;
    }
    return 0;

}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	int x = (n - 1) / k;
	if ((n - 1) % k != 0)x++;
	int put = 2;

	int y = (n - 1) % k;
	if (y == 1)cout << 2 * x - 1 << endl;
	else cout << 2 * x << endl;
	if (y == 0)x++;
	for (int i = 0; i < k; i++) {
		if (y <= 0) {
			y = 10;
			x--;
		}
		else {
			y--;
		}
		for (int j = 0; j < x; j++)adj[i].pb(put++);
	}
	for (int i = 0; i < k; i++) {
		cout << 1 << " " << adj[i][0] << endl;
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	int x = (n - 1) / k;
	if ((n - 1) % k != 0)x++;
	int put = 2;

	int y = (n - 1) % k;
	if (y == 1)cout << 2 * x - 1 << endl;
	else cout << 2 * x << endl;
	if (y == 0)x++;
	for (int i = 0; i < k; i++) {
		if (y <= 0) {
			y = 1000000;
			x--;
		}
		else {
			y--;
		}
		for (int j = 0; j < x; j++)adj[i].pb(put++);
	}
	for (int i = 0; i < k; i++) {
		cout << 1 << " " << adj[i][0] << endl;
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		if (put > k)put = 1;
	}
	int ans = 0;
	vi b;
	int c = 0;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
		if (sz(adj[i]) == sz(adj[0]))c++;
	}
	sort(rall(b));
	cout << max(c - 1 + b[0] - 1 + b[k - 1], b[0] - 1 + b[1] - 1 + 1) << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		put %= k;
	}
	int ans = 0;
	vi b;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
	}
	sort(rall(b));
	cout << b[0] - 1 + b[1] - 1 + 1 << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[i - 1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		if (put > k)put = 1;
	}
	int ans = 0;
	vi b;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
	}
	sort(rall(b));
	cout << b[0] - 1 + b[1] - 1 + 1 << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[i - 1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		if (put > k)put = 1;
	}
	int ans = 0;
	vi b;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
	}
	sort(rall(b));
	cout << max(k - 1 + b[0] - 1 + b[k - 1] - 1, b[0] - 1 + b[1] - 1 + 1) << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		if (put > k)put = 1;
	}
	int ans = 0;
	vi b;
	int c = 0;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
		if (sz(adj[i]) == sz(adj[1]))c++;
	}
	cout << max(c - 1 + b[0] - 1 + b[k - 1] - (c == k), b[0] - 1 + b[1] - 1 + 1) << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	int x = n / k;
	if (n % k != 0)x++;
	int put = 2;

	int y = n % k;
	y = k - y;
	if (y == 1)cout << 2 * x - 1 << endl;
	else cout << 2 * x << endl;
	for (int i = 0; i < k; i++) {
		if (y <= 0) {
			y = 10;
			x--;
		}
		else {
			y--;
		}
		for (int j = 0; j < x; j++)adj[i].pb(put++);
	}
	for (int i = 0; i < k; i++) {
		cout << 1 << " " << adj[i][0] << endl;
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	for (int i = 1; i <= k; i++) {
		adj[i].pb(i);
	}
	int put = 1;
	for (int i = k + 1; i <= n; i++) {
		adj[put++].pb(i);
		if (put > k)put = 1;
	}
	int ans = 0;
	vi b;
	for (int i = 1; i <= k; i++) {
		b.pb(sz(adj[i]));
	}
	sort(rall(b));
	cout << b[0] - 1 + b[1] - 1 + 1 << endl;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}
	for (int i = 2; i <= k; i++) {
		cout << adj[1].back() << " " << adj[i].back() << endl;
	}

	return 0;
}


// Best practice

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// #include <functional> // for less

#define int         long long
#define pb          push_back
#define pf          emplace_front
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define pii 		pair<int,int>
#define psi 		pair<string,int>
#define vi 			vector<int>
#define vpii  		vector<pii>
#define vvi 		vector<vi>
#define sz(x)	    (int)(x).size()
#define x           first
#define y           second
#define endl        '\n'
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         1000000007
#define hell        998244353
#define output(x)   cout << (x ? "YES" : "NO")<<endl;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;

// #define fbo find_by_order
// #define ook order_of_key

// Code from Here -------------------------------------------------------------------------------

signed main() {
	tezz

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	vector<vi> adj(k + 1);
	int x = (n - 1) / k;
	if ((n - 1) % k != 0)x++;
	int put = 2;

	int y = (n - 1) % k;
	if (y == 1)cout << 2 * x - 1 << endl;
	else cout << 2 * x << endl;
	x++;
	for (int i = 0; i < k; i++) {
		if (y <= 0) {
			y = 10;
			x--;
		}
		else {
			y--;
		}
		for (int j = 0; j < x; j++)adj[i].pb(put++);
	}
	for (int i = 0; i < k; i++) {
		cout << 1 << " " << adj[i][0] << endl;
		for (int j = 1; j < sz(adj[i]); j++) {
			cout << adj[i][j - 1] << " " << adj[i][j] << endl;
		}
	}

	return 0;
}


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

int n,k,dp[N],dep[N],mxdep[N];
vector<int> v[N];

void dfs(int u,int f)
{
	mxdep[u]=dep[u];
	int max1=0,max2=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int to=v[u][i];
		if(to==f) continue;
		dep[to]=dep[u]+1;
		dfs(to,u);
		mxdep[u]=max(mxdep[u],mxdep[to]);
		int dis=mxdep[to]-dep[u];
		if(max1<dis) max2=max1,max1=dis;
		else if(max2<dis) max2=dis;
	}
	dp[u]=max(max1,max1+max2);
	//cout<<u<<" "<<max1<<" "<<max2<<endl;
}

int main()
{
	scanf("%d%d",&n,&k);
	int cnt=1,now=1;
	while(cnt<n)
	{
		if(now==1) for(int i=cnt+1;i<=min(cnt+k,n);i++) v[now].push_back(i);
		else v[now].push_back(cnt+1);
		if(now==1) cnt+=k;
		else cnt++;
		now++;
	}
	dfs(1,-1);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		//for(int j=0;j<(int)v[i].size();j++) cout<<v[i][j]<<" "; cout<<endl;
		//cout<<i<<" "<<dp[i]<<endl;
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<(int)v[i].size();j++)
			printf("%d %d\n",i,v[i][j]);
	}
}


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

int n,k,dp[N],dep[N],mxdep[N];
vector<int> v[N];

void dfs(int u,int f)
{
	mxdep[u]=dep[u];
	int max1=0,max2=0;
	for(int i=0;i<(int)v[u].size();i++)
	{
		int to=v[u][i];
		if(to==f) continue;
		dep[to]=dep[u]+1;
		dfs(to,u);
		mxdep[u]=max(mxdep[u],mxdep[to]);
		int dis=mxdep[to]-dep[u];
		if(max1<dis) max2=max1,max1=dis;
		else if(max2<dis) max2=dis;
	}
	dp[u]=max(max1,max1+max2);
	//cout<<u<<" "<<max1<<" "<<max2<<endl;
}

int main()
{
	scanf("%d%d",&n,&k);
	int cnt=1,now=1;
	while(cnt<n)
	{
		for(int i=cnt+1;i<=min(cnt+k,n);i++) v[now].push_back(i);
		cnt+=k;
		now++;
	}
	dfs(1,-1);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		//for(int j=0;j<(int)v[i].size();j++) cout<<v[i][j]<<" "; cout<<endl;
		//cout<<i<<" "<<dp[i]<<endl;
		ans=max(ans,dp[i]);
	}
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<(int)v[i].size();j++)
			printf("%d %d\n",i,v[i][j]);
	}
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <iomanip>
#include <cassert>
#include <unordered_map>
#include <random>
#include <unordered_set>

#define all(x) x.begin(), x.end()
#define siz(x) (int)(x.size())

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
int n, k;
bool ok(int len) {
    int have = n - len - k + 2;
    int mx = (len+1) / 2 - 2;
    //cout << "cnt of vrts: " << len << ", ost: " << have << "\n";
    for (int i = 0; i < k - 2; ++i) {
        int d = max(0, min(have, mx));
       // cout << d << " vert for lev\n";
        have -= d;
    }
    return have == 0;
}
int bins() {
    int l = 2, r = n - k + 2, mi = 0;
    while (r - l > 1) {
        mi = (r + l) / 2;
        if (ok(mi))r = mi;
        else l = mi;
    }
    return r;
}
void print(vector<int>& a) {
    for (int x : a)cout << x << " ";
    cout << "\n";
}
void fill(vector<int>& fre, vector<int>& lev) {
    for (int i = 1; i <= n; ++i) {
        if (lev.size() < k)lev.push_back(i);
        else fre.push_back(i);
    }
}
int pop(vector<int>& a) {
    int x = a.back();
    a.pop_back();
    return x;
}
int main() {
    cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0);
    cin >> n >> k;
   // cout << ok(5);
   // exit(0);
    int len =  bins();
    vector<int>fre, lev;
    fill(fre, lev);
    //cout << "lev: "; print(lev);
    cout << len - 1 << "\n";
    vector<int>ord(1, pop(lev));
    while (ord.size() + 1 < len) {
        ord.push_back(pop(fre));
    }
    ord.push_back(pop(lev));
    //cout << "ord: "; print(ord);
    int have = n - len - k + 2;
    int md = len / 2;
    for (int x : lev) {
        int cnt = min(have, md - 1);
        int v = ord[md];
        have -= cnt;
        //cout << "for leave " << x << " need " << cnt << " vertices\n";
        while (cnt > 0) {
            cout << v << " " << fre.back() << "\n";
            v = pop(fre);
            --cnt;
        }
        cout << v << " " << x << "\n";
    }
    for (int i = 1; i < ord.size(); ++i)cout << ord[i] << " " << ord[i - 1] << "\n";
    return 0;
}/*
 7 3
 6 3

 5 3

 */


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <iomanip>
#include <cassert>
#include <unordered_map>
#include <random>
#include <unordered_set>

#define all(x) x.begin(), x.end()
#define siz(x) (int)(x.size())

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
int n, k;
bool ok(int diam) {
    int have = n - diam - k + 2;
    int md = (diam) / 2;
    //cout << "cnt of vrts: " << diam << ", ost: " << have << "\n";
    for (int i = 0; i < k - 2; ++i) {
        int d = min(have, md - 1);
        // cout << d << " vert for lev\n";
        have -= d;
    }
    return have == 0;
}
int bins() {
    int l = 2, r = n - k + 2, mi = 0;
    while (r - l > 1) {
        mi = (r + l) / 2;
        if (ok(mi))r = mi;
        else l = mi;
    }
    return r;
}
void print(vector<int>& a) {
    for (int x : a)cout << x << " ";
    cout << "\n";
}
void fill(vector<int>& fre, vector<int>& lev) {
    for (int i = 1; i <= n; ++i) {
        if (lev.size() < k)lev.push_back(i);
        else fre.push_back(i);
    }
}
int pop(vector<int>& a) {
    int x = a.back();
    a.pop_back();
    return x;
}
int main() {
    cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0);
    cin >> n >> k;
    // cout << ok(5);
    // exit(0);
    int len = bins();
    vector<int>fre, lev;
    fill(fre, lev);
    //cout << "lev: "; print(lev);
    vector<int>ord(1, pop(lev));
    while (ord.size() + 1 < len) {
        ord.push_back(pop(fre));
    }
    ord.push_back(pop(lev));
    vector<pll>ans;
    //cout << "ord: "; print(ord);
    int have = n - len - k + 2;
    int md = len / 2;
    for (int x : lev) {
        int cnt = min(have, md - 1);
        int v = ord[md];
        have -= cnt;
        //cout << "for leave " << x << " need " << cnt << " vertices\n";
        while (cnt > 0) {
            ans.push_back({ v, fre.back() });
            v = pop(fre);
            --cnt;
        }
        ans.push_back({ v, x });
    }
    for (int i = 1; i < ord.size(); ++i)ans.push_back({ ord[i], ord[i - 1] });
    //cout << (ans.size() == n - 1);
    assert(siz(ans) == n - 1);
    cout << len - 1 << "\n";
    for (auto pp : ans)cout << pp.first << " " << pp.second << "\n";
    return 0;
}/*
 7 3
 6 3

 5 3

 */