#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  if (k == 2) {
    cout << n - 1 << endl;
    for (int i = 1; i < n; ++i) {
      cout << i << " " << i + 1 << endl;
    }
    return 0;
  }
  int mi = floor((n - 1) / (double)k);
  int ma = ceil((n - 1) / (double)k);
  int r = (n - 1) % k;
  if (r == 1) {
    cout << mi + ma << endl;
  } else {
    cout << 2 * ma << endl;
  }
  for (int i = 2; i <= n; ++i) {
    int p = max(1, i - k);
    cout << i << " " << p << endl;
  }
  return 0;
}
