#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long> > tot;
  if (k == n - 1) {
    cout << 2 << '\n';
    for (long long i = 1; i < k + 1; i++) {
      cout << 1 << ' ' << i + 1 << '\n';
    }
    return 0;
  }
  long long depth = (n - 1) / k;
  long long ans = 0;
  long long modVal = (n - 1) % k;
  long long total = 1;
  if (modVal == 0) {
    cout << 2 * depth;
  } else if (modVal == 1) {
    cout << 2 * depth + 1;
  } else {
    cout << 2 * (depth + 1);
  }
  cout << '\n';
  long long root = 1;
  long long ptr = 2;
  while (ptr <= k + 1) {
    cout << root << ' ' << ptr << '\n';
    ptr++;
  }
  while (ptr <= n) {
    root = ptr - k;
    for (long long i = 0; i < k + 1; i++) {
      cout << root << ' ' << ptr << '\n';
      root++;
      ptr++;
      if (ptr > n) {
        break;
      }
    }
  }
  return 0;
}
