#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000404;
const long long MOD = 1000000007ll;
const long double PI = acos(-1.0);
const long long INF = 2000000000000000404ll;
const long double EPS = 1e-9;
template <typename t1, typename t2>
inline void upmax(t1 &a, t2 b) {
  a = max(a, (t1)b);
}
template <typename t1, typename t2>
inline void upmin(t1 &a, t2 b) {
  a = min(a, (t1)b);
}
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
inline T lcm(T a, T b) {
  return a * (b / gcd(a, b));
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
inline bool pal(string &x) {
  int n = (int)x.length();
  for (int i = 0; i < n / 2; i++) {
    if (x[i] != x[n - i - 1]) return 0;
  }
  return 1;
}
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dx8[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy8[] = {-1, 0, 1, 1, -1, -1, 0, 1};
int SQ = 400;
const int N = 4123456;
int p[N];
vector<int> g[N];
int dfs(int v) {
  if ((int((g[v].size()))) == 0)
    return 1;
  else
    return dfs(g[v][0]) + 1;
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 2; i < k + 2; i++) {
    p[i] = 1;
  }
  int x = 0;
  for (int i = k + 2; i < n + 1; i++) {
    x++;
    if (x > k) x -= k;
    p[i] = p[x + 1];
    p[x + 1] = i;
  }
  for (int i = 1; i < n + 1; i++) {
    g[p[i]].push_back(i);
  }
  int maxx = 0;
  int a = 0, b = 0;
  for (auto x : g[1]) {
    int c = dfs(x);
    if (c >= a) {
      b = a;
      a = c;
    } else {
      if (c > b) b = c;
    }
  }
  cout << a + b << endl;
  for (int i = 1; i < n + 1; i++) {
    if (p[i] != 0) cout << i << ' ' << p[i] << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  };
  return 0;
}
