#include <bits/stdc++.h>
using namespace std;
int n, k, clc = 1;
int main() {
  scanf("%d%d", &n, &k);
  int m = n - 1, p = m / k, q = m % k, r = 1;
  if (q == 0)
    printf("%d\n", p * 2);
  else if (q == 1)
    printf("%d\n", p * 2 + 1);
  else
    printf("%d\n", p * 2 + 2);
  for (int i = 2; i <= n; i++) {
    if (r == p + 2) r = 1;
    if (r == p + 1) {
      if (q == 0)
        r = 1;
      else
        q--;
    }
    if (r == 1)
      printf("%d %d\n", 1, i);
    else
      printf("%d %d\n", i - 1, i);
    r++;
  }
}
