using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using static System.Console;
using static System.Math;
using static Atcoder.IO;

namespace Atcoder
{
    class Program
    {
        static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false };
        static void Main(string[] args)
        {
            try
            {
                SetOut(sw);
                Solve.Answer();
                Out.Flush();
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
                Out.Flush();
            }
        }
    }

    public class Solve
    {
        static int[] HW;
        static string[] map;
        static int[][] warp;
        static int warpCnt;

        public static void Answer()
        {
            HW = RSInt;
            var C = RSInt;
            C[0]--;
            C[1]--;
            var D = RSInt;
            D[0]--;
            D[1]--;

            map = new string[HW[0]];
            for (int i = 0; i < HW[0]; i++)
            {
                map[i] = ReadLine();
            }

            warp = new int[HW[0]][];
            for (int i = 0; i < HW[0]; i++)
            {
                warp[i] = new int[HW[1]];
            }
            for (int i = 0; i < HW[0]; i++)
            {
                for (int j = 0; j < HW[1]; j++)
                {
                    warp[i][j] = -1;
                }
            }

            warpCnt = 0;
            warp[C[0]][C[1]] = warpCnt;
            var que = new Queue<int[]>();
            var que2 = new Queue<int[]>();
            que.Enqueue(new int[] { C[0], C[1] });
            que2.Enqueue(new int[] { C[0], C[1] });

            while (Roop(ref que, ref que2))
            { }

            WriteLine(warp[D[0]][D[1]]);
        }

        static bool Roop(ref Queue<int[]> que, ref Queue<int[]> que2)
        {


            while (que.Count != 0)
            {
                int[] now = que.Dequeue();

                if (now[0] - 1 > 0 && map[now[0] - 1][now[1]] == '.')
                {
                    if (warp[now[0] - 1][now[1]] == -1)
                    {
                        warp[now[0] - 1][now[1]] = warpCnt;
                        que.Enqueue(new int[] { now[0] - 1, now[1] });
                        que2.Enqueue(new int[] { now[0] - 1, now[1] });
                    }
                }
                if (now[1] - 1 > 0 && map[now[0]][now[1] - 1] == '.')
                {
                    if (warp[now[0]][now[1] - 1] == -1)
                    {
                        warp[now[0]][now[1] - 1] = warpCnt;
                        que.Enqueue(new int[] { now[0], now[1] - 1 });
                        que2.Enqueue(new int[] { now[0], now[1] - 1 });
                    }
                }
                if (now[0] + 1 < HW[0] && map[now[0] + 1][now[1]] == '.')
                {
                    if (warp[now[0] + 1][now[1]] == -1)
                    {
                        warp[now[0] + 1][now[1]] = warpCnt;
                        que.Enqueue(new int[] { now[0] + 1, now[1] });
                        que2.Enqueue(new int[] { now[0] + 1, now[1] });
                    }
                }
                if (now[1] + 1 < HW[1] && map[now[0]][now[1] + 1] == '.')
                {
                    if (warp[now[0]][now[1] + 1] == -1)
                    {
                        warp[now[0]][now[1] + 1] = warpCnt;
                        que.Enqueue(new int[] { now[0], now[1] + 1 });
                        que2.Enqueue(new int[] { now[0], now[1] + 1 });
                    }
                }
            }
            warpCnt++;
            while (que2.Count != 0)
            {
                int[] now2 = que2.Dequeue();
                for (int i = -2; i <= 2; i++)
                {
                    for (int j = -2; j <= 2; j++)
                    {
                        if (now2[0] + i < 0 || now2[0] + i >= HW[0] || now2[1] + j < 0 || now2[1] + j >= HW[1])
                            continue;
                        if (warp[now2[0] + i][now2[1] + j] >= 0)
                            continue;
                        if (map[now2[0] + i][now2[1] + j] == '.')
                        {
                            warp[now2[0] + i][now2[1] + j] = warpCnt;
                            que.Enqueue(new int[] { now2[0] + i, now2[1] + j });
                        }
                    }
                }
            }

            int cnt = que.Count;
            for (int i = 0; i < cnt; i++)
            {
                var tmp = que.Dequeue();
                que2.Enqueue(tmp);
                que.Enqueue(tmp);
            }

            return que.Count != 0;
        }
    }

    public class IO
    {
        #region Input
        public static int RInt => int.Parse(ReadLine());
        public static long RLong => long.Parse(ReadLine());
        public static double RDouble => double.Parse(ReadLine());

        public static string[] RSString => ReadLine().Split();
        public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray();
        public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray();
        public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray();
        #endregion

        #region Output
        public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list));
        public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No");
        public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO");
        #endregion
    }

    public class Mod
    {
        #region
        private readonly static long M = 1000000007;

        public static long Add(long a, long b) => (a * b) % M;
        public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M;
        public static long Multi(long a, long b) => (a * b) % M;
        #endregion
    }
}

Measured memory_bytes: 47460