#include <bits/stdc++.h>
double eps = 1e-9;
using namespace std;
int ans, n, k;
vector<int> last, g[200001];
int main() {
  cin >> n >> k;
  int left = n - k;
  int next = k + 1;
  while (left > k) {
    for (int i = k - 1; i >= 0; i--) {
      g[next - i - 1].push_back(next + i);
      left--;
    }
    next += k;
    ans += 2;
  }
  for (int i = next - k; i < next; i++) last.push_back(i);
  ans += 2;
  if (left > 2) ans += 2;
  if (left == 2) ans++;
  for (int i = 1; i < left; i++) {
    g[last[i - 1]].push_back(next);
    last[i - 1] = next;
    next++;
  }
  for (int i = 0; i < last.size(); i++) {
    g[last[i]].push_back(next);
  }
  cout << ans << endl;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < g[i].size(); j++) cout << i << " " << g[i][j] << endl;
}
