#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int ans = (n - 1) / k;
  ans *= 2;
  if ((n - 1) % k > 0) ans++;
  if ((n - 1) % k > 1) ans++;
  cout << ans << endl;
  queue<int> q;
  for (int i = 2; i <= k + 1; i++) {
    cout << 1 << " " << i << endl;
    q.push(i);
  }
  for (int i = k + 2; i <= n; i++) {
    cout << i << " " << q.front() << endl;
    q.pop();
    q.push(i);
  }
  return 0;
}
