#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>usingnamespace std;typedeflonglong ll;
ll x,y;voidexgcd(ll a,ll b){if(b ==0){
x =1;
y =0;return;}exgcd(b,a%b);
ll t = y;
y = x - a / b * t;
x = t;}
ll getgcd(ll a, ll b){if(b ==0)return a;elsereturngetgcd(b, a % b);}intmain(){
ll A, B, C, k, a, b, c, n, gcd, n1;while(1){scanf("%lld%lld%lld%lld",&A,&B,&C,&k);if(A+B+C+k ==0)break;
n =(ll)1<<k;
a = C;
b = B-A;if(b ==0){printf("0\n");continue;}
gcd =getgcd(a,n);if(b % gcd !=0|| n < B){printf("FOREVER\n");continue;}exgcd(a,n);
x = x * b / gcd;
n1 = n / gcd;if(x <=0){
x =(x % n1 + n1)% n1;}else x = x % n1;printf("%lld\n",x);}return0;}