#include <bits/stdc++.h>
using namespace std;
const pair<int, int> DD[] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                             {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
template <class T>
T smin(T &a, T b) {
  if (a > b) a = b;
  return a;
}
template <class T>
T smax(T &a, T b) {
  if (a < b) a = b;
  return a;
}
int main(void) {
  srand(time(0));
  cout << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int> > e;
  for (int i = 2; i <= n; ++i)
    if (i > k)
      e.push_back(make_pair(i, i - k));
    else
      e.push_back(make_pair(1, i));
  static vector<int> g[1 << 20];
  for (auto it : e)
    g[it.first].push_back(it.second), g[it.second].push_back(it.first);
  auto bfs = [&](int node) {
    static int D[1 << 20];
    for (int i = 1; i <= n; ++i) D[i] = -1;
    D[node] = 0;
    queue<int> Q;
    Q.push(node);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (auto it : g[v])
        if (D[it] == -1) D[it] = D[v] + 1, Q.push(it);
    }
    int ans = max_element(D + 1, D + 1 + n) - D;
    return make_pair(ans, D[ans]);
  };
  int cnt = bfs(1).first;
  int ans = bfs(cnt).second;
  cout << ans << '\n';
  for (auto it : e) cout << it.first << ' ' << it.second << '\n';
  cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
  return 0;
}
