#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
while(b<=10000000){
if(b>d)d+=c;
if(b<d)b+=a;
if(b==d){
cout<<b;
return 0;
}
}
cout<<-1;
}