#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k) == 2) {
    if (k == 2) {
      printf("%d\n", n - 1);
    } else {
      int now = n / k;
      now *= 2;
      if (n % k == 2)
        now++;
      else if (n % k > 2)
        now += 2;
      deb(now);
    }
    queue<int> q;
    int k1 = 0, cnt = 2;
    for (int i = 1; i <= k; i++) {
      printf("%d %d\n", 1, cnt);
      q.push(cnt++);
      n--;
    }
    n--;
    while (n) {
      int now = q.front();
      q.pop();
      printf("%d %d\n", now, cnt);
      q.push(cnt++);
      n--;
    }
  }
}
