#include <bits/stdc++.h>
using namespace std;
const int SIZE = 5 << 10;
int n, cnt[SIZE], curr[SIZE];
vector<int> arr[SIZE];
vector<pair<int, int> > s;
set<pair<int, int> > active;
int main() {
  scanf("%d", &n);
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &cnt[i]);
    int a, x, y, m;
    scanf("%d%d%d%d", &a, &x, &y, &m);
    arr[i].resize(cnt[i]);
    arr[i][0] = a;
    for (int j = 1; j < cnt[i]; ++j)
      arr[i][j] = ((long long)(arr[i][j - 1]) * x + y) % m;
    sum += cnt[i];
  }
  if (sum <= 200000) {
    for (int i = 0; i < n; ++i) active.insert(make_pair(arr[i][0], i));
    int last = -1;
    int ans = -1;
    while (1) {
      set<pair<int, int> >::iterator it;
      it = active.lower_bound(make_pair(last, -1));
      if (it == active.end()) {
        ++ans;
        it = active.begin();
      }
      if (it == active.end()) break;
      int best = it->second;
      last = arr[best][curr[best]];
      active.erase(make_pair(arr[best][curr[best]], best));
      curr[best]++;
      if (curr[best] < cnt[best])
        active.insert(make_pair(arr[best][curr[best]], best));
      if (s.size() < 210000) s.push_back(make_pair(last, best + 1));
    }
    printf("%d\n", ans);
    if (s.size() <= 200000)
      for (int i = 0; i < s.size(); ++i)
        printf("%d %d\n", s[i].first, s[i].second);
  } else {
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int bad = 0;
      for (int j = 1; j < cnt[i]; ++j)
        if (arr[i][j] < arr[i][j - 1]) ++bad;
      ans = max(ans, bad);
    }
    printf("%d\n", ans);
  }
}
