using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication4 { class Program { static void Main() { int[] a = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int[] b = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int x = a[0]; int y = a[1]; int x1 = b[0]; int y1 = b[1]; long[,] f = new long[y, x]; for(int i = 0; i < y; i++) { for(int j = 0; j < x; j++) { if (i == 0) { f[i, j] = j+1; } else if (j == 0) { f[i, j] = i+1; } else if (i == y1 && j == x1) { f[i, j] = long.MaxValue; } else { f[i, j] = Math.Min(f[i - 1, j], f[i, j - 1]) + 1; } } } Console.WriteLine(f[y - 1, x - 1]); } } }