#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2 * 1e5 + 2;
int v[MAX_N];
vector<int> edges[MAX_N];
int dist[MAX_N];
void DFS(int x) {
  for (int i = 0; i < edges[x].size(); ++i) {
    int y = edges[x][i];
    dist[y] = dist[x] + 1;
    DFS(y);
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  int nonExit = n - k - 1;
  int parent = 1;
  int index = 2;
  while (nonExit > 0) {
    if (!v[parent]) {
      v[parent] = 1;
    }
    edges[v[parent]].push_back(index);
    v[parent] = index;
    ++parent;
    if (parent > k) {
      parent = 1;
    }
    ++index;
    --nonExit;
  }
  for (int i = 1; i <= k; ++i) {
    if (!v[i]) {
      v[i] = 1;
    }
    edges[v[i]].push_back(index);
    ++index;
  }
  DFS(1);
  sort(dist + 1, dist + n + 1);
  printf("%d\n", dist[n - 1] + dist[n]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < edges[i].size(); ++j) {
      printf("%d %d\n", i, edges[i][j]);
    }
  }
  return 0;
}
