#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<int> vec;
  for (int i = 1; i <= k; i++) vec.push_back(i);
  int cnt = k, h = 0;
  n -= k;
  while (n) {
    h++;
    if (n <= k) {
      h = 2 * h;
      h += min(n - 1, 2);
      queue<int> temp;
      for (int j = 0; j < n - 1; j++) q.push({++cnt, vec[j]}), temp.push(cnt);
      ++cnt;
      for (int j = n - 1; j < k; j++) q.push({cnt, vec[j]});
      while (!temp.empty()) q.push({cnt, temp.front()}), temp.pop();
      n = 0;
    } else {
      vector<int> temp;
      for (int i = 0; i < k; i++) temp.push_back(++cnt), q.push({cnt, vec[i]});
      vec = temp;
      n -= k;
    }
  }
  printf("%d\n", h);
  while (!q.empty())
    printf("%d %d\n", q.front().first, q.front().second), q.pop();
  return 0;
}
