#include <bits/stdc++.h>
int in() {
  int r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') return -in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}
using namespace std;
const int INF = int(1e9 + 7);
vector<bool> vis;
vector<vector<int> > g;
int maxD, maxN;
void dfs(int v, int d) {
  if (d > maxD) {
    maxD = d;
    maxN = v;
  }
  vis[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (!vis[u]) dfs(u, d + 1);
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  g.clear();
  g.resize(n + 1);
  vis.assign(n + 1, false);
  int centro = n;
  queue<int> afuera, adentro;
  vector<int> a;
  int sobran = n - k - 1;
  for (int i = 1; i <= k; i++) {
    afuera.push(i);
  }
  int sig = k + 1;
  vector<pair<int, int> > v;
  while (sobran > 0) {
    a.clear();
    int usar = min(sobran, (int)afuera.size());
    if (usar == 1) {
      g[sig].push_back(n);
      g[n].push_back(sig);
      v.push_back(make_pair(n, sig));
      usar++;
    }
    for (int i = sig; i < sig + usar; i++) {
      a.push_back(i);
    }
    sig += usar;
    sobran -= usar;
    for (int i = 0; i < a.size(); i++) {
      int u = afuera.front();
      afuera.pop();
      g[u].push_back(a[i]);
      g[a[i]].push_back(u);
      v.push_back(make_pair(a[i], u));
    }
    while (!afuera.empty()) {
      int u = afuera.front();
      afuera.pop();
      g[u].push_back(a[0]);
      v.push_back(make_pair(a[0], u));
      g[a[0]].push_back(u);
    }
    for (int i = 0; i < a.size(); i++) {
      afuera.push(a[i]);
    }
  }
  while (!afuera.empty() && sobran >= 0) {
    int u = afuera.front();
    afuera.pop();
    g[u].push_back(n);
    v.push_back(make_pair(n, u));
    g[n].push_back(u);
  }
  maxD = 0;
  dfs(1, 0);
  vis.assign(n + 1, false);
  maxD = 0;
  dfs(maxN, 0);
  cout << maxD << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
  return 0;
}
