rt
#include<bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f
using namespace std;
int a,b,c,d,e,f,g,p,x1,x2,y1,y2;
int ans;
int F(int x,int y)
{
return (a*x*x*x+b*y*y*y+c*x*x*y+d*x*y*y+e*x*y+f*x+g*y)%p;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>a>>b>>c>>d>>e>>f>>g>>p>>x1>>x2>>y1>>y2;
for(int x=x1;x<=x2;x++)
{
for(int y=y1;y<=y2;y++)
{
ans=max(ans,F(x,y));
}
}
cout<<ans;
return 0;
}