#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 9;
const double pi = 3.14159265358979323846264;
const int inf = 2e9;
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int tmp = (n - 1) / k;
  int jk = (n - 1) % k;
  if (jk == 0)
    cout << 2 * tmp << endl;
  else if (jk == 1)
    cout << 2 * tmp + 1 << endl;
  else
    cout << 2 * tmp + 2 << endl;
  int kl = 2;
  for (int i = 0; i < jk; ++i) {
    cout << 1 << " " << kl++ << endl;
    for (int j = 0; j < tmp; ++j) {
      cout << kl - 1 << " " << kl << endl;
      kl++;
    }
  }
  for (int i = jk; i < k; ++i) {
    cout << 1 << " " << kl++ << endl;
    for (int j = 0; j < tmp - 1; ++j) {
      cout << kl - 1 << " " << kl << endl;
      kl++;
    }
  }
  return 0;
}
