#include <bits/stdc++.h>
using namespace std;
vector<int> g[200100];
bool used[200100];
long long ans = -1;
long long pos = 0;
void dfs(int v, int cnt = 0) {
  used[v] = 1;
  if (cnt > ans) {
    ans = cnt;
    pos = v;
  }
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) {
      dfs(to, cnt + 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, k;
  cin >> n >> k;
  queue<int> q;
  queue<int> q2;
  vector<pair<int, int> > ret;
  for (int i = 1; i <= k; i++) q.push(i);
  for (int i = k + 1; i <= n; i++) q2.push(i);
  while (q2.size() > q.size()) {
    queue<int> q3;
    while (q.size() > 0) {
      g[q.front()].push_back(q2.front());
      g[q2.front()].push_back(q.front());
      ret.push_back(make_pair(q.front(), q2.front()));
      q.pop();
      q3.push(q2.front());
      q2.pop();
    }
    q = q3;
    ans++;
  }
  queue<int> q3 = q2;
  while (q2.size() > 0) {
    g[q.front()].push_back(q2.front());
    g[q2.front()].push_back(q.front());
    ret.push_back(make_pair(q.front(), q2.front()));
    q.pop();
    q2.pop();
  }
  int t = q3.front();
  q3.pop();
  while (q3.size() > 0 || q.size() > 0) {
    if (q3.size() > 0) {
      g[t].push_back(q3.front());
      g[q3.front()].push_back(t);
      ret.push_back(make_pair(t, q3.front()));
      q3.pop();
    }
    if (q.size() > 0) {
      g[t].push_back(q.front());
      g[q.front()].push_back(t);
      ret.push_back(make_pair(t, q.front()));
      q.pop();
    }
  }
  dfs(1);
  for (int i = 0; i <= 200000; i++) used[i] = 0;
  dfs(pos);
  cout << ans << endl;
  for (int i = 0; i < ret.size(); i++) {
    cout << ret[i].first << " " << ret[i].second << endl;
  }
  return 0;
}
