using static System.Math; using System.Collections.Generic; using System.Linq; using System; public class Hello { static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var b0 = int.Parse(line[0]); var c0 = int.Parse(line[1]); line = Console.ReadLine().Trim().Split(' '); var b1 = int.Parse(line[0]); var c1 = int.Parse(line[1]); getAns(b0, c0, b1, c1); } static void getAns(int b0, int c0, int b1, int c1) { while (c0 < 0) c0 += b0; while (c1 < 0) c1 += b1; while (c0 >= b0) c0 -= b0; while (c1 >= b1) c1 -= b1; for (int i = 0; i < b0 * b1; i++) { if (i % b0 == c0 && i % b1 == c1) { Console.WriteLine(i); return; } } Console.WriteLine("NaN"); } }