#include <bits/stdc++.h>
using namespace std;
int n, child, node, P[200005];
void Print() {
  for (int i = 2; i <= n; i++) cout << P[i] << ' ' << i << "\n";
  exit(0);
}
int main() {
  cin >> n >> child;
  node = n - child;
  if (child >= node) {
    node--;
    int tmp = 1;
    while (child > node) {
      child--;
      P[++tmp] = 1;
    }
    if (tmp == n) {
      cout << 2 << "\n";
      Print();
    }
    int Count = 0;
    for (int i = tmp + 1; i <= n; i += 2) {
      Count++;
      P[i] = 1;
      P[i + 1] = i;
    }
    if (Count == 1)
      cout << 3 << "\n";
    else
      cout << 4 << "\n";
    Print();
  } else {
    int len = (node - 1) / child, mod = (node - 1) % child;
    if (mod == 0)
      cout << 2 * (len + 1) << "\n";
    else if (mod == 1)
      cout << 2 * (len + 1) + 1 << "\n";
    else
      cout << 2 * (len + 1) + 2 << "\n";
    for (int i = 2; i <= child + 1; i++) P[i] = 1;
    for (int i = 2; i <= n - child; i++) P[i + child] = i;
    Print();
  }
}
