#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int r = 1;
  int sz = (n - 1) / k;
  int rem = (n - 1) % k;
  int an = 0;
  if (rem == 0) {
    an = 2 * sz;
  } else if (rem == 1) {
    an = 2 * sz + 1;
  } else {
    an = 2 * sz + 2;
  }
  int c = 2;
  for (int i = 0; i < k && c <= n; i++) {
    int p = r;
    if (rem) {
      int cc = sz + 1;
      for (int j = 0; j < cc && c <= n; j++) {
        v.push_back({p, c});
        p = c;
        c++;
      }
      rem--;
    } else {
      int cc = sz;
      for (int j = 0; j < cc && c <= n; j++) {
        v.push_back({p, c});
        p = c;
        c++;
      }
    }
  }
  cout << an << endl;
  for (auto it : v) cout << it.first << " " << it.second << endl;
  return 0;
}
