#include <bits/stdc++.h>
using namespace std;
string s, t[2000006];
int pos[2000006];
vector<int> v[2000006];
int main() {
  int n, k, i, j;
  cin >> n >> k;
  if (k == n - 1) {
    cout << 2 << endl;
    for (i = 2; i <= n; i++) cout << i << " " << 1 << endl;
    return 0;
  }
  if (n == 3) {
    cout << 2 << endl;
    cout << 1 << " " << 2 << endl;
    cout << 1 << " " << 3 << endl;
    return 0;
  }
  int r = 0;
  for (i = 2; i <= 2 + k - 1; i++) {
    v[r].push_back(i);
  }
  r++;
  int st = 2 + k;
  for (i = st; i <= n; i += k) {
    for (j = i; j < i + k; j++) {
      if (j > n) break;
      v[r].push_back(j);
    }
    r++;
  }
  if (v[r - 1].size() > 1)
    cout << 2 * r << endl;
  else
    cout << 2 * r - 1 << endl;
  for (i = 0; i < v[0].size(); i++) cout << 1 << " " << v[0][i] << endl;
  for (i = 0; i < r; i++) {
    for (j = 0; j < v[i].size(); j++) {
      if (v[i + 1].size() > j && v[i][j] <= n && v[i + 1][j] <= n)
        cout << v[i][j] << " " << v[i + 1][j] << endl;
    }
  }
  return 0;
}
