#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > e;
vector<vector<long long> > a(200005);
long long n, m;
void add(long long u, long long v) {
  e.push_back(make_pair(u, v));
  a[u].push_back(v);
  a[v].push_back(u);
}
queue<long long> q;
void solve() {
  q.push(1);
  long long peak = 1;
  while (!q.empty() && peak < n) {
    long long u = q.front();
    q.pop();
    if (m == 0) {
      add(u, ++peak);
      q.push(peak);
    }
    while (m > 0) {
      m--;
      add(u, ++peak);
      q.push(peak);
    }
  }
}
pair<long long, long long> dfs(long long u, long long p) {
  pair<long long, long long> ret = make_pair(0, u);
  for (auto v : a[u]) {
    if (v == p) continue;
    pair<long long, long long> rec = dfs(v, u);
    rec.first++;
    ret = max(ret, rec);
  }
  return ret;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  solve();
  long long u = dfs(1, 1).second;
  cout << dfs(u, u).first << '\n';
  for (long long i = 0; i < e.size(); i++)
    cout << e[i].first << ' ' << e[i].second << '\n';
}
