#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long> primeFact(long long n) {
  vector<long long> ans;
  for (long long t = 2; t <= sqrt(n); t++) {
    if (n % t == 0) {
      ans.push_back(t);
      while (n % t == 0) {
        n = n / t;
      }
    }
  }
  if (n > 2) ans.push_back(n);
  return ans;
}
vector<long long> fact(long long n) {
  vector<long long> ans;
  for (long long i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (i * i == n) {
        ans.push_back(i);
      } else {
        ans.push_back(i);
        ans.push_back(n / i);
      }
    }
  }
  return ans;
}
long long powerr(long long base, long long exp, long long mod) {
  if (exp == 0) return 1;
  long long t = powerr(base, exp / 2, mod) % mod;
  if (exp % 2 == 0) {
    return (t % mod * t % mod) % mod;
  } else {
    return (t % mod * t % mod * base % mod) % mod;
  }
}
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int32_t main() {
  fastio();
  long long n, k;
  cin >> n >> k;
  n = n - k;
  long long node = 1;
  set<pair<long long, long long> > s;
  vector<pair<long long, long long> > ans;
  map<long long, long long> dis;
  for (long long i = 0; i < k; i++) {
    node++;
    s.insert({1, node});
    dis[node] = 1;
  }
  for (long long i = 0; i < n - 1; i++) {
    node++;
    pair<long long, long long> temp = *s.begin();
    s.erase(s.begin());
    ans.push_back({temp.first, node});
    s.insert({node, temp.second});
    dis[temp.second]++;
  }
  for (auto a : s) {
    ans.push_back(a);
  }
  vector<long long> y;
  for (auto a : dis) {
    y.push_back(a.second);
  }
  sort(y.begin(), y.end());
  cout << y[k - 1] + y[k - 2] << "\n";
  for (auto a : ans) {
    cout << a.first << " " << a.second << "\n";
  }
  return 0;
}
