#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  long long i, n, j, k;
  cin >> n >> k;
  n--;
  long long y = n / k, r = 0;
  if (n % k >= 2)
    r = 2;
  else if (n % k == 1)
    r = 1;
  cout << 2 * y + r << "\n";
  long long t = 1, x = 2, c = 0;
  std::vector<pair<long long, long long> > v;
  while (c < k) {
    v.push_back({t, x});
    x++;
    c++;
  }
  c = 1;
  t = 2;
  while (c < y) {
    long long d = 0, g = x;
    while (d < k) {
      v.push_back({t, x});
      x++;
      t++;
      d++;
    }
    c++;
  }
  c = 0, k = n % k;
  while (c < k) {
    v.push_back({t, x});
    t++;
    x++;
    c++;
  }
  for (auto i : v) cout << i.first << " " << i.second << "\n";
}
