#include <bits/stdc++.h>
using namespace std;
const int N = 200007;
int last[N], lev[N];
vector<int> g[N];
vector<pair<int, int> > edges;
int dfs(int u, int par = -1, int l = 0) {
  lev[u] = l;
  int best = 0;
  for (int v : g[u]) {
    if (v == par) continue;
    best = max(best, dfs(v, u, l + 1));
  }
  return 1 + best;
}
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int cur = k, cnt = n - k;
  for (int i = 1; i <= k; ++i) last[i] = i;
  while (cnt) {
    for (int i = 1; i <= k; ++i) {
      ++cur;
      edges.push_back(make_pair(last[i], cur));
      g[last[i]].push_back(cur);
      g[cur].push_back(last[i]);
      cnt--;
      last[i] = cur;
      if (cnt == 0) break;
    }
  }
  for (int i = 1; i <= k; ++i) {
    if (last[i] == cur) continue;
    edges.push_back(make_pair(last[i], cur));
    g[last[i]].push_back(cur);
    g[cur].push_back(last[i]);
  }
  int x = dfs(1);
  for (int u = 1; u <= n; ++u) {
    if (lev[u] == x - 1) {
      cout << dfs(u) - 1 << endl;
      break;
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    cout << edges[i].first << " " << edges[i].second << endl;
  }
  return 0;
}
