#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

typedef vector <int> vi;

#define pb push_back
#define forn(i, n) for (int i = 0; i < (int)(n); i++)

#include "random"

#define m 100100

vi c[m];
int n = 0, p[m];

void adde( int a, int b )
{
  c[a].pb(b); c[b].pb(a);
}

void shuffle()
{
  forn (i, n)
    p[i] = i, swap(p[i], p[r(0, i)]);
}

void out()
{
  printf("%d\n", n);
  forn (i, n)
    forn (j, c[i].size())
      if (i < c[i][j])
        printf("%d %d\n", p[c[i][j]] + 1, p[i] + 1);
}

int main( int argc, char **argv )
{
  if (argc < 4)
    return -1;
  int np = atoi(argv[1]); // number of vertices
  int k = atoi(argv[2]); // k-ary tree
  osrand(atoi(argv[3]));
  n++, np--;
  int t = 1, p = 0;
  while (np > 0)
  {
    forn (i, t)
      forn (j, k)
        if (np > 0)
          adde(p + i, p + t + i * k + j), np--, n++;
    p += t, t *= k;
  }
  shuffle();
  out();
  return 0;
}
