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(); 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); } } }