#include <bits/stdc++.h>
using namespace std;
int e[200009];
int main() {
  int n, k, lvl = 0;
  cin >> n >> k;
  int on = n, sn = n - k - 1;
  while (sn >= k) {
    for (int i = n; i > n - k; i--) {
      e[i] = i - k;
    }
    lvl++;
    sn -= k;
    n -= k;
  }
  if (sn > 1) {
    for (int i = n; i > n - k; i--) {
      e[i] = (i % sn) + 2;
    }
    for (int i = 0; i < sn; i++) {
      e[i + 2] = 1;
    }
    cout << lvl * 2 + 4 << endl;
  }
  if (sn == 1) {
    for (int i = n; i > n - k; i--) {
      e[i] = (i % 2) + 1;
    }
    e[2] = 1;
    cout << lvl * 2 + 3 << endl;
  }
  if (sn == 0) {
    for (int i = n; i > n - k; i--) {
      e[i] = 1;
    }
    cout << lvl * 2 + 2 << endl;
  }
  for (int i = 2; i <= on; i++) {
    cout << i << " " << e[i] << endl;
  }
  return 0;
}
