#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > e;
queue<int> q;
int h[200010];
int main() {
  int n, k, i;
  scanf("%d%d", &n, &k);
  for (i = 2; i <= k + 1; i++) {
    e.push_back(pair<int, int>(1, i));
    h[i] = 1;
    q.push(i);
  }
  for (i = k + 2; i <= n; i++) {
    e.push_back(pair<int, int>(q.front(), i));
    h[i] = h[q.front()] + 1;
    q.pop();
    q.push(i);
  }
  sort(h + 1, h + n + 1);
  printf("%d\n", h[n] + h[n - 1]);
  for (i = 0; i < n - 1; i++) printf("%d %d\n", e[i].first, e[i].second);
  return 0;
}
