#include <bits/stdc++.h>
using namespace std;
const int N = 200009;
bool flag1, flag2;
int n, m, rb, x, y, mo, now, ans;
int a[N], b[N];
int main() {
  scanf("%d", &rb);
  scanf("%d %d %d %d %d", &n, a + 1, &x, &y, &mo);
  for (int i = 2; i <= n; i++) {
    a[i] = ((long long)a[i - 1] * x + y) % mo;
    now += (a[i] < a[i - 1]);
  }
  ans = now;
  now = 0;
  scanf("%d %d %d %d %d", &m, b + 1, &x, &y, &mo);
  for (int i = 2; i <= m; i++) {
    b[i] = ((long long)b[i - 1] * x + y) % mo;
    now += (b[i] < b[i - 1]);
  }
  if (now > ans) ans = now;
  printf("%d\n", ans);
  if (n + m > 200000) return 0;
  int i = 1, j = 1;
  flag1 = flag2 = false;
  while (i <= n && j <= m) {
    if (flag1 && flag2) {
      flag1 = flag2 = false;
      continue;
    } else if (flag2 || (!flag1 && a[i] <= b[j])) {
      printf("%d 1\n", a[i++]);
      if (a[i] < a[i - 1]) flag1 = true;
    } else {
      printf("%d 2\n", b[j++]);
      if (b[j] < b[j - 1]) flag2 = true;
    }
  }
  for (; i <= n; printf("%d 1\n", a[i++]))
    ;
  for (; j <= m; printf("%d 2\n", b[j++]))
    ;
}
