#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> paths(k + 1, 0);
  int p1 = (n - 1) / k + ((n - 1) % k > 0 ? 1 : 0);
  int p2 = (n - 1) / k + ((n - 1) % k > 1 ? 1 : 0);
  ;
  cout << p1 + p2 << endl;
  for (int i = 1, j = 1; i < n; i++) {
    if (paths[j] != 0) cout << paths[j] << " " << i << endl;
    paths[j] = i;
    j++;
    if (j == k + 1) j = 1;
  }
  for (int i = 1; i <= k; i++) cout << paths[i] << " " << n << endl;
  return 0;
}
