#include <bits/stdc++.h>
const long long mod = (long long)1e9 + 7;
using namespace std;
long long n, k;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  queue<long long> qu;
  vector<pair<long long, long long> > ans;
  vector<long long> arr;
  unordered_map<long long, long long> mp;
  long long p = 2;
  for (long long i = 0; i < k; i++) {
    ans.push_back({1, p});
    qu.push(p);
    mp[p] = 1;
    p++;
    arr.push_back(1);
  }
  long long c = k + 1;
  while (c != n) {
    long long pp = qu.front();
    qu.pop();
    ans.push_back({pp, p});
    qu.push(p);
    mp[p] = mp[pp] + 1;
    arr.push_back(mp[p]);
    c++;
    p++;
  }
  sort(arr.begin(), arr.end());
  n = arr.size();
  cout << arr[n - 1] + arr[n - 2] << "\n";
  for (auto i : ans) {
    cout << i.first << ' ' << i.second << "\n";
  }
  return 0;
}
