using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace AC
{
    class Program
    {
        static void Main(string[] args)
        {
            const int max = int.MaxValue;
            var input = ReadArrayInt();
            var H = input[0];
            var W = input[1];
            input = ReadArrayInt();
            var cY = input[0] - 1;
            var cX = input[1] - 1;
            input = ReadArrayInt();
            var dY = input[0] - 1;
            var dX = input[1] - 1;
            var S = new char[H, W];
            for (int i = 0; i < H; i++)
            {
                var line = Read();
                for (int j = 0; j < W; j++)
                {
                    S[i, j] = line[j];
                }
            }
            var times = new int[H, W];
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    times[i, j] = max;
                }
            }
            var que = new Queue<(int y, int x)>();
            que.Enqueue((cY, cX));
            times[cY, cX] = 0;
            var walkY = new int[] { -1, 1, 0, 0 };
            var walkX = new int[] { 0, 0, -1, 1 };
            while (que.Any())
            {
                var now = que.Dequeue();
                var Y = now.y;
                var X = now.x;
                var cnt = times[Y, X];
                var walkQue = new Queue<(int y, int x)>();
                walkQue.Enqueue((Y, X));
                while (walkQue.Any())
                {
                    var walNow = walkQue.Dequeue();
                    var walY = walNow.y;
                    var walX = walNow.x;
                    for (int i = 0; i < 4; i++)
                    {
                        var nextY = walY + walkY[i];
                        var nextX = walX + walkX[i];
                        if (nextY < 0 || nextY >= H || nextX < 0 || nextX >= W || S[nextY, nextX] == '#') continue;
                        if (times[nextY, nextX] > cnt)
                        {
                            times[nextY, nextX] = cnt;
                            que.Enqueue((nextY, nextX));
                            walkQue.Enqueue((nextY, nextX));
                        }
                    }
                }
                var up = Math.Max(0, Y - 2);
                var down = Math.Min(H - 1, Y + 2);
                var left = Math.Max(0, X - 2);
                var right = Math.Min(W - 1, X + 2);
                cnt++;
                for (int i = up; i <= down; i++)
                {
                    for (int j = left; j <= right; j++)
                    {
                        if (S[i, j] == '#') continue;
                        if (times[i, j] > cnt)
                        {
                            times[i, j] = cnt;
                            que.Enqueue((i, j));
                        }
                    }
                }
            }

            Console.WriteLine(times[dY, dX] != max ? times[dY, dX] : -1);
            Console.ReadLine();
        }

        static string Read() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Read()); }
        static long ReadLong() { return long.Parse(Read()); }
        static string[] ReadArrayString() { return Read().Split(' '); }
        static int[] ReadArrayInt() { return Read().Split(' ').Select(s => int.Parse(s)).ToArray(); }
        static long[] ReadArrayLong() { return Read().Split(' ').Select(s => long.Parse(s)).ToArray(); }
    }
}

Measured memory_bytes: 46980