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