import java.util.Scanner; public class Main { private static final int MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = 3; long[] X = new long[N]; long[] Y = new long[N]; for (int i = 0; i < N; i++) { X[i] = sc.nextInt(); Y[i] = sc.nextInt(); } sc.close(); for (int i = 1; i < N; i++) { long[] cmt = cmt(X[0], Y[0], X[i], Y[i]); if (cmt[0] < 0) { System.out.println(-1); return; } X[0] = cmt[0]; Y[0] = cmt[1]; } if (X[0] == 0) { X[0] = Y[0]; } System.out.println(X[0]); } static long[] extgcd(long a, long b, long[] is) { if (a == 0) { is[0] = 0; is[1] = 1; is[2] = b; } else { extgcd(b % a, a, is); long x = is[1] - b / a * is[0]; is[1] = is[0]; is[0] = x; } return is; } static long[] cmt(long a1, long mo1, long a2, long mo2) { long[] exgcd = extgcd(mo1, mo2, new long[3]); long g = exgcd[2]; long x = exgcd[0]; a1 %= mo1; a2 %= mo2; if (a1 % exgcd[2] != a2 % exgcd[2]) { return new long[] { -1, 0 }; } long lcm = mo1 * (mo2 / g); long v = a1 + ((a2 - a1) % lcm) * x % lcm * (mo1 / g); return new long[] { ((v % lcm) + lcm) % lcm, lcm }; } }