#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int ans = (n - 1) / m * 2;
  if ((n - 1) % m == 1)
    ans++;
  else if ((n - 1) % m)
    ans += 2;
  cout << ans << endl;
  for (int i = 2; i <= m; i++) cout << "1 " << i << endl;
  for (int i = m + 1; i <= n; i++) cout << i << " " << i - m << endl;
}
