#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long double pai = acos(-1);
int n, k;
int last[200009];
int main() {
  cin >> n >> k;
  vector<pair<int, int> > ans;
  int it = 0, num = 0;
  for (int i = 1; i < n; i++) {
    num += (it <= 1);
    ans.push_back({last[it], i});
    last[it] = i;
    it++;
    if (it == k) it = 0;
  }
  cout << num << endl;
  for (auto u : ans) cout << u.first + 1 << " " << u.second + 1 << endl;
}
