#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
vector<int> depths;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  priority_queue<pair<int, int> > pq;
  int n, k;
  cin >> n >> k;
  for (int i = 2; i <= k + 1; i++) {
    pq.push({-1, i});
    v.push_back({1, i});
    depths.push_back(1);
  }
  for (int i = k + 2; i <= n; i++) {
    pair<int, int> t = pq.top();
    pq.pop();
    v.push_back({t.second, i});
    pq.push({t.first - 1, i});
    depths.push_back(-t.first + 1);
  }
  reverse(depths.begin(), depths.end());
  cout << depths[0] + depths[1] << endl;
  for (auto i : v) {
    cout << i.first << " " << i.second << endl;
  }
}
