#include<cassert>
#include<cstdio>
#include<tuple>
using namespace std;
typedef long long ll;
struct mat2{
ll a, b, c, d;
mat2(const ll &a=0, const ll &b=0, const ll &c=0, const ll &d=0): a(a), b(b), c(c), d(d){}
mat2 operator *(const mat2 &B) const{
return mat2(a*B.a+b*B.c, a*B.b+b*B.d, c*B.a+d*B.c, c*B.b+d*B.d);
}
mat2 operator %(const ll &p) const{
return mat2(a%p, b%p, c%p, d%p);
}
};
mat2 pow(const mat2 &A, const ll &p, ll n){
if(n == 1){
return A;
}
mat2 B = pow(A, p, n/2);
B = B*B%p;
if(n%2){
B = A*B%p;
}
return B;
}
tuple<int, int, int> extgcd(const int &a0, const int &b0){
ll a=a0, b=b0, p=1, q=0, r=0, s=1;
while(b){
ll k = a/b;
tie(p, r) = make_pair(r, ((p-k*r)%b0+b0)%b0);
tie(q, s) = make_pair(s, ((q-k*s)%a0+a0)%a0);
tie(a, b) = make_pair(b, a%b);
}
return make_tuple((int)a, (int)p, (int)q);
}
int mod_inv(int a, int n){
tuple<int, int, int> t = extgcd(a, n);
assert(get<0>(t) == 1);
return get<1>(t);
}
int main(){
ll b, c, n, p, q;
scanf("%lld%lld%lld%lld%lld", &b, &c, &n, &p, &q);
mat2 A(c%p, b%p, 1, 0);
A = pow(A, p, n);
ll x = (A.a+A.b)%p;
if(x == 0){
puts("-1");
return 0;
}
ll xinv = mod_inv((int)x, p)%q;
A = mat2(c%q, b%q, 1, 0);
A = pow(A, q, n);
ll y = (A.c+A.d)%q;
printf("%lld\n", xinv*y%q);
return 0;
}
I2luY2x1ZGU8Y2Fzc2VydD4KI2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTx0dXBsZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CgpzdHJ1Y3QgbWF0MnsKICAgIGxsIGEsIGIsIGMsIGQ7CiAgICBtYXQyKGNvbnN0IGxsICZhPTAsIGNvbnN0IGxsICZiPTAsIGNvbnN0IGxsICZjPTAsIGNvbnN0IGxsICZkPTApOiBhKGEpLCBiKGIpLCBjKGMpLCBkKGQpe30KICAgIG1hdDIgb3BlcmF0b3IgKihjb25zdCBtYXQyICZCKSBjb25zdHsKICAgICAgICByZXR1cm4gbWF0MihhKkIuYStiKkIuYywgYSpCLmIrYipCLmQsIGMqQi5hK2QqQi5jLCBjKkIuYitkKkIuZCk7CiAgICB9CiAgICBtYXQyIG9wZXJhdG9yICUoY29uc3QgbGwgJnApIGNvbnN0ewogICAgICAgIHJldHVybiBtYXQyKGElcCwgYiVwLCBjJXAsIGQlcCk7CiAgICB9Cn07CgptYXQyIHBvdyhjb25zdCBtYXQyICZBLCBjb25zdCBsbCAmcCwgbGwgbil7CiAgICBpZihuID09IDEpewogICAgICAgIHJldHVybiBBOwogICAgfQogICAgbWF0MiBCID0gcG93KEEsIHAsIG4vMik7CiAgICBCID0gQipCJXA7CiAgICBpZihuJTIpewogICAgICAgIEIgPSBBKkIlcDsKICAgIH0KICAgIHJldHVybiBCOwp9Cgp0dXBsZTxpbnQsIGludCwgaW50PiBleHRnY2QoY29uc3QgaW50ICZhMCwgY29uc3QgaW50ICZiMCl7CiAgICBsbCBhPWEwLCBiPWIwLCBwPTEsIHE9MCwgcj0wLCBzPTE7CiAgICB3aGlsZShiKXsKICAgICAgICBsbCBrID0gYS9iOwogICAgICAgIHRpZShwLCByKSA9IG1ha2VfcGFpcihyLCAoKHAtaypyKSViMCtiMCklYjApOwogICAgICAgIHRpZShxLCBzKSA9IG1ha2VfcGFpcihzLCAoKHEtaypzKSVhMCthMCklYTApOwogICAgICAgIHRpZShhLCBiKSA9IG1ha2VfcGFpcihiLCBhJWIpOwogICAgfQogICAgcmV0dXJuIG1ha2VfdHVwbGUoKGludClhLCAoaW50KXAsIChpbnQpcSk7Cn0KCmludCBtb2RfaW52KGludCBhLCBpbnQgbil7CiAgICB0dXBsZTxpbnQsIGludCwgaW50PiB0ID0gZXh0Z2NkKGEsIG4pOwogICAgYXNzZXJ0KGdldDwwPih0KSA9PSAxKTsKICAgIHJldHVybiBnZXQ8MT4odCk7Cn0KCmludCBtYWluKCl7CiAgICBsbCBiLCBjLCBuLCBwLCBxOwogICAgc2NhbmYoIiVsbGQlbGxkJWxsZCVsbGQlbGxkIiwgJmIsICZjLCAmbiwgJnAsICZxKTsKICAgIG1hdDIgQShjJXAsIGIlcCwgMSwgMCk7CiAgICBBID0gcG93KEEsIHAsIG4pOwogICAgbGwgeCA9IChBLmErQS5iKSVwOwogICAgaWYoeCA9PSAwKXsKICAgICAgICBwdXRzKCItMSIpOwogICAgICAgIHJldHVybiAwOwogICAgfQogICAgbGwgeGludiA9IG1vZF9pbnYoKGludCl4LCBwKSVxOwogICAgQSA9IG1hdDIoYyVxLCBiJXEsIDEsIDApOwogICAgQSA9IHBvdyhBLCBxLCBuKTsKICAgIGxsIHkgPSAoQS5jK0EuZCklcTsKICAgIHByaW50ZigiJWxsZFxuIiwgeGludip5JXEpOwogICAgcmV0dXJuIDA7Cn0K