#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5024;
int n, a[MAXN], x[MAXN], y[MAXN], m[MAXN], k[MAXN];
priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >,
               greater<pair<int, pair<int, int> > > >
    q[2];
vector<pair<int, int> > answer;
void solveSlow() {
  int t = 0;
  for (int i = 0; i < n; i++) {
    q[t].push(make_pair(a[i], make_pair(i, k[i] - 1)));
  }
  int res = -1;
  while (!q[t].empty()) {
    res++;
    while (!q[t ^ 1].empty()) q[t ^ 1].pop();
    while (!q[t].empty()) {
      int value = q[t].top().first, idx = q[t].top().second.first,
          rem = q[t].top().second.second;
      q[t].pop();
      answer.push_back(make_pair(value, idx));
      if (rem) {
        a[idx] = ((long long)a[idx] * x[idx] + y[idx]) % m[idx];
        if (value <= a[idx]) {
          q[t].push(make_pair(a[idx], make_pair(idx, rem - 1)));
        } else {
          q[t ^ 1].push(make_pair(a[idx], make_pair(idx, rem - 1)));
        }
      }
    }
    t ^= 1;
  }
  printf("%d\n", res);
  for (vector<pair<int, int> >::const_iterator it = answer.begin();
       it != answer.end(); it++) {
    printf("%d %d\n", it->first, it->second + 1);
  }
}
void solveFast() {
  int res = 0;
  for (int i = 0; i < n; i++) {
    int prevValue = a[i], cnt = 0;
    for (int j = 1; j < k[i]; j++) {
      a[i] = ((long long)a[i] * x[i] + y[i]) % m[i];
      cnt += prevValue > a[i];
      prevValue = a[i];
    }
    res = max(res, cnt);
  }
  printf("%d\n", res);
}
int main() {
  scanf("%d", &n);
  int sumK = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d%d", &k[i], &a[i], &x[i], &y[i], &m[i]);
    sumK += k[i];
  }
  if (sumK <= 200000) {
    solveSlow();
  } else {
    solveFast();
  }
  return 0;
}
