#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long n, k;
  cin >> n >> k;
  set<long long> lnk[k];
  set<pair<long long, long long> > ans;
  for (long long i = 2; i <= n; i++) {
    lnk[i % k].insert(i);
  }
  multiset<long long> sze;
  for (long long i = 0; i < k; i++) {
    long long lst = 1;
    for (long long x : lnk[i]) {
      ans.insert({lst, x});
      lst = x;
    }
    sze.insert(lnk[i].size());
  }
  long long mx = *sze.rbegin();
  sze.erase(sze.find(*sze.rbegin()));
  mx += *sze.rbegin();
  cout << mx << '\n';
  for (pair<long long, long long> y : ans) {
    cout << y.first << " " << y.second << '\n';
  }
  return 0;
}
