import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(); long y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); int x3 = sc.nextInt(); int y3 = sc.nextInt(); if (x1 == 0 && x2 == 0 && x3 == 0) { System.out.println(lcm(lcm(y1, y2), y3)); return; } long ans = 0; for (int i = 0; i < 1000000; i++) { ans = y1 * i + x1; if (ans % y2 == x2) { break; } } if (ans % y2 != x2) { System.out.println(-1); return; } long y1y2 = lcm(y1, y2); long fine = 0; for (int i = 0; i < 1000000; i++) { fine = y1y2 * i + ans; if (fine % y3 == x3) { System.out.println(fine); return; } } System.out.println(-1); } static long lcm(long x, long y) { return x / gcd(x, y) * y; } static long gcd(long x, long y) { if (x % y == 0) { return y; } else { return gcd(y, x % y); } } }