#include <bits/stdc++.h>
using namespace std;
vector<int> ext;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) ext.push_back(i);
  queue<pair<int, int> > q;
  for (int i = 0; i < k; i++) q.push({k + 1, 0});
  int idx = k + 2;
  vector<pair<int, int> > ans;
  vector<int> tmp;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (idx > n && ext.size()) {
      ans.push_back({p.first, ext[ext.size() - 1]}), ext.pop_back();
      tmp.push_back(p.second + 1);
      sort(tmp.rbegin(), tmp.rend());
      if (tmp.size() > 2) tmp.pop_back();
    } else if (idx <= n)
      ans.push_back({p.first, idx}), q.push({idx++, p.second + 1});
  }
  cout << tmp[0] + tmp[1] << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << endl;
}
