#include <bits/stdc++.h>
using namespace std;
int r[3000009];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int ma = ((n - 1) / k) * 2;
  if ((n - 1) % k >= 2)
    ma += 2;
  else if ((n - 1) % k == 1)
    ma += 1;
  printf("%d\n", ma);
  for (int i = 1; i <= k; i++) {
    r[i] = i + 1;
    printf("1 %d\n", i + 1);
  }
  n -= k + 1;
  int nowr, now;
  now = k + 2, nowr = 1;
  int a[10], sum = 0;
  for (int i = 1; i <= 5; i++) {
    sum += a[i];
    if (sum == 2) break;
  }
  while (n) {
    printf("%d %d\n", r[nowr], now);
    r[nowr++] = now;
    now++;
    nowr = (nowr - 1) % k + 1;
    n--;
    if (n == 0) break;
  }
  return 0;
}
