#include <bits/stdc++.h>
using namespace std;
inline void getInt(int* p);
int n, k;
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n >> k;
  int ans = 0;
  int ceng = (n - 1) / k;
  if ((n - 1) % k != 0) {
    ceng++;
  }
  ans += ceng;
  if ((n - 1) % k == 1) {
    ans += max(ceng - 1, 1);
  } else {
    ans += ceng;
  }
  cout << ans << endl;
  queue<int> q;
  q.push(1);
  int x;
  x = q.front();
  q.pop();
  for (int i = 1; i <= k; i++) {
    cout << x << " " << x + i << endl;
    q.push(x + i);
  }
  int cnt = k;
  while (cnt < n - 1) {
    x = q.front();
    q.pop();
    cout << x << " " << x + k << endl;
    q.push(x + k);
    cnt++;
  }
  return 0;
}
inline void getInt(int* p) {
  char ch;
  do {
    ch = getchar();
  } while (ch == ' ' || ch == '\n');
  if (ch == '-') {
    *p = -(getchar() - '0');
    while ((ch = getchar()) >= '0' && ch <= '9') {
      *p = *p * 10 - ch + '0';
    }
  } else {
    *p = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9') {
      *p = *p * 10 + ch - '0';
    }
  }
}
