#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
// For Ideone
typedef long long __int64;
const int base = 1000;
const int digit = 3;
class bign
{
private:
int _arr[110];
int _m;
void _simplify (void);
public:
bign (void);
bign (int init);
friend istream& operator >> (istream& fin, bign& a);
friend bign operator / (bign a, int b);
friend int operator % (bign a, int b);
friend bool operator == (bign a, bign b);
};
void bign::_simplify (void)
{
for (int i = 0; i <= _m; i++)
{
if (i == _m && _arr[i] >= base) _m++;
_arr[i + 1] += _arr[i] / base;
_arr[i] %= base;
}
}
bign::bign (void) : _m(0) { memset(_arr, 0, sizeof(_arr)); }
bign::bign (int init) : _m(0)
{
memset(_arr, 0, sizeof(_arr));
_arr[0] = init;
_simplify();
}
istream& operator >> (istream& fin, bign& a)
{
char init[10010]; int len, b, t;
fin >> init;
len = strlen(init); a._m = -1;
for (int i = len - 1; i >= 0;)
{
t = 0, b = 1;
for (int j = 0; j < digit && i >= 0; j++, i--)
{
t += (init[i] - '0') * b;
b *= 10;
}
a._arr[++a._m] = t;
}
return fin;
}
bign operator / (bign a, int b)
{
for (int i = a._m; i >= 0; i--)
{
if (a._arr[i] < b && i == a._m && i != 0) a._m--;
if (i > 0) a._arr[i - 1] += (a._arr[i] % b) * base;
a._arr[i] /= b;
} return a;
}
int operator % (bign a, int b)
{
for (int i = a._m; i >= 0; i--)
{
if (i == 0) return a._arr[i] % b;
else a._arr[i - 1] += (a._arr[i] % b) * base;
}
}
bool operator == (bign a, bign b)
{
if (a._m != b._m) return false;
for (int i = 0; i <= a._m; i++)
if (a._arr[i] != b._arr[i]) return false;
return true;
}
int cha_a[110], cha_b[110];
int hm[510], hk[510], rm, rk, bs[510], md[510];
struct rec
{
__int64 gcd, crdx, crdy;
rec (void) {}
rec (__int64 g0, __int64 cx0, __int64 cy0) : gcd(g0), crdx(cx0), crdy(cy0) {}
};
rec euclid (__int64 x, __int64 y)
{
if (y == 0) return rec(x, 1, 0);
else
{
rec ans = euclid(y, x % y);
return rec(ans.gcd, ans.crdy, ans.crdx - (x / y) * ans.crdy);
}
}
pair<int, int> trans (int *cha, int hhm, int hhk)
{
int round = 0, cnt = 0, p = hhm;
while (1)
{
if (p == hhk) break;
else if (cnt && p == hhm) { cnt = -1; break; }
p = cha[p], ++cnt;
}
if (cnt == -1) return make_pair(-1, 0);
while (1)
{
if (round && p == hhk) break;
p = cha[p], ++round;
} return make_pair(cnt, round);
}
int main ()
{
bign m, k; int d; __int64 lm, lb, z;
while (1)
{
restart: cin >> d;
if (d == -1) break;
for (int i = 1; i < d; i++) cin >> cha_a[i];
for (int i = 0; i < d; i++) cin >> cha_b[i];
cin >> m >> k;
for (rm = 0; !(m == 0); rm++)
{
hm[rm] = m % d;
m = m / d;
}
for (rk = 0; !(k == 0); rk++)
{
hk[rk] = k % d;
k = k / d;
}
if (rm != rk) { printf("NO\n"); goto restart; }
for (int i = 0; i < rm - 1; i++)
{
pair<int, int> adp = trans(cha_b, hm[i], hk[i]);
if (adp.first == -1) { printf("NO\n"); goto restart; }
else md[i] = adp.first, bs[i] = adp.second;
}
pair<int, int> adp = trans(cha_a, hm[rm - 1], hk[rm - 1]);
if (adp.first == -1) { printf("NO\n"); goto restart; }
else md[rm - 1] = adp.first, bs[rm - 1] = adp.second;
lm = 0, lb = 1;
for (int i = 0; i < rm; i++)
{
rec adp = euclid(lb, bs[i]);
if ((lm - md[i]) % adp.gcd) { printf("NO\n"); goto restart; }
z = adp.crdy * ((lm - md[i]) / adp.gcd);
lb = lb / adp.gcd * bs[i];
lm = z * bs[i] + md[i];
lm = ((lm % lb) + lb) % lb;
}
// FOR POJ Submittion, please use:
// printf("%I64d\n", lm);
printf("%lld\n", lm);
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUg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W0mIGZpbiwgYmlnbiYgYSkKewogICAgY2hhciBpbml0WzEwMDEwXTsgaW50IGxlbiwgYiwgdDsKICAgIGZpbiA+PiBpbml0OwogICAgbGVuID0gc3RybGVuKGluaXQpOyBhLl9tID0gLTE7CiAgICBmb3IgKGludCBpID0gbGVuIC0gMTsgaSA+PSAwOykKICAgIHsKICAgICAgICB0ID0gMCwgYiA9IDE7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBkaWdpdCAmJiBpID49IDA7IGorKywgaS0tKQogICAgICAgIHsKICAgICAgICAgICAgdCArPSAoaW5pdFtpXSAtICcwJykgKiBiOwogICAgICAgICAgICBiICo9IDEwOwogICAgICAgIH0KICAgICAgICBhLl9hcnJbKythLl9tXSA9IHQ7CiAgICB9CiAgICByZXR1cm4gZmluOwp9CmJpZ24gb3BlcmF0b3IgLyAoYmlnbiBhLCBpbnQgYikKewogICAgZm9yIChpbnQgaSA9IGEuX207IGkgPj0gMDsgaS0tKQogICAgewogICAgICAgIGlmIChhLl9hcnJbaV0gPCBiICYmIGkgPT0gYS5fbSAmJiBpICE9IDApIGEuX20tLTsKICAgICAgICBpZiAoaSA+IDApIGEuX2FycltpIC0gMV0gKz0gKGEuX2FycltpXSAlIGIpICogYmFzZTsKICAgICAgICBhLl9hcnJbaV0gLz0gYjsKICAgIH0gcmV0dXJuIGE7Cn0KaW50IG9wZXJhdG9yICUgKGJpZ24gYSwgaW50IGIpCnsKICAgIGZvciAoaW50IGkgPSBhLl9tOyBpID49IDA7IGktLSkKICAgIHsKICAgICAgICBpZiAoaSA9PSAwKSByZXR1cm4gYS5fYXJyW2ldICUgYjsKICAgICAgICBlbHNlIGEuX2FycltpIC0gMV0gKz0gKGEuX2FycltpXSAlIGIpICogYmFzZTsKICAgIH0KfQpib29sIG9wZXJhdG9yID09IChiaWduIGEsIGJpZ24gYikKewogICAgaWYgKGEuX20gIT0gYi5fbSkgcmV0dXJuIGZhbHNlOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gYS5fbTsgaSsrKQogICAgICAgIGlmIChhLl9hcnJbaV0gIT0gYi5fYXJyW2ldKSByZXR1cm4gZmFsc2U7CiAgICByZXR1cm4gdHJ1ZTsKfQoKaW50IGNoYV9hWzExMF0sIGNoYV9iWzExMF07CmludCBobVs1MTBdLCBoa1s1MTBdLCBybSwgcmssIGJzWzUxMF0sIG1kWzUxMF07CnN0cnVjdCByZWMgCnsKICAgIF9faW50NjQgZ2NkLCBjcmR4LCBjcmR5OwogICAgcmVjICh2b2lkKSB7fQogICAgcmVjIChfX2ludDY0IGcwLCBfX2ludDY0IGN4MCwgX19pbnQ2NCBjeTApIDogZ2NkKGcwKSwgY3JkeChjeDApLCBjcmR5KGN5MCkge30KfTsKcmVjIGV1Y2xpZCAoX19pbnQ2NCB4LCBfX2ludDY0IHkpCnsKICAgIGlmICh5ID09IDApIHJldHVybiByZWMoeCwgMSwgMCk7CiAgICBlbHNlCiAgICB7CiAgICAgICAgcmVjIGFucyA9IGV1Y2xpZCh5LCB4ICUgeSk7CiAgICAgICAgcmV0dXJuIHJlYyhhbnMuZ2NkLCBhbnMuY3JkeSwgYW5zLmNyZHggLSAoeCAvIHkpICogYW5zLmNyZHkpOwogICAgfQp9CnBhaXI8aW50LCBpbnQ+IHRyYW5zIChpbnQgKmNoYSwgaW50IGhobSwgaW50IGhoaykKewogICAgaW50IHJvdW5kID0gMCwgY250ID0gMCwgcCA9IGhobTsKICAgIHdoaWxlICgxKQogICAgewogICAgICAgIGlmIChwID09IGhoaykgYnJlYWs7CiAgICAgICAgZWxzZSBpZiAoY250ICYmIHAgPT0gaGhtKSB7IGNudCA9IC0xOyBicmVhazsgfQogICAgICAgIHAgPSBjaGFbcF0sICsrY250OwogICAgfQogICAgaWYgKGNudCA9PSAtMSkgcmV0dXJuIG1ha2VfcGFpcigtMSwgMCk7CiAgICB3aGlsZSAoMSkKICAgIHsKICAgICAgICBpZiAocm91bmQgJiYgcCA9PSBoaGspIGJyZWFrOwogICAgICAgIHAgPSBjaGFbcF0sICsrcm91bmQ7CiAgICB9IHJldHVybiBtYWtlX3BhaXIoY250LCByb3VuZCk7Cn0KCmludCBtYWluICgpCnsKICAgIGJpZ24gbSwgazsgaW50IGQ7IF9faW50NjQgbG0sIGxiLCB6OwogICAgd2hpbGUgKDEpCiAgICB7CiAgICAgICAgcmVzdGFydDogY2luID4+IGQ7CiAgICAgICAgaWYgKGQgPT0gLTEpIGJyZWFrOwogICAgICAgIGZvciAoaW50IGkgPSAxOyBpIDwgZDsgaSsrKSBjaW4gPj4gY2hhX2FbaV07CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBkOyBpKyspIGNpbiA+PiBjaGFfYltpXTsKICAgICAgICBjaW4gPj4gbSA+PiBrOwogICAgICAgIGZvciAocm0gPSAwOyAhKG0gPT0gMCk7IHJtKyspCiAgICAgICAgewogICAgICAgICAgICBobVtybV0gPSBtICUgZDsKICAgICAgICAgICAgbSA9IG0gLyBkOwogICAgICAgIH0KICAgICAgICBmb3IgKHJrID0gMDsgIShrID09IDApOyByaysrKQogICAgICAgIHsKICAgICAgICAgICAgaGtbcmtdID0gayAlIGQ7CiAgICAgICAgICAgIGsgPSBrIC8gZDsKICAgICAgICB9CiAgICAgICAgaWYgKHJtICE9IHJrKSB7IHByaW50ZigiTk9cbiIpOyBnb3RvIHJlc3RhcnQ7IH0KICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHJtIC0gMTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgcGFpcjxpbnQsIGludD4gYWRwID0gdHJhbnMoY2hhX2IsIGhtW2ldLCBoa1tpXSk7CiAgICAgICAgICAgIGlmIChhZHAuZmlyc3QgPT0gLTEpIHsgcHJpbnRmKCJOT1xuIik7IGdvdG8gcmVzdGFydDsgfQogICAgICAgICAgICBlbHNlIG1kW2ldID0gYWRwLmZpcnN0LCBic1tpXSA9IGFkcC5zZWNvbmQ7CiAgICAgICAgfQogICAgICAgIHBhaXI8aW50LCBpbnQ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