import java.util.*; import java.io.*; import static java.util.Arrays.*; import static java.lang.Math.*; public class No0186 { static final Scanner in = new Scanner(System.in); static final PrintWriter out = new PrintWriter(System.out,false); static void solve() { boolean[][] f = new boolean[3][1000001]; for (int i=0; i<3; i++) { int x = in.nextInt(); int y = in.nextInt(); for (int j=x; j<1000001; j+=y) { f[i][j] = true; } } for (int i=0; i<1_000_001; i++) { if (f[0][i] && f[1][i] && f[2][i]) { out.println(i); return; } } out.println("-1"); } public static void main(String[] args) { long start = System.currentTimeMillis(); solve(); out.flush(); long end = System.currentTimeMillis(); //trace(end-start + "ms"); in.close(); out.close(); } static void trace(Object... o) { System.out.println(deepToString(o));} }