#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int INF = 1e9;
const double eps = 1e-6;
template <class T>
T gcd(T a, T b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int main() {
  int n, k;
  while (cin >> n >> k) {
    int rmd = (n - 1) % k, dvd = (n - 1) / k;
    if (rmd == 1)
      cout << 2 * dvd + 1 << endl;
    else if (rmd >= 2)
      cout << 2 * dvd + 2 << endl;
    else
      cout << 2 * dvd << endl;
    for (int i = 1; i <= k; i++) cout << 1 << " " << i + 1 << endl;
    for (int i = k + 2; i <= n; i++) cout << i << " " << i - k << endl;
  }
  return 0;
}
