#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200100];
int maxi = 0, idx;
void dfs(int node, int dist, int par) {
  if (dist > maxi) maxi = dist, idx = node;
  for (int i = 0; i < adj[node].size(); i++)
    if (adj[node][i] != par) dfs(adj[node][i], dist + 1, node);
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int> > vec;
  queue<int> q;
  q.push(1);
  int x = 2;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    if (a == n) break;
    if (x > n) continue;
    if (x == 2) {
      for (int i = 0; i < k; i++) {
        vec.push_back({a, x});
        q.push(x);
        x++;
      }
      continue;
    }
    vec.push_back({a, x});
    q.push(x);
    x++;
  }
  for (int i = 0; i < vec.size(); i++) {
    adj[vec[i].first].push_back(vec[i].second);
    adj[vec[i].second].push_back(vec[i].first);
  }
  dfs(1, 0, -1);
  maxi = 0;
  dfs(idx, 0, -1);
  cout << maxi << endl;
  for (int i = 0; i < vec.size(); i++)
    printf("%d %d\n", vec[i].first, vec[i].second);
  return 0;
}
