#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int Lim = 1e5 + 1e3;
const double eps = 1e-15;
int main() {
  int n, m, total;
  cin >> n >> m;
  if ((n - 1) % m == 0) {
    total = 2 * ((n - 1) / m);
    cout << total << endl;
    int temp = 2;
    for (int i = 0, _n = (int)m; i < _n; i++) {
      for (int j = 0, _n = (int)(n - 1) / m; j < _n; j++) {
        if (!j)
          cout << 1 << " " << temp << endl;
        else {
          cout << temp << " " << temp + 1 << endl;
          temp++;
        }
      }
      temp++;
    }
  } else if ((n - 1) % m == 1) {
    total = 2 * ((n - 1) / m) + 1;
    cout << total << endl;
    int temp = 2;
    for (int i = 0, _n = (int)m; i < _n; i++) {
      for (int j = 0, _n = (int)(n - 1) / m; j < _n; j++) {
        if (!j)
          cout << 1 << " " << temp << endl;
        else {
          cout << temp << " " << temp + 1 << endl;
          temp++;
        }
      }
      if (i == m - 1) cout << temp << " " << temp + 1 << endl;
      temp++;
    }
  } else {
    total = 2 * ((n - 1) / m) + 2;
    cout << total << endl;
    int temp = 2;
    for (int i = 0, _n = (int)m; i < _n; i++) {
      for (int j = 0, _n = (int)(n - 1) / m; j < _n; j++) {
        if (!j)
          cout << 1 << " " << temp << endl;
        else {
          cout << temp << " " << temp + 1 << endl;
          temp++;
        }
      }
      if (i >= m - (n - 1) % m) {
        cout << temp << " " << temp + 1 << endl;
        temp++;
      }
      temp++;
    }
  }
  return 0;
}
