#include <bits/stdc++.h>
using namespace std;
int n, m, rsp, curent, aux, i, j;
int main() {
  cin >> n >> m;
  rsp = n - m - 1;
  curent = rsp / m;
  aux = rsp % m;
  cout << (curent + 1) * 2 + min(aux, 2) << '\n';
  for (i = 2; i <= n; i++) {
    cout << 1 << " ";
    for (j = i; j - i < curent; j++) cout << j << '\n' << j << " ";
    if (aux) {
      i++;
      cout << j << '\n' << j << " ";
      aux--;
    }
    cout << i + curent << '\n';
    i += curent;
  }
}
