#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200000 + 100;
const long long mod = 1e9 + 7;
vector<pair<int, int> > ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  if (n == 3) {
    cout << "2\n1 2\n2 3\n";
  } else if (k == n - 1) {
    cout << 2 << endl;
    for (int i = 1; i < n; i++) cout << i << ' ' << n << endl;
  } else {
    int first = 1;
    for (int i = 1; i <= k; i++) {
      ans.push_back({n, i});
    }
    int m = n - k - 1;
    long long j = 1;
    for (int i = k + 1; i <= n - 1; i++) {
      if (j % k == 1) first++;
      ans.push_back({i, j});
      j++;
    }
    cout << first * 2 - ((j - 1) % k == 1) << endl;
    for (int i = 0; i < n - 1; i++)
      cout << ans[i].first << ' ' << ans[i].second << endl;
  }
  return 0;
}
