#include <bits/stdc++.h>
using namespace std;
int N;
int ki[(4)];
vector<int> A[(4)];
vector<int> sol;
int main() {
  int i, j;
  scanf("%d", &N);
  for (i = 0; i < N; ++i) {
    int a, b, c, d, e;
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
    long long x, y, m;
    A[i].resize(a);
    ki[i] = a;
    A[i][0] = b;
    x = c;
    y = d;
    m = e;
    for (j = 1; j < a; ++j) A[i][j] = ((long long)A[i][j - 1] * x + y) % m;
  }
  i = 0;
  j = 0;
  int last = 0;
  int bad = 0;
  int sa = ((ki[0] + ki[1]) <= 200000);
  while (i < ki[0] || j < ki[1]) {
    int a;
    if (j == ki[1]) {
      a = A[0][i++];
      if (sa) sol.push_back(0);
    } else if (i == ki[0]) {
      a = A[1][j++];
      if (sa) sol.push_back(1);
    } else {
      int b = A[0][i];
      int c = A[1][j];
      if (b >= last) {
        if (c >= last) {
          if (b <= c) {
            a = b;
            ++i;
            if (sa) sol.push_back(0);
          } else {
            a = c;
            ++j;
            if (sa) sol.push_back(1);
          }
        } else {
          a = b;
          ++i;
          if (sa) sol.push_back(0);
        }
      } else {
        if (c < last) {
          if (b <= c) {
            a = b;
            ++i;
            if (sa) sol.push_back(0);
          } else {
            a = c;
            ++j;
            if (sa) sol.push_back(1);
          }
        } else {
          a = c;
          ++j;
          if (sa) sol.push_back(1);
        }
      }
    }
    if (last > a) ++bad;
    last = a;
  }
  printf("%d\n", bad);
  if (sa) {
    int sz = sol.size();
    j = 0;
    int k = 0;
    for (i = 0; i < sz; ++i) {
      if (sol[i] == 0)
        printf("%d 1\n", A[0][j++]);
      else
        printf("%d 2\n", A[1][k++]);
    }
  }
  return 0;
}
