#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << "\n";
  err(++it, args...);
}
const long long M = 1e9 + 7, inf = 1e18;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, k;
  cin >> n >> k;
  n--;
  int u = 1, v = 2;
  vector<pair<int, int> > ans;
  for (int i = 0; i < k; i++) {
    int len = n / k + (i < n % k);
    u = 1;
    for (int j = 0; j < len; j++) {
      ans.push_back({u, v});
      u = v;
      v++;
    }
  }
  int cnt = 2 * (n / k) + min(n % k, 2);
  cout << cnt << "\n";
  for (auto x : ans) {
    cout << x.first << " " << x.second << "\n";
  }
}
