#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10;
vector<int> v[M];
int n, m, x, y = 0, k = 0;
int main() {
  cin >> n >> k;
  n -= k;
  if (n <= k) {
    n--;
    if (n == 0) {
      cout << 2 << endl;
      for (int i = 2; i <= n + k + 1; i++) {
        cout << 1 << " " << i << endl;
      }
      return 0;
    }
    if (n == 1) {
      cout << 3 << endl;
      cout << 1 << " " << 2 << endl;
      for (int i = 3; i <= n + k + 1; i++) {
        cout << (i % 2) + 1 << " " << i << endl;
      }
      return 0;
    }
    cout << 4 << endl;
    for (int i = 1; i <= n; i++) {
      cout << 1 << " " << i + 1 << endl;
    }
    for (int i = 1; i <= k; i++) {
      x = i - 1;
      x %= n;
      x += 2;
      cout << x << " " << n + i + 1 << endl;
    }
    return 0;
  } else {
    x = n - 1;
    x = x / k;
    x++;
    y = n - 1;
    y = y % k;
    if (y == 0)
      cout << 2 * x << endl;
    else if (y == 1)
      cout << 2 * x + 1 << endl;
    else
      cout << 2 * x + 2 << endl;
    for (int i = 1; i <= k; i++) {
      cout << 1 << " " << i + 1 << endl;
    }
    for (int i = k + 2; i <= n; i++) {
      cout << i - k << " " << i << endl;
    }
    for (int i = n + 1; i <= n + k; i++) {
      cout << i - k << " " << i << endl;
    }
  }
  return 0;
}
