#include <bits/stdc++.h>
using namespace std;
const int OO = 1e9;
const long long INF = 1e18;
const int irand(int lo, int hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
const long long lrand(long long lo, long long hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
int n, m;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cin >> n >> m;
  if (n - m == 1) {
    cout << 2 << endl;
    for (int i = 2; i <= n; i += 1) cout << "1 " << i << endl;
  } else if (n - m == 2) {
    cout << 3 << endl;
    cout << "1 2" << endl;
    cout << "1 3" << endl;
    for (int i = 4; i <= n; i += 1) cout << "2 " << i << endl;
  } else {
    int nonexit = n - m;
    int legs = min(m, nonexit - 1);
    int len = (nonexit - 1) / legs;
    int mn = len;
    if ((nonexit - 1) % legs != 0) len++;
    mn *= 2;
    if ((nonexit - 1) % legs == 0)
      mn = mn;
    else if ((nonexit - 1) % legs == 1)
      mn++;
    else
      mn += 2;
    mn += 2;
    cout << mn << endl;
    int exitnow = nonexit + 1;
    int curr = 2;
    for (int i = 1; i <= legs; i += 1) {
      int prev = 1;
      for (int j = 1; j <= len; j += 1) {
        if (curr + legs - i > nonexit) break;
        cout << prev << " " << curr << endl;
        prev = curr;
        curr++;
      }
      cout << prev << " " << exitnow << endl;
      exitnow++;
    }
    while (exitnow <= n) cout << "1 " << exitnow++ << endl;
  }
  return 0;
}
