#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using db = double;
int n, k;
int len[200006];
int top[200006];
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; ++i) top[i] = 1;
  int t = 0;
  for (int i = 2; i <= n; ++i) {
    ++len[t];
    ++t %= k;
  }
  multiset<int> h;
  for (int i = 0; i < k; ++i) h.insert(len[i]);
  cout << *h.rbegin() + *++h.rbegin() << endl;
  t = 0;
  for (int i = 2; i <= n; ++i) {
    printf("%d %d\n", top[t], i);
    top[t] = i;
    ++t %= k;
  }
  return 0;
}
