#include <bits/stdc++.h>
using namespace std;
int n, k, L;
vector<pair<int, int> > ans;
void Work() {
  int i, r = n % k;
  if (r == 0) r = k;
  for (i = r + 1; i <= n; ++i) {
    if (i + k <= n) ans.push_back({i, i + k});
  }
  if (r == 1) {
    for (i = 2; i <= 1 + k; ++i) ans.push_back({1, i});
    L = (n - r) / k * 2;
  } else if (r == 2) {
    ans.push_back({1, 3});
    for (i = 4; i <= 2 + k; ++i) ans.push_back({2, i});
    ans.push_back({1, 2});
    L = (n - r) / k * 2 + 1;
  } else {
    for (i = 2; i <= r; ++i) {
      ans.push_back({i, i + r - 1});
      ans.push_back({1, i});
    }
    for (; i <= k + 1; ++i) {
      ans.push_back({r, i + r - 1});
    }
    L = (n - r) / k * 2 + 2;
  }
  assert(ans.size() == n - 1);
  printf("%d\n", L);
  for (auto &e : ans) {
    printf("%d %d\n", e.first, e.second);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  if (k == 2) {
    printf("%d\n", n - 1);
    for (int i = 1; i < n; ++i) printf("%d %d\n", i, i + 1);
  } else
    Work();
  return 0;
}
