#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll mod = 1e9 + 7;
vector<pii> merge(vector<int>& a, vector<int>& b) {
  int ptrA = 0, ptrB = 0;
  vector<pii> c;
  int last = INT_MIN;
  while (ptrA < a.size() && ptrB < b.size()) {
    if (a[ptrA] < b[ptrB]) {
      if (a[ptrA] >= last || b[ptrB] < last)
        c.push_back({a[ptrA++], 1});
      else
        c.push_back({b[ptrB++], 2});
    } else {
      if (b[ptrB] >= last || a[ptrA] < last)
        c.push_back({b[ptrB++], 2});
      else
        c.push_back({a[ptrA++], 1});
    }
    last = c.back().first;
  }
  while (ptrA < a.size()) c.push_back({a[ptrA++], 1});
  while (ptrB < b.size()) c.push_back({b[ptrB++], 2});
  return c;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v[2];
  for (int i = 0; i < n; i++) {
    ll k, a, x, y, m;
    cin >> k >> a >> x >> y >> m;
    v[i].push_back(a);
    for (int j = 1; j < k; j++) {
      a = (a * x + y) % m;
      v[i].push_back(a);
    }
  }
  vector<pii> ans = merge(v[0], v[1]);
  int res = 0;
  for (int i = 1; i < ans.size(); i++) {
    if (ans[i].first < ans[i - 1].first) res++;
  }
  cout << res << endl;
  for (pii& i : ans) cout << i.first << " " << i.second << "\n";
  return 0;
}
