using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.ABC_176
{
    class Dcs
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int[] hw = ReadInts();
            int h = hw[0];
            int w = hw[1];
            int[] cs = ReadInts();
            cs[0]--;
            cs[1]--;
            int[] ds = ReadInts();
            ds[0]--;
            ds[1]--;

            bool[,] grid = new bool[h, w];
            int[,] nos = new int[h,w];
            for (int i = 0; i < h; i++)
            {
                string s = Read();
                for(int j = 0; j < w; j++)
                {
                    grid[i, j] = s[j] == '.';
                    nos[i, j] = -1;
                }
            }

            List<HashSet<int>> graph = new List<HashSet<int>>();
            int startNo = 0;
            int goalNo = 0;
            for(int i = 0; i < h; i++)
            {
                for(int j = 0; j < w; j++)
                {
                    if(!grid[i,j]|| nos[i, j] >= 0)
                    {
                        continue;
                    }

                    int no = graph.Count;
                    graph.Add(new HashSet<int>());
                    Queue<int[]> queTmp = new Queue<int[]>();
                    queTmp.Enqueue(new int[2] { i, j });
                    while (queTmp.Count > 0)
                    {
                        int[] pos = queTmp.Dequeue();
                        int y = pos[0];
                        int x = pos[1];

                        if (nos[y, x] >= 0)
                        {
                            continue;
                        }

                        nos[y, x] = no;
                        if (y == cs[0] && x == cs[1])
                        {
                            startNo = no;
                        }
                        if (y == ds[0] && x == ds[1])
                        {
                            goalNo = no;
                        }

                        for (int ii = Max(0, y - 2); ii <= Min(h - 1, y + 2); ii++)
                        {
                            for (int jj = Max(0, x - 2); jj <= Min(w - 1, x + 2); jj++)
                            {
                                if (!grid[ii, jj])
                                {
                                    continue;
                                }

                                int delta = Abs(ii - y) + Abs(jj - x);
                                if (delta > 1&&nos[ii,jj]>=0)
                                {
                                    graph[no].Add(nos[ii, jj]);
                                    graph[nos[ii, jj]].Add(no);
                                }
                                if (delta == 1 && nos[ii, jj] < 0)
                                {
                                    queTmp.Enqueue(new int[2] { ii, jj });
                                }
                            }
                        }
                    }
                }
            }

            int[] dists = new int[graph.Count];
            for(int i = 0; i < graph.Count; i++)
            {
                dists[i] = int.MaxValue / 2;
            }
            Queue<int> que = new Queue<int>();
            que.Enqueue(startNo);
            dists[startNo] = 0;
            while (que.Count > 0)
            {
                int now = que.Dequeue();
                foreach(int val in graph[now])
                {
                    if (dists[val] <= dists[now] + 1)
                    {
                        continue;
                    }

                    dists[val] = dists[now] + 1;
                    que.Enqueue(val);
                }
            }

            WriteLine(dists[goalNo] == int.MaxValue / 2 ? -1 : dists[goalNo]);
        }

        class PriorityQueue<T>
        {
            private readonly List<KeyValuePair<long, T>> list;

            public int Count { get; private set; }

            public PriorityQueue()
            {
                list = new List<KeyValuePair<long, T>>();
                Count = 0;
            }

            private void Add(KeyValuePair<long, T> pair)
            {
                if (Count == list.Count)
                {
                    list.Add(pair);
                }
                else
                {
                    list[Count] = pair;
                }
                Count++;
            }

            private void Swap(int a, int b)
            {
                KeyValuePair<long, T> tmp = list[a];
                list[a] = list[b];
                list[b] = tmp;
            }

            public void Enqueue(long key, T value)
            {
                Add(new KeyValuePair<long, T>(key, value));
                int c = Count - 1;
                while (c > 0)
                {
                    int p = (c - 1) / 2;
                    if (list[c].Key >= list[p].Key) break;

                    Swap(p, c);
                    c = p;
                }
            }

            public KeyValuePair<long, T> Top()
            {
                return list[0];
            }

            public KeyValuePair<long, T> Dequeue()
            {
                KeyValuePair<long, T> pair = list[0];
                Count--;
                if (Count == 0) return pair;

                list[0] = list[Count];
                int p = 0;
                while (true)
                {
                    int c1 = p * 2 + 1;
                    int c2 = p * 2 + 2;
                    if (c1 >= Count) break;

                    int c = (c2 >= Count || list[c1].Key < list[c2].Key)
                        ? c1 : c2;
                    if (list[c].Key >= list[p].Key) break;

                    Swap(p, c);
                    p = c;
                }
                return pair;
            }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 85280