use std::io::*; fn gcd(a: u64, b: u64) -> u64 { if b == 0 { return a; } gcd(b, a % b) } fn lcm(a: u64, b: u64) -> u64 { a / gcd(a, b) * b } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let mut x = vec![0u64; 3]; let mut y = vec![0u64; 3]; for i in 0..3 { x[i] = itr.next().unwrap().parse().unwrap(); y[i] = itr.next().unwrap().parse().unwrap(); } // 0 と 1を両方満たすもの // n = y[0] * i + x[0] // このiを0 ~ y[1] - 1まで試す。(周期がy[1]なので) // let mut n = 0; let mut found = false; for i in 0..y[1] { n = y[0] * i + x[0]; if n % y[1] == x[1] { found = true; break; } } if found { let y01 = lcm(y[0], y[1]); let mut m = 0; found = false; for i in 0..y[2] { m = n + y01 * i; if m % y[2] == x[2] { found = true; break; } } if found && m != 0 { println!("{}", m); return; } } println!("-1"); }