use std::io::*; //use std::num; use std::str::FromStr; //use std::time::Instant; fn main() { //let now = Instant::now(); let a: isize = read(); let b: isize = read(); let c: isize = read(); let d: isize = read(); let mut flag = 0; let mut x = -100; loop { if x == 100 { break; } let mut y = -100; loop { if y == 100 { break; } if b + a * x == d + c * y && b + a * x >= 0 { flag = 1; println!("{}", b + a * x); break; } y += 1; } if flag == 1 { break; } x += 1; } if flag == 0 { println!("NaN"); } //println!("[DEBUG_PRINT] {} ms", now.elapsed().as_millis()); } pub fn min(x: usize, y: usize) -> usize { let z: usize; if x <= y { z = x; } else { z = y; } return z; } pub fn read() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } pub fn read_array() -> Vec { let array: String = read(); let chars: Vec = array.trim().chars().collect::>(); chars } pub fn get_data() -> Vec { let mut s: String = String::new(); std::io::stdin().read_line(&mut s).ok(); let vec: Vec = s .trim() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect(); vec } pub fn get_chars() -> Vec { let mut s: String = String::new(); std::io::stdin().read_line(&mut s).ok(); let vec: Vec = s.trim().chars().collect::>(); vec }