#include <bits/stdc++.h>
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int depth;
  int remain;
  depth = (n - 1) / k;
  remain = n - depth * k - 1;
  depth--;
  int pos = 2;
  if (remain == 0) {
    printf("%d\n", depth * 2 + 2);
  } else if (remain == 1) {
    printf("%d\n", depth * 2 + 3);
  } else {
    printf("%d\n", depth * 2 + 4);
  }
  for (int i = 0; i < remain; i++) {
    printf("1 %d\n", pos++);
    for (int j = 0; j <= depth; j++) {
      printf("%d %d\n", pos - 1, pos);
      pos++;
    }
  }
  for (int i = remain; i < k; i++) {
    printf("1 %d\n", pos++);
    for (int j = 0; j < depth; j++) {
      printf("%d %d\n", pos - 1, pos);
      pos++;
    }
  }
}
