#include <bits/stdc++.h>
#pragma comment(linker, "/stack:100000000")
using namespace std;
const int MAX = 200000 + 10;
int a[2][MAX];
int main() {
  int n;
  scanf("%d", &n);
  int k[2], p[2] = {0};
  for (int(i) = 0; (i) < (n); (i)++) {
    int s, x, y, m;
    scanf("%d %d %d %d %d", &k[i], &s, &x, &y, &m);
    a[i][0] = s;
    for (int j = 1; j < k[i]; j++) {
      a[i][j] = ((long long)a[i][j - 1] * x + y) % m;
    }
  }
  vector<pair<int, int> > res;
  int last = 0;
  while (p[0] < k[0] && p[1] < k[1]) {
    int j = -1;
    for (int(i) = 0; (i) < (2); (i)++) {
      if (a[i][p[i]] >= last && (j == -1 || a[i][p[i]] < a[j][p[j]])) {
        j = i;
      }
    }
    if (j == -1) {
      if (a[0][p[0]] < a[1][p[1]]) {
        res.push_back(make_pair(a[0][p[0]], 0));
        last = a[0][p[0]];
        p[0]++;
      } else {
        res.push_back(make_pair(a[1][p[1]], 1));
        last = a[1][p[1]];
        p[1]++;
      }
    } else {
      res.push_back(make_pair(a[j][p[j]], j));
      last = a[j][p[j]];
      p[j]++;
    }
  }
  for (int(i) = 0; (i) < (2); (i)++) {
    while (p[i] < k[i]) {
      res.push_back(make_pair(a[i][p[i]], i));
      p[i]++;
    }
  }
  int ans = 0;
  for (int(i) = 0; (i) < ((int)(res).size() - 1); (i)++) {
    if (res[i].first > res[i + 1].first) {
      ++ans;
    }
  }
  printf("%d\n", ans);
  if ((int)(res).size() <= 200000) {
    for (int(i) = 0; (i) < ((int)(res).size()); (i)++) {
      printf("%d %d\n", res[i].first, res[i].second + 1);
    }
  }
  return 0;
}
