#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 123;
const int MOD = (int)1e9 + 7;
const int inf = (int)5e8;
const long long infll = (long long)1e17;
vector<int> g[N];
vector<int> gg[N];
vector<int> bad;
int n, k;
pair<int, int> bfs_tree(int v) {
  vector<int> dist(n, inf);
  dist[v] = 0;
  vector<int> qu = {v};
  pair<int, int> ans = {0, v};
  for (int t = 0; t < qu.size(); ++t) {
    int u = qu[t];
    for (auto to : gg[u]) {
      if (dist[to] > dist[u] + 1) {
        dist[to] = dist[u] + 1;
        qu.push_back(to);
        if (dist[to] > ans.first) {
          ans = {dist[to], to};
        }
      }
    }
  }
  return ans;
}
void solve() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < k; ++i) {
    bad.push_back(i);
  }
  int kek = 0;
  int d = k;
  while (d < n) {
    if (d + 1 != n) {
      int rem = n - d;
      if (rem > bad.size()) {
        kek += 2;
        for (int i = 0; i < bad.size(); i++) {
          g[bad[i]].push_back(d + i);
          gg[bad[i]].push_back(d + i);
          gg[d + i].push_back(bad[i]);
          bad[i] = d + i;
        }
        d += bad.size();
      } else {
        kek++;
        for (int i = 0; i < rem - 1; i++) {
          g[bad[i]].push_back(d + i);
          gg[bad[i]].push_back(d + i);
          gg[d + i].push_back(bad[i]);
          bad[i] = d + i;
        }
        d += rem - 1;
      }
    } else {
      kek += 2;
      for (int i : bad) {
        g[d].push_back(i);
        gg[d].push_back(i);
        gg[i].push_back(d);
      }
      d++;
    }
  }
  printf("%d\n", bfs_tree(bfs_tree(0).second).first);
  for (int i = 0; i < n; ++i) {
    for (auto to : g[i]) {
      printf("%d %d\n", i + 1, to + 1);
    }
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
