#include <bits/stdc++.h>
using namespace std;
void solve() {
  int i, j, k, l, m, n, o, p, q, r, s, t;
  cin >> n >> k;
  queue<int> stk[k];
  for (i = 1; i <= k; i++) {
    stk[i % k].push(i);
  }
  for (i = k + 1; i <= n - 1; i++) {
    stk[i % k].push(i);
  }
  vector<pair<int, int>> vp;
  int m1 = LLONG_MIN, m2 = LLONG_MIN;
  for (i = 0; i < k; i++) {
    if (stk[i].size() > m1) {
      m2 = m1;
      m1 = stk[i].size();
    } else {
      m2 = max(m2, (int)stk[i].size());
    }
    vp.push_back({n, stk[i].front()});
  }
  for (i = 0; i < k; i++) {
    while (!stk[i].empty()) {
      int x = stk[i].front();
      stk[i].pop();
      if (!stk[i].empty()) {
        vp.push_back({x, stk[i].front()});
      }
    }
  }
  cout << m1 + m2 << endl;
  for (auto it : vp) {
    cout << it.first << " " << it.second << endl;
  }
}
int main() {
  int t = 1, i = 1;
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  while (t--) {
    solve();
    i++;
  }
  return 0;
}
