#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200010];
int p[200010];
int c[200010];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 2; i <= k + 1; i++) {
    p[i] = 1;
    c[i] = i;
  }
  int x = n - k - 1;
  int cont = k + 2;
  int ans = 2;
  if (x) {
    int y = x / k;
    int z = x % k;
    ans += 2 * y;
    if (z >= 2)
      ans += 2;
    else if (z)
      ans++;
    for (int i = 2; i <= k + 1 and z; i++, z--) {
      p[cont] = c[i];
      c[i] = cont;
      cont++;
    }
    for (int i = 2; i <= k + 1; i++) {
      int aux = y;
      while (aux--) {
        p[cont] = c[i];
        c[i] = cont;
        cont++;
      }
    }
  }
  cout << ans << "\n";
  for (int i = 1; i < cont; i++) {
    if (p[i] != 0) {
      cout << i << " " << p[i] << "\n";
    }
  }
}
