#include <bits/stdc++.h>
using namespace std;
int n, m;
int ls[200004], p[200004];
int main() {
  cin >> n >> m;
  int x = 0;
  if (n - m == 1) {
    cout << 2 << "\n";
    for (int i = 2; i < n + 1; i++) {
      cout << 1 << " " << i << "\n";
    }
    return 0;
  }
  if (n - m == 2) {
    cout << 3 << "\n";
    cout << 1 << " " << 2 << "\n";
    for (int i = 3, k = 1; i < n + 1; i++, k++) {
      if (k > 2) k = 1;
      cout << i << " " << k << "\n";
    }
    return 0;
  }
  if (n - m >= m) {
    if (n - m % m > 0) x = 1;
    for (int i = 2; i < m + 2; i++) {
      p[i]++;
    }
    for (int i = m + 2, k = 2; i < n + 1; i++, k++) {
      if (k == m + 2) k = 2;
      p[k]++;
    }
    sort(p, p + m + 2);
    cout << p[m + 1] + p[m] << "\n";
    for (int i = 2; i < m + 2; i++) {
      cout << 1 << " " << i << "\n";
      ls[i] = i;
    }
    for (int i = m + 2, k = 2; i < n + 1; i++, k++) {
      if (k == m + 2) k = 2;
      cout << i << " " << ls[k] << "\n";
      ls[k] = i;
    }
  } else {
    cout << 4 << "\n";
    for (int i = 2; i < n - m + 1; i++) {
      cout << 1 << " " << i << "\n";
      ls[i] = i;
      p[i]++;
    }
    int k;
    k = 2;
    for (int i = n - m + 1; i < n + 1; i++, k++) {
      if (k == n - m + 1) {
        k = 2;
      }
      cout << i << " " << k << "\n";
    }
  }
}
