#include <bits/stdc++.h>
int const maxscientists = 2;
int const maxprosize = 2000;
int scientists = 0;
int pro[maxscientists], x[maxscientists], y[maxscientists], m[maxscientists],
    curindex[maxscientists];
long long a[maxscientists];
int bestmemindex[maxscientists * maxprosize];
int bestsciindex[maxscientists * maxprosize];
int dp[maxscientists][maxscientists][2];
int main() {
  int i, j, sciindex, k;
  scanf("%d", &scientists);
  for (i = 0; i < scientists; ++i)
    scanf("%d%I64d%d%d%d", &pro[i], &a[i], &x[i], &y[i], &m[i]);
  int pre = -1, result = 0, curtask = 0;
  int leastindex = -1;
  while (true) {
    sciindex = -1;
    leastindex = -1;
    k = 1000000001;
    for (i = 0; i < scientists; ++i) {
      if (curindex[i] < pro[i]) {
        if (a[i] < k) {
          k = a[i];
          leastindex = i;
        }
        if (a[i] >= pre && (sciindex == -1 || a[i] < a[sciindex])) sciindex = i;
      }
    }
    if (k == 1000000001) {
      printf("%d\n", result);
      for (i = 0; i < curtask; ++i)
        printf("%d %d\n", bestmemindex[i], bestsciindex[i]);
      return 0;
    } else {
      if (sciindex == -1) {
        sciindex = leastindex;
        if (sciindex == -1) {
          printf("%d\n", result);
          for (i = 0; i < curtask; ++i)
            printf("%d %d\n", bestmemindex[i], bestsciindex[i]);
          return 0;
        }
      }
      k = a[sciindex];
      bestmemindex[curtask] = k;
      bestsciindex[curtask] = sciindex + 1;
      a[sciindex] = (a[sciindex] * x[sciindex] + y[sciindex]) % m[sciindex];
      ++curtask;
      if (pre > k) ++result;
      pre = k;
      ++curindex[sciindex];
    }
  }
  return 0;
}
