#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  pair<int, int> mi(1e8, -1);
  int lo = (n - 1) / k;
  int hi = lo + 1;
  int nhi = (n - 1) % k;
  int dist = nhi >= 2 ? 2 * hi : nhi == 1 ? hi + lo : 2 * lo;
  cout << dist << endl;
  int pos = 2;
  vector<pair<int, int> > edges;
  for (int i = (int)(0); i < (int)(nhi); i++) {
    edges.push_back(pair<int, int>(1, pos));
    pos++;
    for (int j = (int)(1); j < (int)(hi); j++) {
      edges.push_back(pair<int, int>(pos - 1, pos));
      pos++;
    }
  }
  for (int i = (int)(0); i < (int)(k - nhi); i++) {
    edges.push_back(pair<int, int>(1, pos));
    pos++;
    for (int j = (int)(1); j < (int)(lo); j++) {
      edges.push_back(pair<int, int>(pos - 1, pos));
      pos++;
    }
  }
  for (int i = (int)(0); i < (int)(edges.size()); i++) {
    cout << edges[i].first << " " << edges[i].second << "\n";
  }
}
