#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef LOCAL
freopen("rsir.in", "r", stdin);
freopen("rsir.out", "w", stdout);
cin.tie(nullptr)->sync_with_stdio(false);
#endif
int t0, t1, a, b, x, y, z, m; long long n;
cin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
auto f = [&](pair<long long, long long> pair) {
return make_pair(pair.second,
(1ll * a * pair.first * pair.first + 1ll * b * pair.second * pair.second +
x * pair.first + y * pair.second + z) % m);
};
t0 %= m;
t1 %= m;
pair<long long, long long> testoasa{t0, t1}, iepure{t0, t1};
iepure = f(iepure);
while (testoasa != iepure) {
testoasa = f(testoasa);
iepure = f(f(iepure));
}
int c = 0;
do {
testoasa = f(testoasa);
++c;
} while (testoasa != iepure);
testoasa = iepure = {t0, t1};
for (int i = 1; i <= c; ++i) {
iepure = f(iepure);
}
int p = 0;
while (testoasa != iepure) {
++p;
testoasa = f(testoasa);
iepure = f(iepure);
}
int target = n >= p ? p + int((n - p) % c) : int(n);
testoasa = {t0, t1};
for (int i = 0; i < target; ++i) {
testoasa = f(testoasa);
}
cout << testoasa.first << '\n';
return 0;
}