#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
int i, j, n;
long long k[2], a[2], x[2], y[2], m[2];
long long p[2][300000], pre[2005][2005][2];
int dp[2005][2005][2];
long long ans[200005][2];
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%I64d%I64d%I64d%I64d%I64d", &k[i], &a[i], &x[i], &y[i], &m[i]);
    p[i][1] = a[i];
    for (j = 2; j <= k[i]; j++) p[i][j] = (p[i][j - 1] * x[i] + y[i]) % m[i];
  }
  for (i = 0; i <= k[0]; i++)
    for (j = 0; j <= k[1]; j++) dp[i][j][0] = dp[i][j][1] = inf;
  dp[1][0][0] = dp[0][1][1] = 0;
  for (i = 0; i <= k[0]; i++) {
    for (j = 0; j <= k[1]; j++) {
      for (int l = 0; l < 2; l++) {
        int p1;
        if (l == 0)
          p1 = p[0][i];
        else
          p1 = p[1][j];
        if (p[0][i + 1] < p1) {
          if (dp[i][j][l] + 1 < dp[i + 1][j][0]) {
            dp[i + 1][j][0] = dp[i][j][l] + 1;
            pre[i + 1][j][0] = l;
          }
        } else {
          if (dp[i][j][l] < dp[i + 1][j][0]) {
            dp[i + 1][j][0] = dp[i][j][l];
            pre[i + 1][j][0] = l;
          }
        }
        if (p[1][j + 1] < p1) {
          if (dp[i][j][l] + 1 < dp[i][j + 1][1]) {
            dp[i][j + 1][1] = dp[i][j][l] + 1;
            pre[i][j + 1][1] = l;
          }
        } else {
          if (dp[i][j][l] < dp[i][j + 1][1]) {
            dp[i][j + 1][1] = dp[i][j][l];
            pre[i][j + 1][1] = l;
          }
        }
      }
    }
  }
  printf("%d\n", min(dp[k[0]][k[1]][0], dp[k[0]][k[1]][1]));
  int z;
  if (dp[k[0]][k[1]][0] < dp[k[0]][k[1]][1])
    z = 0;
  else
    z = 1;
  i = k[0], j = k[1];
  while (1) {
    if (i + j == 1) {
      if (z == 1) {
        ans[i + j][0] = p[1][1];
        ans[i + j][1] = 1;
      } else {
        ans[i + j][0] = p[0][1];
        ans[i + j][1] = 0;
      }
      break;
    }
    if (z == 0) {
      ans[i + j][0] = p[0][i];
      ans[i + j][1] = 0;
      z = pre[i][j][z];
      i--;
    } else {
      ans[i + j][0] = p[1][j];
      ans[i + j][1] = 1;
      z = pre[i][j][z];
      j--;
    }
  }
  for (i = 1; i <= k[0] + k[1]; i++) {
    printf("%I64d %I64d\n", ans[i][0], ans[i][1] + 1);
  }
  return 0;
}
