#include <bits/stdc++.h>
using namespace std;
const long long int mxn = 2 * 1e5 + 5;
long long int vis[mxn];
vector<vector<long long int>> ad(mxn);
long long int leaf, ans = 0;
void dfs(long long int n, long long int d) {
  vis[n] = 1;
  for (auto child : ad[n])
    if (!vis[child]) dfs(child, d + 1);
  if (d > ans) {
    ans = d;
    leaf = n;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  while (t--) {
    long long int i, j, k, n, m;
    cin >> n >> k;
    vector<long long int> par(k + 1);
    for (j = 1; j <= k; j++) par[j] = j;
    vector<vector<long long int>> adj(n + 1);
    for (i = 1; i <= k; i++) {
      adj[n].emplace_back(i);
      ad[n].emplace_back(i);
      ad[i].emplace_back(n);
    }
    for (j = 0; i < n; i++, j++) {
      adj[par[j % k + 1]].emplace_back(i);
      ad[par[j % k + 1]].emplace_back(i);
      ad[i].emplace_back(par[j % k + 1]);
      par[j % k + 1] = i;
    }
    dfs(1, 0);
    ans = 0;
    memset(vis, 0, sizeof vis);
    dfs(leaf, 0);
    cout << ans << "\n";
    for (i = 1; i <= n; i++) {
      for (auto j : adj[i]) cout << i << " " << j << "\n";
    }
  }
  return 0;
}
