/* -*- coding: utf-8 -*- * * 186.cc: No.186 中華風 (Easy) - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ /* typedef */ typedef long long ll; /* global variables */ /* subroutines */ ll xs[3], ys[3]; /* main */ int main() { for (int i = 0; i < 3; i++) cin >> xs[i] >> ys[i]; ll y01 = ys[0] * ys[1] / __gcd((int)ys[0], (int)ys[1]); for (ll k = 0; k <= ys[1]; k++) { ll z0 = xs[0] + k * ys[0]; if (z0 % ys[1] == xs[1]) { for (ll l = 0; l <= ys[2]; l++) { //printf("k=%lld,l=%lld\n", k, l); ll z1 = z0 + y01 * l; if (z1 % ys[2] == xs[2]) { printf("%lld\n", z1); return 0; } } } } puts("-1"); return 0; }