#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  if (k == 2) {
    printf("%d\n", n - 1);
    for (int i = 1; i <= n - 2; i++) printf("%d %d\n", i, i + 1);
    printf("%d %d\n", n - 1, n);
    return 0;
  }
  if (k == n - 1) {
    printf("2\n");
    for (int i = 2; i <= n; i++) printf("1 %d\n", i);
    return 0;
  }
  int groupSize1 = (n - 1) / k;
  int groupSize2 = groupSize1 + 1;
  int upperGroup = (n - 1) % k;
  int lowerGroup = k - upperGroup;
  int N = 1;
  if (upperGroup == 0)
    printf("%d\n", 2 * groupSize1);
  else if (upperGroup == 1)
    printf("%d\n", groupSize1 + groupSize2);
  else
    printf("%d\n", 2 * groupSize2);
  for (int i = 1; i <= lowerGroup; i++) {
    printf("1 %d\n", ++N);
    for (int j = 1; j < groupSize1; j++) {
      printf("%d %d\n", N, N + 1);
      N++;
    }
  }
  for (int i = 1; i <= upperGroup; i++) {
    printf("1 %d\n", ++N);
    for (int j = 1; j < groupSize2; j++) {
      printf("%d %d\n", N, N + 1);
      N++;
    }
  }
}
