#include <bits/stdc++.h>
using namespace std;
const long long MAXVAL = (long long)1e18 + 1;
const long long MOD = 1000000007;
const int INF = 1000000000 + 1;
long long poww(int x, long long pow) {
  if (pow == 0ll) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x) % MOD;
  }
  return ans % MOD;
}
vector<int> pi;
vector<int> we;
int dsu_rep(int x) {
  if (pi[x] == x) return x;
  int res = dsu_rep(pi[x]);
  pi[x] = res;
  return res;
}
void dsu_union(int x, int y) {
  x = dsu_rep(x);
  y = dsu_rep(y);
  if (x == y) return;
  if (we[x] > we[y]) swap(x, y);
  pi[y] = x;
  if (we[x] == we[y])
    ++we[x];
  else
    we[x] += we[y];
}
int bitcnt(int x) {
  int res = 0;
  while (x > 0) {
    if (x & 1) res++;
    x >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int Q = 1;
  while (Q-- > 0) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    vector<vector<int>> g(n);
    int curind = 1;
    int curleaf = 0;
    vector<int> lastv(k, 0);
    while (curind < n) {
      int last = lastv[curleaf];
      int newv = curind;
      g[newv].push_back(last);
      lastv[curleaf] = newv;
      curleaf = (curleaf + 1) % k;
      ++curind;
    }
    int ans;
    int mm = (n - 1) % k;
    if (mm >= 2) {
      ans = 2 + 2 * ((n - 1) / k);
    } else if (mm >= 1) {
      ans = 1 + 2 * ((n - 1) / k);
    } else {
      ans = 2 * ((n - 1) / k);
    }
    cout << ans << endl;
    for (int i = 0; i < n; ++i) {
      for (int j : g[i]) {
        cout << (i + 1) << " " << j + 1 << "\n";
      }
    }
  }
  return 0;
}
