#include <bits/stdc++.h>
using namespace std;
int n, k, m;
int main() {
  int x, num = 1;
  scanf("%d%d", &n, &k);
  m = (n - 1) / k;
  x = (n - 1) % k;
  if (x == 0)
    cout << m * 2 << endl;
  else if (x == 1)
    cout << m * 2 + 1 << endl;
  else
    cout << m * 2 + 2 << endl;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < m + 1; j++) {
      if (j == 0) {
        cout << 1 << " " << num + 1 << endl;
        num++;
      } else {
        cout << num << " " << num + 1 << endl;
        num++;
      }
    }
  }
  for (int i = x; i < k; i++) {
    for (int j = 0; j < m; j++) {
      if (j == 0) {
        cout << 1 << " " << num + 1 << endl;
        num++;
      } else {
        cout << num << " " << num + 1 << endl;
        num++;
      }
    }
  }
  return 0;
}
