package yukicoder; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] X = new int[3]; int[] Y = new int[3]; for (int i = 0; i < 3; i++) { X[i] = sc.nextInt(); Y[i] = sc.nextInt(); } sc.close(); for (long i = 1; i < 10000000; i++) { boolean check = true; for (int j = 0; j < 3; j++) { if (i % Y[j] != X[j]) { check = false; break; } } if (check) { System.out.println(i); return; } } System.out.println(-1); } }