#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e6 + 10;
int deg[N];
multiset<int> tt;
vector<vector<int> > v;
void dfs(int i, int p, int cost) {
  bool leaf = 1;
  for (int j = 0; j < v[i].size(); ++j) {
    int next = v[i][j];
    if (next != p) dfs(next, i, cost + 1), leaf = 0;
  }
  if (leaf == 1) tt.insert(cost);
  if (tt.size() > 2) tt.erase(tt.begin());
}
int main() {
  int centernode = 0;
  int n, k;
  cin >> n >> k;
  deg[0] = 1;
  v.resize(n);
  vector<vector<int> > ray(k);
  vector<int> temp;
  int i;
  int cur = 0;
  for (i = 1; i < n; ++i) {
    temp.push_back(i);
    deg[i] = 1;
    ray[cur % k].push_back(i);
    ++cur;
  }
  for (int i = 0; i < ray.size(); ++i) {
    for (int j = 1; j < ray[i].size(); ++j)
      v[ray[i][j - 1]].push_back(ray[i][j]);
    v[0].push_back(ray[i][0]);
  }
  dfs(0, -1, 0);
  int e = *tt.begin() + *++tt.begin();
  cout << e << endl;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < v[i].size(); ++j)
      cout << i + 1 << " " << v[i][j] + 1 << endl;
}
