#include <bits/stdc++.h>
using namespace std;
long long n, k;
void solve() {
  cin >> n >> k;
  long long m = n - 1;
  long long d = m / k;
  long long ans = 2 * d;
  if (m % k == 1)
    ans++;
  else if (m % k > 1)
    ans += 2;
  cout << ans << endl;
  for (long long i = 1; i < n; i++) {
    if (i <= k)
      cout << 1 << " " << i + 1 << endl;
    else
      cout << i + 1 << " " << (i + 1) - k << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
}
