#include <bits/stdc++.h>
using namespace std;
int n, k, tot;
vector<pair<int, int> > edge;
inline void link(int u, int t) {
  int c = u;
  while (t >= 1) {
    edge.emplace_back(c, ++tot);
    c = tot;
    t--;
  }
  edge.emplace_back(c, k + 1);
}
int main() {
  scanf("%d%d", &n, &k);
  int center = k + 1, avg = (n - k - 1) / k;
  int more = n - k - 1 - avg * k;
  tot = k + 1;
  int cnt1 = 0;
  for (int i = 1; i <= more; ++i) {
    link(i, avg + 1);
    cnt1++;
  }
  for (int i = more + 1; i <= k; ++i) {
    link(i, avg);
  }
  int d;
  if (cnt1 > 1)
    d = 2 * (avg + 2);
  else if (cnt1 == 1)
    d = 2 * avg + 3;
  else
    d = 2 * avg + 2;
  printf("%d\n", d);
  for (auto g : edge) {
    printf("%d %d\n", g.first, g.second);
  }
  return 0;
}
