#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
int n, k;
int main() {
  scanf("%d %d", &n, &k);
  if ((n - 1) % k == 0)
    printf("%d\n", (n - 1) / k * 2);
  else if ((n - 1) % k == 1)
    printf("%d\n", (n - 1) / k * 2 + 1);
  else
    printf("%d\n", (n - 1) / k * 2 + 2);
  int exm = (n - 1) % k;
  int re = 2;
  for (int j = 1; j <= k; j++) {
    int jishu = (n - 1) / k;
    if (exm > 0) {
      jishu++;
      exm--;
    }
    printf("1 %d\n", re);
    for (int i = 2; i <= jishu; i++) {
      printf("%d %d\n", re, re + 1);
      re++;
    }
    re++;
  }
  return 0;
}
