#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline int mul_mod(int a, int b, int mo) {
  int ret;
  __asm__ __volatile__("\tmull %%ebx\n\tdivl %%ecx\n"
                       : "=d"(ret)
                       : "a"(a), "b"(b), "c"(mo));
  return ret;
}
const int mod = 1000000007;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f;
const int MAXN = 200000 + 30;
int n, m, k;
int num[MAXN];
int main() {
  scanf("%d%d", &n, &k);
  int m = n - k;
  int left = m - 1;
  int av = left / k;
  int one = 0;
  int ans;
  if (left - av * k == 1)
    ans = 2 * (av + 1) + 1;
  else if (left - av * k > 1)
    ans = 2 * (av + 2);
  else
    ans = 2 * (av + 1);
  printf("%d\n", ans);
  for (int i = 1; i <= k; ++i) num[i] = 1;
  int cnt = 1;
  int have = 1;
  for (int i = 1; i <= av; ++i, have += k) {
    for (int j = 1; j <= k; ++j) {
      printf("%d %d\n", ++cnt, num[j]), num[j] = cnt;
    }
  }
  for (int j = 1; have < m && j <= k; ++j, have++) {
    printf("%d %d\n", ++cnt, num[j]), num[j] = cnt;
  }
  for (int j = 1; j <= k; ++j) printf("%d %d\n", ++cnt, num[j]);
  return 0;
}
