A,B,a,b = map(int,input().split()) for x in range(1,3000+1): if x%A == a and x%B == b: print(x) exit()