#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
  a4 = max(a4, b4);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(10);
  ;
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> edges;
  int N = n - k, L = k;
  if (L >= N - 1) {
    int doubles = 0, singles = 0;
    for (int i = 0; i < (int)N - 1; i++) {
      doubles++;
      edges.push_back({0, i + 1});
      edges.push_back({i + 1, N + i});
    }
    L -= N - 1;
    for (int i = 0; i < (int)L; i++) {
      singles++;
      edges.push_back({0, 2 * N - 1 + i});
    }
    cout << (2 + (doubles > 0) + (doubles > 1)) << "\n";
    for (auto x : edges) cout << x.first + 1 << " " << x.second + 1 << "\n";
  } else {
    int full = ((N - 1) / L) + 1, extra = (N - 1) % L;
    int nxt = 0;
    for (int i = 0; i < (int)L; i++) {
      edges.push_back({0, ++nxt});
      for (int j = 0; j < (int)full - 1; j++)
        edges.push_back({nxt, nxt + 1}), nxt++;
      if (extra) edges.push_back({nxt, nxt + 1}), nxt++, extra--;
    }
    cout << 2 * full + ((N - 1) % L > 0) + ((N - 1) % L > 1) << "\n";
    for (auto x : edges) cout << x.first + 1 << " " << x.second + 1 << "\n";
  }
}
