module main; // https://kmjp.hatenablog.jp/entry/2015/04/20/0900 より // 中国剰余定理、総当たり import std; void main() { // 入力 long[3] X, Y; foreach (i; 0 .. 3) readln.chomp.formattedRead("%d %d", X[i], Y[i]); // 答えの計算と出力 long x = 0, y; foreach (i; 0 .. Y[1] + 1) { long xx = X[0] + Y[0] * i; if (xx > 0 && xx % Y[1] == X[1]) { x = xx; y = lcm(Y[0], Y[1]); break; } } if (x == 0) { writeln(-1); return; } foreach (i; 0 .. Y[2] + 1) { long xx = x + y * i; if (xx > 0 && xx % Y[2] == X[2]) { writeln(xx); return; } } writeln(-1); }