#include<bits/stdc++.h> #define lowbit(x) (x & -x) #define ll long long usingnamespace std;
constint N = 3e7 + 66;
int n, res; ll S, B, C, D; int a[N], b[N], tree[N];
inlinevoidchenge(int x, int c) { for (; x <= n; x += lowbit(x)) tree[x] += c; return; }
inlinequery(int x) { int ret = 0; while (x) { ret += tree[x]; x -= lowbit(x); } return ret; }
signedmain() { int i; n = read(), S = read(), B = read(), C = read(), D = read(); for (i = 1; i <= n; ++ i) { a[i] = i; S = (S * B + C) % D; swap(a[i], a[(S % i) + 1]); } // cin >> n; // for (i = 1; i <= n; ++ i) a[i] = read();
for (i = 1; i <= n; ++ i) { // chenge(a[i], 1); // res = max(res, i - query(a[i])); res = max(res, i - a[i]); } put(res); return0; }