#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int ans;
  if ((n - 1) % k) {
    ans = (n - 1 + k) / k;
    ans = ans * 2 - 1;
  } else {
    ans = (n - 1) / k;
    ans = ans * 2;
  }
  int index = (n - 1) / k;
  int flag = index * k + 1;
  flag = n - flag;
  if (flag >= 2) ans++;
  cout << ans << endl;
  int next = 2;
  for (int i = 0; i < k; i++) {
    cout << "1 " << next << endl;
    for (int j = 1; j < index; j++) {
      cout << next;
      next++;
      cout << " " << next << endl;
    }
    if (flag) {
      cout << next;
      next++;
      cout << " " << next << endl;
      flag--;
    }
    next++;
  }
  return 0;
}
