#include <bits/stdc++.h>
using namespace std;
char d[55][55];
int vis[55][55];
int ans;
int main() {
  int n, k;
  while (cin >> n >> k) {
    int cnt = 0;
    int ans = (int)((n - 1) / k) * 2;
    ans += ((n - 1) % k) <= 2 ? ((n - 1) % k) : 2;
    cout << ans << endl;
    for (int i = 2; i <= k; i++) {
      cout << 1 << " " << i << endl;
    }
    for (int i = k + 1; i <= n; i++) {
      cout << i - k << " " << i << endl;
    }
  }
  return 0;
}
