#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long k[n + 1], a[n + 1], x[n + 1], y[n + 1], m[n + 1];
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> k[i] >> a[i] >> x[i] >> y[i] >> m[i];
    sum += k[i];
  }
  if (sum > 2e5) {
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      vector<long long> b;
      while (k[i]--) {
        b.push_back(a[i]);
        a[i] = (a[i] * x[i]) % m[i];
        a[i] = (a[i] + y[i]) % m[i];
      }
      long long now = 0;
      for (long long j = 1; j < b.size(); j++) {
        if (b[j] < b[j - 1]) {
          now++;
        }
      }
      ans = max(ans, now);
    }
    cout << ans << "\n";
    return 0;
  }
  vector<long long> adj[n + 1];
  vector<pair<pair<long long, long long>, long long>> res;
  for (long long i = 1; i <= n; i++) {
    while (k[i]--) {
      adj[i].push_back(a[i]);
      a[i] = (a[i] * x[i]) % m[i];
      a[i] = (a[i] + y[i]) % m[i];
    }
    k[i] = adj[i].size();
    int pre[k[i]];
    for (int j = 0; j < k[i]; j++) {
      pre[j] = 0;
    }
    for (int j = 1; j < k[i]; j++) {
      if (adj[i][j - 1] > adj[i][j])
        pre[j] = pre[j - 1] + 1;
      else
        pre[j] = pre[j - 1];
    }
    for (int j = 0; j < k[i]; j++) {
      res.push_back({{pre[j], adj[i][j]}, i});
    }
  }
  sort(res.begin(), res.end());
  long long ans = 0;
  for (int i = 1; i < res.size(); i++) {
    if (res[i - 1].first.second > res[i].first.second) ans++;
  }
  cout << ans << "\n";
  for (auto u : res) {
    cout << u.first.second << " " << u.second << "\n";
  }
  return 0;
}
