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