#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
bool sortbysec(const pair<long long, long long>& a,
               const pair<long long, long long>& b) {
  return (a.second < b.second);
}
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> v[k];
  for (long long i = 0; i < k; i++) {
    v[i].push_back(1);
  }
  for (long long i = 2; i < n + 1; i++) {
    v[((i - 2) % k)].push_back(i);
  }
  vector<long long> ans;
  for (long long i = 0; i < k; i++) {
    ans.push_back(v[i].size());
  }
  sort(ans.begin(), ans.end());
  cout << ans[k - 1] + ans[k - 2] - 2 << "\n";
  for (long long i = 0; i < k; i++) {
    for (long long j = 1; j < v[i].size(); j++) {
      cout << v[i][j] << " " << v[i][j - 1] << "\n";
    }
  }
}
int main() {
  fast();
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
}
