using System;
using static System.Console;
using System.Collections.Generic;

namespace abc176
{
    class Program
    {
        static int[] ReadNumList_1()
        {
            var line = Console.ReadLine().Split(' ');
            if (line.Length == 1 && string.IsNullOrEmpty(line[0]))
            {
                return new int[0];
            }
            var numList = new int[line.Length];
            for (var i = 0; i < line.Length; ++i)
            {
                numList[i] = int.Parse(line[i]) - 1;
            }
            return numList;
        }
        static void Main()
        {
            var hw = ReadLine().Split(' ');
            var h = int.Parse(hw[0]);
            var w = int.Parse(hw[1]);
            var c = ReadNumList_1();
            var d = ReadNumList_1();
            var s = new char[h][];
            for (var i = 0; i < h; ++i)
            {
                s[i] = ReadLine().ToCharArray();
            }

            var dp = new int[h][];
            for (var i = 0; i < h; ++i)
            {
                dp[i] = new int[w];
                for (var j = 0; j < w; ++j)
                {
                    dp[i][j] = -1;
                }
            }

            WriteLine(Search(h, w, c, d, s, dp));
        }
        static int Search(int h, int w, int[] c, int[] d, char[][] s, int[][] dp)
        {
            var qWarp = new Queue<int[]>();
            var qMove = new Queue<int[]>();
            qMove.Enqueue(c);
            var warps = 0;
            while (true)
            {
                if (qMove.Count == 0)
                {
                    return -1;
                }
                while (qMove.Count > 0)
                {
                    var a = qMove.Dequeue();
                    if (a[0] == d[0] && a[1] == d[1])
                    {
                        return dp[a[0]][a[1]];
                    }
                    qWarp.Enqueue(a);
                    // up
                    if (a[0] > 0 && s[a[0] - 1][a[1]] == '.' && dp[a[0] - 1][a[1]] < warps)
                    {
                        dp[a[0] - 1][a[1]] = warps;
                        qMove.Enqueue(new int[]{ a[0] - 1, a[1] });
                    }
                    // down
                    if (a[0] < h - 1 && s[a[0] + 1][a[1]] == '.')
                    {
                        dp[a[0] + 1][a[1]] = warps;
                        qMove.Enqueue(new int[]{ a[0] + 1, a[1] });
                    }
                    // left
                    if (a[1] > 0 && s[a[0]][a[1] - 1] == '.' && dp[a[0]][a[1] - 1] < warps)
                    {
                        dp[a[0]][a[1] - 1] = warps;
                        qMove.Enqueue(new int[]{ a[0], a[1] - 1 });
                    }
                    // right
                    if (a[1] < w - 1 && s[a[0]][a[1] + 1] == '.' && dp[a[0]][a[1] + 1] < warps)
                    {
                        dp[a[0]][a[1] + 1] = warps;
                        qMove.Enqueue(new int[]{ a[0], a[1] + 1 });
                    }
                }
                ++warps;
                // warps
                while (qWarp.Count > 0)
                {
                    var a = qWarp.Dequeue();
                    var top = Math.Max(0, a[0] - 2);
                    var bottom = Math.Min(h - 1, a[0] + 2);
                    var left = Math.Max(0, a[1] - 2);
                    var right = Math.Min(w - 1, a[1] + 2);
                    for (var i = top; i <= bottom; ++i)
                    {
                        for (var j = left; j <= right; ++j)
                        {
                            if (s[i][j] == '.' && dp[i][j] == -1)
                            {
                                dp[i][j] = warps;
                                qMove.Enqueue(new int[]{ i, j });
                            }
                        }
                    }
                }
            }
            
        }
    }
}


Measured memory_bytes: 47236