#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:255000000")
bool firstout = 1;
template <class T>
T &minn(T &a, T b) {
  if (b < a) a = b;
  return a;
}
template <class T>
T &maxx(T &a, T b) {
  if (a < b) a = b;
  return a;
}
int &madd(int &a, int b) {
  a += b;
  if (a >= 1000000009) a -= 1000000009;
  return a;
}
int &msub(int &a, int b) {
  a -= b;
  if (a < 0) a += 1000000009;
  return a;
}
int &mmult(int &a, int b) { return a = (long long)a * b % 1000000009; }
int mdiv(long long a, long long b, long long m) {
  a = (a % m + m) % m;
  b = (b % m + m) % m;
  if (a % b == 0) return a / b;
  return (a + m * mdiv(-a, m, b)) / b;
}
int n, m;
int A[5012][5012];
int AN[5012];
int AI[5012];
int calc(bool print) {
  int res = 0;
  int p = 0;
  int i, j, k;
  if (print) {
    set<pair<int, int> > s;
    for (i = (0); i < (n); ++i) AI[i] = 0, s.insert(make_pair(A[i][0], i));
    for (; s.size();) {
      set<pair<int, int> >::iterator si = s.lower_bound(make_pair(p, -1));
      if (si == s.end()) si = s.begin(), ++res;
      i = si->second;
      p = si->first;
      if (print)
        printf((firstout) ? "%"
                            "d"
                          : " %"
                            "d",
               (p)),
            firstout = 0,
            printf((firstout) ? "%"
                                "d"
                              : " %"
                                "d",
                   (i + 1)),
            firstout = 0, printf("\n"), firstout = 1;
      s.erase(si);
      if (++AI[i] < AN[i]) s.insert(make_pair(A[i][AI[i]], i));
    }
  } else {
    for (i = (0); i < (n); ++i) {
      int r = 0;
      for (j = (1); j < (AN[i]); ++j)
        if (A[i][j] < A[i][j - 1]) ++r;
      maxx(res, r);
    }
  }
  return res;
}
int main() {
  int i, j, k;
  char c;
  int a, d;
  int ts;
  for (ts = 1; scanf("%"
                     "d",
                     &(n)) > 0;
       ++ts) {
    int nn = 0;
    for (i = (0); i < (n); ++i) {
      int l, a, x, y, m;
      scanf(
          "%"
          "d",
          &(l));
      nn += l;
      scanf(
          "%"
          "d",
          &(a));
      scanf(
          "%"
          "d",
          &(x));
      scanf(
          "%"
          "d",
          &(y));
      scanf(
          "%"
          "d",
          &(m));
      AN[i] = l;
      for (j = (0); j < (l); ++j) {
        A[i][j] = a;
        a = ((long long)a * x + y) % m;
      }
    }
    int res = calc(0);
    printf(
        "%"
        "d",
        (res));
    printf("\n"), firstout = 1;
    if (nn <= 200000) calc(1);
  }
  return 0;
}
