#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200003;
const long long maxk = 1003;
long long lev[maxn];
long long curr[maxn];
vector<long long> g[maxn];
long long ans1[maxn];
long long ans2[maxn];
long long total = 0;
long long n, k;
void dfs(long long v, long long parent) {
  ans1[total] = v;
  ans2[total] = parent;
  total++;
  for (long long ch : g[v]) {
    if (ch == parent) continue;
    dfs(ch, v);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  long long root = 0;
  for (long long i = 1; i <= k; i++) {
    curr[i] = i;
    lev[i] = 1;
    g[root].push_back(i);
  }
  long long it = 1;
  for (long long i = k + 1; i < n; i++) {
    if (it == k + 1) it = 1;
    lev[it]++;
    g[curr[it]].push_back(i);
    curr[it] = i;
    it++;
  }
  cout << lev[1] + lev[2] << '\n';
  for (long long ch : g[root]) {
    dfs(ch, root);
  }
  for (long long i = 0; i < total; i++) {
    cout << ans1[i] + 1 << ' ' << ans2[i] + 1 << '\n';
  }
  return 0;
}
