#include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int> m,pair<int,int> n)
{
return m.first>n.first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string a,b;
while(getline(cin,a) and getline(cin,b))
{
stringstream s1,s2;
s1.str("");
s1.clear();
s2.str("");
s2.clear();
s1<<a;
s2<<b;
vector<pair<long long int,long long int>> v;
long long int x,y;
while(s1>>x and s2>>y)
{
v.push_back({x,y});
}
sort(v.begin(),v.end(),cmp);
long long int k=v[0].first,ans=v[0].second+k;
for(int i=1;i<v.size();i++)
{
while(ans%v[i].first!=v[i].second)
{
ans+=k;
}
k*=v[i].first;
//cout<<ans<<'\n';
}
cout<<ans<<'\n';
}
}