#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
int a, b, c, d, ans = 0;
cin >> a >> b >> c >> d;
int t = b - d;
int vis[110];
memset(vis, 0, sizeof(vis));
while(t < 0) t += a;
//还需要研究,不太懂
while(t%c && !vis[t%c])
{
vis[t%c] = 1;
t += a;
}
if(t%c) cout << -1;
else cout << t + d;
return 0;
}
/*
20 2
9 19
82
*/
/*
2 1
16 12
-1
*/