#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
vector<pair<int, int> > ord;
int dep[maxn], last[maxn];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  if (k == 2) {
    printf("%d\n", n - 1);
    for (int i = 1; i < n; i++) printf("%d %d\n", i, i + 1);
    return 0;
  }
  for (int i = 1; i <= k; i++) {
    dep[i] = 1;
    last[i] = i + 1;
    ord.push_back(make_pair(1, i + 1));
  }
  int cnt = n - 1 - k, idcnt = k + 1;
  while (cnt) {
    for (int i = 1; i <= k; i++) {
      dep[i]++;
      ord.push_back(make_pair(last[i], ++idcnt));
      last[i] = idcnt;
      cnt--;
      if (!cnt) break;
    }
  }
  int ans = dep[1] + dep[2];
  printf("%d\n", ans);
  for (int i = 0; i < ord.size(); i++)
    printf("%d %d\n", ord[i].first, ord[i].second);
  return 0;
}
