#include <cassert>
#include <cstdio>
#include <cstdlib>

#include <algorithm>
#include <map>

const int maxc = 1000;

unsigned long long lcg_seed;
unsigned int lcg_rand( unsigned int a, unsigned int b ) {
  unsigned int r = a + (lcg_seed >> 16) % (b - a + 1);
  lcg_seed = lcg_seed * 239017 + 66666;
  return r;
}
template <typename T>
void lcg_shuffle( T begin, T end ) {
  for (int i = 0; begin + i != end; i++) {
    int j = lcg_rand(0, i);
    if (j != i)
      std::swap(*(begin + i), *(begin + j));
  }
}

int main( int argc, char **argv ) {
  assert(argc == 5 || argc == 6);
  int n = atoi(argv[1]);
  int m = atoi(argv[2]);
  int t = atoi(argv[3]);
  lcg_seed = atoll(argv[4]);
  int tt = argc == 6 ? atoi(argv[5]) : 0;

  std::map <int, int> d[n];
  for (int i = 0; i < m; i++) {
    int a, b, x;
    do {
      a = i < n ? i : lcg_rand(0, n - 1);
      b = lcg_rand(1, maxc);
      if (tt == 0)
        x = lcg_rand(1, t);
      else {
        x = t;
        while (x > 1 && lcg_rand(0, 12 - t) == 0)
          x--;
      }
      // x = lcg_rand(1, t);
    } while (d[a].count(b));
    d[a][b] = x;
  }

  printf("%d\n", n);
  for (int i = 0; i < n; i++) {
    int k = d[i].size(), j = 0;
    int a[k], b[k];
    for (auto it = d[i].begin(); it != d[i].end(); it++) {
      a[j] = it->first;
      b[j] = it->second;
      j++;
    }
    assert(j == k);
    lcg_shuffle(a, a + k);
    lcg_shuffle(b, b + k);
    printf("%d", k);
    for (int j = 0; j < k; j++)
      printf(" %d %d", a[j], b[j]);
    puts("");
  }

  return 0;
}

