#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> temp;
  int ans = (n - 1) / m;
  ans *= 2;
  if ((n - 1) % m >= 2)
    ans += 2;
  else if ((n - 1) % m == 1)
    ans++;
  cout << ans << "\n";
  for (int i = 2; i <= m + 1; ++i) {
    cout << 1 << " " << i << "\n";
    temp.push_back(i);
  }
  int ptr = m + 2;
  while (ptr <= n) {
    for (int j = 0; j < temp.size() && ptr <= n; ++j) {
      cout << temp[j] << " " << ptr << "\n";
      temp[j] = ptr;
      ptr++;
    }
  }
}
