#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, k;
  cin >> n >> k;
  long long ne = 2;
  vector<vector<long long> > no(k, vector<long long>());
  long long index = 0;
  while (ne <= n) {
    no[index].push_back(ne);
    index++;
    ne++;
    index %= k;
  }
  vector<long long> sizes(k);
  for (long long i = 0; i < k; i++) {
    sizes[i] = (long long)(no[i]).size();
  }
  sort(sizes.begin(), sizes.end());
  cout << sizes[k - 1] + sizes[k - 2] << "\n";
  for (long long i = 0; i < k; i++) {
    cout << no[i][0] << " " << 1 << "\n";
    for (long long j = 0; j < (long long)(no[i]).size() - 1; j++) {
      cout << no[i][j] << " " << no[i][j + 1] << "\n";
    }
  }
}
