#include <bits/stdc++.h>
using namespace std;
const double EPS = 0.0000000001;
const long long mod1 = 998244353;
const long long mod2 = 1000000007;
const long long mod3 = 1000000009;
const long long inf = 1000000000000000000;
using namespace std;
signed main() {
  long long n, a;
  cin >> n >> a;
  vector<queue<long long> > q(a);
  long long x = 2;
  long long cnt[a];
  memset(cnt, 0, sizeof cnt);
  for (; x <= n; x++) {
    q[(x - 1) % a].push(x);
    cnt[(x - 1) % a]++;
  }
  sort(cnt, cnt + a);
  cout << cnt[a - 2] + cnt[a - 1] << endl;
  for (long long i = 0; i < a; i++) {
    long long prev = 1;
    while (q[i].size()) {
      long long x = q[i].front();
      q[i].pop();
      cout << prev << " " << x << endl;
      prev = x;
    }
  }
}
