using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.D
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var (H, W) = Read2Longs();
            var (Ch, Cw) = Read2Longs();
            Ch--; Cw--; // インデックス変換
            var (Dh, Dw) = Read2Longs();
            Dh--; Dw--; // インデックス変換
            var S = new List<string>();
            for (int i = 0; i < H; i++)
            {
                S.Add(ReadText());
            }

            var warpCount = 0;
            var table = GetTable(H, W);
            table[Ch, Cw] = warpCount;
            var queue = new Queue<Point>();
            queue.Enqueue(new Point(Ch, Cw));
            var warpQueue = new Queue<Point>();

            // 到達可能な範囲で埋め尽くす
            while (queue.Count > 0)
            {
                while (queue.Count > 0)
                {
                    var p = queue.Dequeue();

                    var nexts = new Point[]
                    {
                    new Point(p.H + 1, p.W),
                    new Point(p.H, p.W + 1),
                    new Point(p.H - 1, p.W),
                    new Point(p.H, p.W - 1),
                    };

                    foreach (var next in nexts)
                    {
                        if (next.H < 0 || next.H >= H) continue;
                        if (next.W < 0 || next.W >= W) continue;
                        if (S[next.H][next.W] == '#') continue;
                        if (table[next.H, next.W] != -1) continue;

                        // ワープ回数をカウント (ワープ不要で更新する)
                        table[next.H, next.W] = warpCount;

                        queue.Enqueue(next);
                    }

                    var warps = new Point[]
                    {
                        new Point(p.H + 2, p.W - 2),
                        new Point(p.H + 2, p.W - 1),
                        new Point(p.H + 2, p.W),
                        new Point(p.H + 2, p.W + 1),
                        new Point(p.H + 2, p.W + 2),
                        new Point(p.H + 1, p.W - 2),
                        new Point(p.H + 1, p.W - 1),
                        //new Point(p.H + 1, p.W),
                        new Point(p.H + 1, p.W + 1),
                        new Point(p.H + 1, p.W + 2),
                        new Point(p.H, p.W - 2),
                        //new Point(p.H, p.W - 1),
                        //new Point(p.H, p.W),
                        //new Point(p.H, p.W + 1),
                        new Point(p.H, p.W + 2),
                        new Point(p.H - 1, p.W - 2),
                        new Point(p.H - 1, p.W - 1),
                        //new Point(p.H - 1, p.W),
                        new Point(p.H - 1, p.W + 1),
                        new Point(p.H - 1, p.W + 2),
                        new Point(p.H - 2, p.W - 2),
                        new Point(p.H - 2, p.W - 1),
                        new Point(p.H - 2, p.W),
                        new Point(p.H - 2, p.W + 1),
                        new Point(p.H - 2, p.W + 2),
                    };

                    foreach (var warp in warps)
                    {
                        if (warp.H < 0 || warp.H >= H) continue;
                        if (warp.W < 0 || warp.W >= W) continue;
                        if (S[warp.H][warp.W] == '#') continue;
                        if (table[warp.H, warp.W] != -1) continue;

                        warpQueue.Enqueue(warp);
                    }
                }

                warpCount++;
                while (warpQueue.Count > 0)
                {
                    var warp = warpQueue.Dequeue();
                    if (table[warp.H, warp.W] == -1)
                    {
                        table[warp.H, warp.W] = warpCount;
                        queue.Enqueue(warp);
                    }
                }
            }

            var X = new List<string>();
            for (int i = 0; i < H; i++)
            {
                var s = "";
                for (int j = 0; j < W; j++)
                {
                    s += table[i, j];
                }
                X.Add(s);
            }

            return table[Dh, Dw];
        }

        private static int[,] GetTable(long H, long W)
        {
            var t = new int[H, W];
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    t[i, j] = -1;
                }
            }


            return t;
        }

        class Point
        {
            public int H { get; set; }
            public int W { get; set; }

            public Point(long h, long w)
            {
                H = (int)h;
                W = (int)w;
            }
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


Measured memory_bytes: 53712