#include <bits/stdc++.h>
using namespace std;
enum { MAXN = 5050, MAXK = 200010 };
int cnum[MAXN], pval[MAXN], x[MAXN], y[MAXN], m[MAXN], ctot[MAXN];
pair<int, int> res[MAXK];
void solveA(int n, int sumk) {
  set<pair<int, int> > snum;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    snum.insert(pair<int, int>(pval[i], i));
    ctot[i] = 0;
  }
  for (int i = 0, lst = 0; i < sumk; i++) {
    set<pair<int, int> >::iterator pos =
        snum.lower_bound(pair<int, int>(lst, 0));
    if (pos == snum.end()) {
      pos = snum.begin();
      ++ans;
    }
    res[i] = *pos;
    snum.erase(pos);
    lst = res[i].first;
    const int &p = res[i].second;
    if (++ctot[p] < cnum[p]) {
      snum.insert(
          pair<int, int>(((long long)res[i].first * x[p] + y[p]) % m[p], p));
    }
  }
  printf("%d\n", ans);
  for (int i = 0; i < sumk; i++)
    printf("%d %d\n", res[i].first, res[i].second + 1);
}
void solveB(int n) {
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 1, v = pval[i], nv; j < cnum[i]; j++) {
      nv = ((long long)v * x[i] + y[i]) % m[i];
      if (nv < v) ++cnt;
      v = nv;
    }
    ans = max(cnt, ans);
  }
  printf("%d\n", ans);
}
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d%d", &cnum[i], &pval[i], &x[i], &y[i], &m[i]);
    sum += cnum[i];
  }
  if (sum <= 200000)
    solveA(n, sum);
  else
    solveB(n);
  return 0;
}
