using System; using static System.Console; using System.Linq; using System.Collections.Generic; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.Intrinsics.X86; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (h, w) = (c[0], c[1]); c = NList; var (si, sj) = (c[0] - 1, c[1] - 1); c = NList; var (gi, gj) = (c[0] - 1, c[1] - 1); var s = SList(h); var mx = new int[] { -1 , 1, 0, 0 }; var my = new int[] { 0, 0, -1, 1 }; var INF = int.MaxValue / 2; var len = new int[h][]; for (var i = 0; i < h; ++i) len[i] = Enumerable.Repeat(INF, w).ToArray(); len[si][sj] = 0; var q = new Queue<(int i, int j)>(); q.Enqueue((si, sj)); while (q.Count > 0) { var cur = q.Dequeue(); if (cur.i == gi && cur.j == gj) { WriteLine(len[gi][gj]); return; } for (var p = 0; p < 4; ++p) { var ni = cur.i + mx[p]; var nj = cur.j + my[p]; if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if (len[ni][nj] <= len[cur.i][cur.j] + 1) continue; len[ni][nj] = len[cur.i][cur.j] + 1; q.Enqueue((ni, nj)); } } } }