#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7, oo = 1e9;
const ll loo = 1e18;
ll modpow(ll a, ll b) {
  ll ans = 1;
  for (; b; b /= 2, a = a * a % mod)
    if (b & 1) ans = (ans * a) % mod;
  return ans;
}
ll gcd(ll a, ll b) {
  while (a) b %= a, swap(a, b);
  return b;
}
const bool DEBUG = 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int parent[n], d[n];
  parent[0] = -1;
  d[0] = 0;
  stringstream ss;
  for (int i = 1; i < k + 1; i++) parent[i] = 0, d[i] = 1;
  for (int i = k + 1; i < n; i++) parent[i] = i - k, d[i] = d[i - k] + 1;
  if ((n - 1) % k == 0 || (n - 1) % k > 1)
    ss << 2 * d[n - 1] << endl;
  else
    ss << 2 * d[n - 1] - 1 << endl;
  for (int i = 0; i < n; i++)
    if (parent[i] != -1) ss << i + 1 << " " << parent[i] + 1 << endl;
  cout << ss.str();
  return 0;
}
