#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  if (k == 1) {
    cout << 2 << endl;
    for (int i = 2; i < n + 1; i++) cout << 1 << " " << i << endl;
  } else {
    int a = k;
    int b = n - k - 1;
    int q = b / a;
    if (b % a == 0)
      cout << 2 * (q + 1) << endl;
    else if (b % a == 1)
      cout << 1 + 2 * (q + 1) << endl;
    else if (b % a >= 2)
      cout << 2 + 2 * (q + 1) << endl;
    int v[a];
    for (int i = 1; i < k + 1; i++) {
      cout << i + 1 << " 1" << endl;
      v[i - 1] = i;
    }
    int cnt = 0;
    for (int i = k + 1; i < n; i++) {
      cout << i + 1 << " " << v[cnt] + 1 << endl;
      v[cnt] = i;
      cnt++;
      cnt %= k;
    }
  }
}
