#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 55, mod = 1e9 + 7;
long long boo[N];
vector<pair<long long, long long> > v;
int main() {
  long long c = 2, n, k, i, j, x, y, curr1 = 1, idx = 1;
  scanf("%lld", &n);
  scanf("%lld", &k);
  for (i = 2; i <= n; i++) {
    if (i <= k + 1) {
      v.push_back(make_pair(curr1, i));
      boo[i - 1] = i;
      continue;
    }
    v.push_back(make_pair(boo[idx], i));
    boo[idx] = i;
    if (idx == 1 || idx == 2) c++;
    idx++;
    if (idx == k + 1) idx = 1;
  }
  printf("%lld\n", c);
  for (i = 0; i < v.size(); i++) {
    cout << v[i].first << " " << v[i].second << " \n";
  }
}
