#include <bits/stdc++.h>
using namespace std;
mt19937 rng(
    (unsigned int)chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int n, k;
  cin >> n >> k;
  vector<long long int> a(k);
  vector<long long int> what(k);
  vector<pair<long long int, long long int>> v;
  long long int itr = 0;
  for (long long int i = 2; i < 2 + k; i++) {
    v.push_back({1, i});
    a[itr % k] = i;
    what[itr % k]++;
    itr++;
  }
  for (long long int i = 2 + k; i <= n; i++) {
    v.push_back({a[itr % k], i});
    a[itr % k] = i;
    what[itr % k]++;
    itr++;
  }
  sort(what.begin(), what.end());
  reverse(what.begin(), what.end());
  cout << what[0] + what[1] << "\n";
  for (long long int i = 0; i < v.size(); i++) {
    cout << v[i].first << " " << v[i].second << "\n";
  }
}
