#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, n, k, nn;
  cin >> n >> k;
  if (k == n - 1) {
    cout << "2\n";
    for (i = 1; i < n; i++) {
      cout << i << " " << n << "\n";
    }
    return 0;
  }
  if (k == n - 2) {
    cout << "3\n";
    for (i = 1; i < k; i++) {
      cout << i << " " << n << "\n";
    }
    cout << k << " " << n - 1 << "\n";
    cout << n - 1 << " " << n << "\n";
    return 0;
  }
  if (2 * k > n) {
    cout << "4\n";
    nn = n - k;
    for (i = 2; i <= nn; i++) {
      cout << 1 << " " << i << "\n";
    }
    for (; i < 2 * nn; i++) {
      cout << i << " " << (i - nn + 1) << "\n";
    }
    for (; i <= n; i++) {
      cout << 1 << " " << i << "\n";
    }
    return 0;
  }
  for (i = 0; i < n; i++) {
    nn = n - 1;
    if (nn % k == 0) {
      nn = 2 * (nn / k);
    } else {
      if (nn % k == 1)
        nn = 2 * (nn / k) + 1;
      else
        nn = 2 * (nn / k) + 2;
    }
    cout << nn << endl;
    for (i = 1; i <= k; i++) {
      cout << i << " " << n << "\n";
    }
    for (i = k + 1; i < n; i++) {
      cout << i << " " << (i - k) << "\n";
    }
  }
}
