#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n, k, id = 2;
  cin >> n >> k;
  long long int q = (n - 1 - k) / k, r = (n - 1 - k) % k;
  long long int a[k];
  for (long long int i = 1; i < k + 1; i++) a[i - 1] = 1;
  if (r == 0)
    cout << (q + 1) * 2 << '\n';
  else if (r == 1)
    cout << (q + 1) * 2 + 1 << '\n';
  else
    cout << (q + 2) * 2 << '\n';
  for (long long int i = 0; i < q + 1; i++) {
    for (long long int i = 0; i < k; i++) {
      cout << a[i] << " " << id << '\n';
      a[i] = id;
      id++;
    }
  }
  for (long long int i = 0; i < r; i++) cout << a[i] << " " << id++ << '\n';
}
