#include <bits/stdc++.h>
using namespace std;
struct Node {
  int to, next;
} e[200010 << 1];
int ne, head[200010], dis[200010], a[200010], n, k;
queue<int> q;
void ins(int u, int v) {
  ne++;
  e[ne].to = v;
  e[ne].next = head[u];
  head[u] = ne;
}
void insert(int u, int v) {
  ins(u, v);
  ins(v, u);
}
int bfs(int x) {
  q.push(x);
  for (int i = 1; i <= n; i++) dis[i] = -1;
  dis[x] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i; i = e[i].next) {
      int v = e[i].to;
      if (dis[v] == -1) {
        dis[v] = dis[now] + 1;
        q.push(v);
      }
    }
  }
  int ret = x;
  for (int i = 1; i <= n; i++)
    if (dis[i] > dis[ret]) ret = i;
  return ret;
}
void dfs(int x, int fa) {
  for (int i = head[x]; i; i = e[i].next) {
    if (e[i].to == fa) continue;
    printf("%d %d\n", x, e[i].to);
    dfs(e[i].to, x);
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) a[i] = i;
  for (int i = k + 1; i < n; i++) {
    int now = (i % k == 0) ? k : i % k;
    insert(i, a[now]);
    a[now] = i;
  }
  for (int i = 1; i <= k; i++) {
    insert(n, a[i]);
  }
  int x = bfs(1);
  int y = bfs(x);
  printf("%d\n", dis[y]);
  dfs(1, 0);
  return 0;
}
