#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000011];
queue<int> q;
int n, k;
long long bfs() {
  q.push(n);
  vector<long long> dis(1000011, 2000000001);
  dis[n] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i];
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
  return dis[1] + dis[2];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  bool f = 0;
  for (int i = 1; i < n; i += k) {
    if (i + k >= n) f = 1;
    for (int j = i; j < i + k && j < n; j++) {
      if (!q.empty()) {
        int x = q.front();
        q.pop();
        adj[j].push_back(x);
      }
    }
    for (int j = i; j < i + k && j < n; j++) {
      q.push(j);
    }
    if (f) {
      for (int j = i + k; j < n; j++) {
        if (!q.empty()) {
          adj[j].push_back(q.front());
          q.pop();
        }
      }
      for (int j = i + k; j < n; j++) q.push(j);
    }
  }
  while (!q.empty()) {
    adj[n].push_back(q.front());
    q.pop();
  }
  cout << bfs() << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      cout << i << " " << adj[i][j] << endl;
    }
  }
  return 0;
}
