#include <bits/stdc++.h>
using namespace std;
struct SCIENT {
  int index;
  int k;
  int inv;
  long long a[200009];
} sc[3];
vector<pair<int, int> > v;
int K[3];
int dynam[2100][2100][3];
int recurse(int k1, int k2, int which) {
  int last;
  if (k1 == K[1] && k2 == K[2])
    last = INT_MIN;
  else if (which == 0)
    last = sc[which + 1].a[k1 + 1];
  else
    last = sc[which + 1].a[k2 + 1];
  if (dynam[k1][k2][which] != -1) return dynam[k1][k2][which];
  if (k1 == 0 && k2 == 0) return dynam[k1][k2][which] = 0;
  if (k1 == 0) {
    return dynam[k1][k2][which] = recurse(k1, k2 - 1, 1) + (sc[2].a[k2] < last);
  }
  if (k2 == 0) {
    return dynam[k1][k2][which] = recurse(k1 - 1, k2, 0) + (sc[1].a[k1] < last);
  }
  int p1 = recurse(k1 - 1, k2, 0) + (sc[1].a[k1] < last);
  int p2 = recurse(k1, k2 - 1, 1) + (sc[2].a[k2] < last);
  if (p1 < p2) {
    return dynam[k1][k2][which] = p1;
  } else {
    return dynam[k1][k2][which] = p2;
  }
}
int reconstruct(int k1, int k2, int which) {
  if (k1 == 0 && k2 == 0) return 0;
  int last;
  if (k1 == K[1] && k2 == K[2])
    last = INT_MIN;
  else if (which == 0)
    last = sc[which + 1].a[k1 + 1];
  else
    last = sc[which + 1].a[k2 + 1];
  if (k1 == 0) {
    v.push_back(make_pair(sc[2].a[k2], 2));
    return reconstruct(k1, k2 - 1, 1) + (sc[2].a[k2] < last);
  }
  if (k2 == 0) {
    v.push_back(make_pair(sc[1].a[k1], 1));
    return reconstruct(k1 - 1, k2, 0) + (sc[1].a[k1] < last);
  }
  int p1 = dynam[k1 - 1][k2][0] + (sc[1].a[k1] < last);
  int p2 = dynam[k1][k2 - 1][1] + (sc[2].a[k2] < last);
  if (p1 < p2) {
    v.push_back(make_pair(sc[1].a[k1], 1));
    reconstruct(k1 - 1, k2, 0);
    return p1;
  } else {
    v.push_back(make_pair(sc[2].a[k2], 2));
    reconstruct(k1, k2 - 1, 1);
    return p2;
  }
}
int main() {
  int N;
  scanf("%d", &N);
  memset(dynam, 0xff, sizeof(dynam));
  for (int i = 1; i <= N; i++) {
    long long k, a, x, y, m;
    cin >> k >> a >> x >> y >> m;
    K[i] = k;
    sc[i].k = k;
    sc[i].index = i;
    sc[i].a[1] = a;
    sc[i].inv = 0;
    for (int j = 2; j <= k; j++) sc[i].a[j] = (sc[i].a[j - 1] * x + y) % m;
    reverse(sc[i].a + 1, sc[i].a + k + 1);
  }
  int n = recurse(sc[1].k, sc[2].k, 2);
  reconstruct(sc[1].k, sc[2].k, 2);
  cout << n << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
  return 0;
}
