local function getgcd(x, y) while 0LL < x do x, y = y % x, x end return y end local function getlcm(x, y) local gcd = getgcd(x, y) return (x / gcd) * y end local x1, y1 = io.read("*n", "*n") local x2, y2 = io.read("*n", "*n") local x3, y3 = io.read("*n", "*n") local v = x1 * 1LL y1 = y1 * 1LL if v == 0LL then v = y1 end x2 = x2 * 1LL y2 = y2 * 1LL x3 = x3 * 1LL y3 = y3 * 1LL local f = false for irep = 1, 2000000 do if v % y2 == x2 then f = true break end v = v + y1 end if not f then print(-1) os.exit() end local lcm = getlcm(y1, y2) f = false for irep = 1, 2000000 do if v % y3 == x3 then f = true break end v = v + lcm if v < 0LL then break end end if not f then print(-1) os.exit() end v = tostring(v):gsub("LL", "") print(v)