import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; T exEuclid(T)(T a, T b, ref T x, ref T y) { auto g = a; x = 1; y = 0; if (b != 0) { g = exEuclid(b, a % b, y, x); y -= a / b * x; } return g; } void main() { auto rd1 = readln.split.map!(a => BigInt(a)); auto x1 = rd1[0], y1 = rd1[1]; auto rd2 = readln.split.map!(a => BigInt(a)); auto x2 = rd2[0], y2 = rd2[1]; auto rd3 = readln.split.map!(a => BigInt(a)); auto x3 = rd3[0], y3 = rd3[1]; BigInt a, b, g, l, r; g = exEuclid(y1, y2, a, b); if (x1 % g != x2 % g) { writeln(-1); return; } l = y1 / g * y2; r = (y1 / g * x2 * a + y2 / g * x1 * b) % l; BigInt x4 = r, y4 = l; g = exEuclid(y3, y4, a, b); if (x3 % g != x4 % g) { writeln(-1); return; } l = y3 / g * y4; r = (y3 / g * x4 * a + y4 / g * x3 * b) % l; writeln(r); }