#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 2, K = 2e5;
int n, k_total;
int32_t k[N], a[N], x[N], y[N], m[N], p[N];
bool greater_by_a(int i, int j) { return a[i] > a[j]; }
priority_queue<int32_t, vector<int32_t>, decltype(&greater_by_a)> q(
    greater_by_a);
vector<int32_t> q2;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> a[i] >> x[i] >> y[i] >> m[i];
    k_total += k[i];
    q.push(i);
  }
  int ans = 0, a0 = 0;
  vector<pii> ans2;
  for (;;) {
    while (!q.empty()) {
      int i = q.top();
      q.pop();
      ;
      if (a[i] >= a0) {
        if (k_total <= 2e5) ans2.emplace_back(a[i], i + 1);
        a0 = a[i];
        if (++p[i] < k[i]) {
          a[i] = ((int64_t)a[i] * x[i] + y[i]) % m[i];
          q.push(i);
        }
      } else {
        q2.push_back(i);
      }
    }
    if (q2.empty()) break;
    for (int i : q2) q.push(i);
    q2.clear();
    ans++, a0 = 0;
  }
  cout << ans << endl;
  if (k_total <= 2e5) {
    for (auto ai : ans2) {
      cout << ai.first << ' ' << ai.second << '\n';
    }
  }
  return 0;
}
