#include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int> m,pair<int,int> n)
{
return m.first>n.first;
}
int main()
{
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<int,int>> v;
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';
}
}