#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<vector<int>> a(k);
  for (int i = 2; i <= n; i++) {
    a[i % k].push_back(i);
  }
  vector<int> sz;
  for (int i = 0; i < k; i++) {
    sz.push_back(a[i].size());
  }
  sort(sz.begin(), sz.end());
  int dist = sz[k - 1] + sz[k - 2];
  printf("%d\n", dist);
  for (int i = 0; i < k; i++) {
    printf("1 %d\n", a[i][0]);
    for (int j = 0; j < a[i].size() - 1; j++) {
      printf("%d %d\n", a[i][j], a[i][j + 1]);
    }
  }
}
