#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3fll;
long long int getint() {
  long long int ret = 0;
  bool f = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f ^= 1;
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return f ? -ret : ret;
}
using namespace std;
const int MAXN = 5000;
int a[MAXN + 10], x[MAXN + 10], y[MAXN + 10], mod[MAXN + 10], k[MAXN + 10];
int ans[200100], blg[200100];
struct cmp {
  bool operator()(int i, int j) const { return a[i] < a[j]; }
};
multiset<int, cmp> st;
int main() {
  int n = getint();
  int top = 0;
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    k[i] = getint(), a[i] = getint();
    x[i] = getint(), y[i] = getint(), mod[i] = getint();
    st.insert(i);
    sum += k[i];
  }
  int ret = 0;
  if (sum <= 200000) {
    a[0] = -INF;
    while (!st.empty()) {
      set<int, cmp>::iterator it = st.lower_bound(0);
      if (it == st.end()) {
        it = st.begin();
        ++ret;
      }
      int p = *it;
      st.erase(it);
      a[0] = ans[++top] = a[p];
      blg[top] = p;
      --k[p];
      if (k[p]) {
        a[p] = ((long long int)a[p] * x[p] + y[p]) % mod[p];
        st.insert(p);
      }
    }
    printf("%d\n", ret);
    for (int i = 1; i <= top; ++i) printf("%d %d\n", ans[i], blg[i]);
  } else {
    for (int i = 1; i <= n; ++i) {
      int cur = -INF;
      int tmp = 0;
      for (int j = 1; j <= k[i]; ++j) {
        if (cur > a[i]) ++tmp;
        cur = a[i];
        a[i] = ((long long int)a[i] * x[i] + y[i]) % mod[i];
      }
      ret = max(ret, tmp);
    }
    printf("%d\n", ret);
  }
}
