#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  printf("%d\n",
         (n - 2) / k + ((n - 2) % k > 0) + (n - 1) / k + ((n - 1) % k > 0));
  int l = 2;
  for (int i = 0; i < k; i++) q.push(1);
  while (l <= n) {
    printf("%d %d\n", q.front(), l);
    q.pop();
    q.push(l);
    l++;
  }
}
