#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  if (K == 2) {
    cout << N - 1 << endl;
    for (int i = 0; i < N - 1; ++i) {
      cout << i + 1 << " " << i + 2 << endl;
    }
  } else {
    vector<vector<int> > V(K);
    for (int i = 0; i < K; ++i) {
      V[i].push_back(i + 1);
    }
    int sz = K;
    while (sz + 1 < N) {
      for (int i = 0; i < K && sz + 1 < N; ++i) {
        V[i].push_back(++sz);
      }
    }
    for (int i = 0; i < K; ++i) {
      V[i].push_back(sz + 1);
    }
    sz++;
    int D = V[0].size() + V[1].size() - 2;
    cout << D << endl;
    for (int i = 0; i < K; ++i) {
      for (int j = 1; j < V[i].size(); ++j) {
        cout << V[i][j - 1] << " " << V[i][j] << endl;
      }
    }
  }
}
