#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int l = (n - 1) / m;
  if ((n - 1) % m == 0)
    printf("%d\n", 2 * l);
  else
    printf("%d\n", 2 * l + 1 + ((n - 1) % m >= 2));
  int need = (n - 1) % m;
  for (int i = 2; i <= n;) {
    int maxn = min(n, i + l - 1 + (need-- > 0));
    printf("%d %d\n", 1, i);
    int j = i;
    while (j++ < maxn) printf("%d %d\n", j - 1, j);
    i = maxn + 1;
  }
  return 0;
}
