#include <iostream>
#include <math.h>
using namespace std;
long long ans =0;
int main() {
int a, b, c, d, e, f, g, p, x, xx, y, yy;
cin >> a >> b >> c >> d >> e >> f >> g ;
cin>> p >> x >> xx >> y >> yy;
for (int i = x; i <= xx; i++) {
for (int j = y; j <= yy; j++) {
long long g1;
long long g2;
long long g3;
g1 = ((a * i * i * i) % p + (b * j * j * j) % p) % p;
g2 = ((c * j * i * i) % p + (d * i * j * j) % p) % p;
g3 = ((e * i * j) % p + (f * i) % p + (g * j) % p) % p;
long long g4= (g1 + g2 + g3) % p;
ans = max(g4, ans);
}
}
cout << ans % p;
return 0;
}