#include <cstdio>
typedef long long ll;
int a[20], m[20], t;
ll qmul(ll a, ll b, ll m) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % m;
a = (a << 1) % m;
b >>= 1;
}
return ans;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll CRT() {
ll ans = 0;
ll M = 1;
ll x, y;
for (int i = 1; i <= t; i++) M *= m[i];
for (int i = 1; i <= t; i++) {
ll Mi = M / m[i];
exgcd(Mi, m[i], x, y);
x = (x % m[i] + m[i]) % m[i]; //变成最小的解
ans = (ans + qmul(a[i] * Mi % M, x , M)) % M;
}
return (ans + M) % M; //防止为负数
}
int main() {
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
scanf("%d", a + i);
}
for (int i = 1; i <= t; i++) {
scanf("%d", m + i);
}
printf("%lld",CRT());
return 0;
}
#include <cstdio>
typedef long long ll;
int a[20], m[20], n;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll CRT() {
ll ans = 0;
ll M = 1;
ll t1, t2;
for (int i = 1; i <= n; i++) M *= m[i];
for (int i = 1; i <= n; i++) {
ll Mi = M / m[i];
exgcd(Mi, m[i], t1, t2);
ans = (ans + a[i] * Mi % M * t1) % M;
}
//return (ans % M + M) % M; //保证结果为正整数
return (ans + M) % M; //保证结果为正整数
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", m + i, a + i);
}
printf("%lld", CRT());
return 0;
}