#include <bits/stdc++.h>
using namespace std;
int n, k, x, y;
vector<int> halp;
queue<int> aux;
queue<pair<int, int> > q;
vector<pair<int, int> > ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 2; i <= n - k; i++) aux.push(i);
  for (int i = n; i > n - k; i--) q.push({i, 0});
  while (!q.empty()) {
    x = q.front().first, y = q.front().second;
    q.pop();
    if (!aux.empty())
      ans.push_back({x, aux.front()}), q.push({aux.front(), y + 1}), aux.pop();
    else
      ans.push_back({1, x}), halp.push_back(y + 1);
  }
  sort(halp.begin(), halp.end());
  cout << halp[halp.size() - 1] + halp[halp.size() - 2] << "\n";
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << "\n";
}
