import java.util.Arrays; import java.util.Scanner; class Scratch { public static long mod_inv(long a, long m){ return (a == 1 ? 1 : (1 - m*mod_inv(m%a, a)) / a + m); } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a % b); } public static boolean make_so(long[] as, long[] ms){ for(int fst = 0; fst < ms.length; fst++){ for(int snd = fst + 1; snd < ms.length; snd++){ long gcd = gcd(ms[fst], ms[snd]); if(gcd == 1){ continue; } if(as[fst] % gcd != as[snd] % gcd){ return false; } ms[fst] /= gcd; ms[snd] /= gcd; while(true){ long gt = gcd(ms[fst], gcd); if(gt == 1){ break; } ms[fst] *= gt; gcd /= gt; } ms[snd] *= gcd; as[fst] %= ms[fst]; as[snd] %= ms[snd]; } } return true; } public static long chinese_remainder(long[] as, long[] ms){ long prod = 1, sum = 0; long v = 0; for(int i = 0; i < ms.length; i++){ long sub = as[i] - (sum % ms[i]); if(sub < 0){ sub += ms[i]; } v = (sub * mod_inv(prod, ms[i])) % ms[i]; sum += (v * prod); prod *= ms[i]; } return sum; } public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { final long x1 = sc.nextLong(); final long y1 = sc.nextLong(); final long x2 = sc.nextLong(); final long y2 = sc.nextLong(); final long x3 = sc.nextLong(); final long y3 = sc.nextLong(); long[] as = new long[]{x1, x2, x3}; long[] ms = new long[]{y1, y2, y3}; final boolean so = make_so(as, ms); //System.out.println(Arrays.toString(as)); //System.out.println(Arrays.toString(ms)); if(!so){ System.out.println(-1); return; } long prod = 1; for(final long p : ms){ prod *= p; } final long ret = chinese_remainder(as, ms); System.out.println(ret == 0 ? prod : ret); } } }