import java.util.*;
class RickAndMorty {
public static void main
(String[] args
) { int n, m;
Scanner in
= new Scanner
(System.
in); int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
for(int i=0;i<=100;i++)
{
for(int j=0;j<=100;j++)
{
n=b+i*a;
m=d+j*c;
if(m==n)
{
s.add(n);
}
}
}
if(s.isEmpty())
{
} else
System.
out.
print(s.
firstElement()); }
}
aW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgUmlja0FuZE1vcnR5IHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgaW50IG4sIG07CiAgICAgICAgVmVjdG9yIHMgPSBuZXcgVmVjdG9yKCk7CiAgICAgICAgU2Nhbm5lciBpbiA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgaW50IGEgPSBpbi5uZXh0SW50KCk7CiAgICAgICAgaW50IGIgPSBpbi5uZXh0SW50KCk7CiAgICAgICAgaW50IGMgPSBpbi5uZXh0SW50KCk7CiAgICAgICAgaW50IGQgPSBpbi5uZXh0SW50KCk7CiAgICAgICAKICAgICAgICBmb3IoaW50IGk9MDtpPD0xMDA7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqPTA7ajw9MTAwO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbj1iK2kqYTsKICAgICAgICAgICAgICAgIG09ZCtqKmM7CiAgICAgICAgICAgICAgICBpZihtPT1uKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHMuYWRkKG4pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKHMuaXNFbXB0eSgpKQogICAgICAgIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCgtMSk7CiAgICAgICAgfSBlbHNlCiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQocy5maXJzdEVsZW1lbnQoKSk7CiAgICB9Cn0=