#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 200005;
long long n, k;
vector<long long> G[N];
void dfs(int u, int fa) {
  for (auto to : G[u])
    if (to != fa) {
      printf("%d %d\n", u, to);
      dfs(to, u);
    }
}
int main() {
  scanf("%lld %lld", &n, &k);
  long long cnt = 1, j;
  while (cnt < n) {
    for (j = 1; j <= k, cnt + j <= n; ++j) {
      long long u = max(1LL, cnt + j - k);
      G[u].push_back(cnt + j);
      G[cnt + j].push_back(u);
    }
    cnt = cnt + j;
  }
  long long ans = (n - 1 + k - 1) / k * 2;
  if (n - 1 - k * (ans / 2 - 1) == 1) --ans;
  printf("%lld\n", ans);
  dfs(1, 0);
  return 0;
}
