#include <bits/stdc++.h>
using namespace std;
vector<int> g[200000];
int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  int currN = n - k - 1;
  int ans = 0;
  for (int i = 1; i <= k; i++) {
    g[0].push_back(i);
  }
  ans = 2;
  int l = 1;
  int r = k;
  while (r != n - 1) {
    int currL = l;
    for (int i = r + 1; i <= min(n - 1, r + k); i++) {
      g[currL].push_back(i);
      currL++;
      if (i == r + 1) ans++;
      if (i == r + 2) ans++;
    }
    l = r + 1;
    r = min(n - 1, r + k);
  }
  cout << ans << "\n";
  for (int i = 0; i < n; i++) {
    for (int j : g[i]) {
      cout << i + 1 << " " << j + 1 << "\n";
    }
  }
  return 0;
}
