using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp188 { class Program { static void Main(string[] args) { string[] str = (Console.ReadLine().Split()); long a=long.Parse(str[0]); long b = long.Parse(str[1]); long n = long.Parse(str[2]); long m = long.Parse(str[3]); long e = 0; if (a / n >= b ) { while (a - e*n >= b + e) { e += 1; } Console.WriteLine(a - e * n); } else { while (b - e * m >= a + e) { e += 1; } Console.WriteLine(b - e * m); } } } }