#!/usr/bin/env python3 import collections import math Equation = collections.namedtuple("Equation", "x y") NOSOL = -1 def lcm(a, b): return a // math.gcd(a, b) * b def solve(eq1, eq2, eq3): for i in range(eq2.y + 1): cand12 = eq1.x + eq1.y * i if cand12 > 0 and cand12 % eq2.y == eq2.x: sol12 = cand12 lcm12 = lcm(eq1.y, eq2.y) break else: return NOSOL for j in range(eq3.y + 2): cand123 = sol12 + lcm12 * j if cand123 > 0 and cand123 % eq3.y == eq3.x: return cand123 else: return NOSOL def main(): eq1 = Equation(*map(int, input().split())) eq2 = Equation(*map(int, input().split())) eq3 = Equation(*map(int, input().split())) print(solve(eq1, eq2, eq3)) if __name__ == '__main__': main()