#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  printf("%d\n", 2 * ((N - 1) / K) + min((N - 1) % K, 2));
  for (int i = 0; i < K; i++) {
    int start = i * (long long int)(N - 1) / K + 2;
    int end = (i + 1) * (long long int)(N - 1) / K + 2;
    printf("%d %d\n", 1, start);
    for (int j = start + 1; j < end; j++) printf("%d %d\n", j - 1, j);
  }
}
