using System.Linq;
using System.Text;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Threading;
using System.Runtime.InteropServices.ComTypes;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace AtCoder
{
    class Program
    {

        struct Point
        {
            public int x;
            public int y;

            public Point(int x, int y)
            {

                this.x = x;
                this.y = y;

            }

        }


        const int BiggestMod = 1000000007;

        public static void Main()
        {

            var args = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var h = args[0];
            var w = args[1];

            int[][] data =
            {
                new int[]{ 1, 0 },
                new int[]{ -1, 0 },
                new int[]{ 0, 1 },
                new int[]{ 0, -1 }
            };

            args = Console.ReadLine().Split(' ').Select(x => int.Parse(x) - 1).ToArray();
            var st = new Point(args[1], args[0]);

            args = Console.ReadLine().Split(' ').Select(x => int.Parse(x) - 1).ToArray();
            var gl = new Point(args[1], args[0]);

            var map = new BitArray[h];
            var explored = new BitArray[h];

            for (int i = 0; i < h; i++)
            {

                map[i] = new BitArray(Console.ReadLine().Select(x => x != '#').ToArray());
                explored[i] = new BitArray(w, false);

            }

            var searching = new List<Point> { st };
            var nextsearch = new List<Point>();
            var ans = 0;

            explored[st.y][st.x] = true;

            for (; true; ans++)
            {

                if (searching.Count == 0)
                {

                    ans = -1;
                    Answer();
                    return;

                }    

                for (int i = 0; i < searching.Count; i++)
                    if (searching[i].x == gl.x && searching[i].y == gl.y)
                    {

                        Answer();
                        return;

                    }
                    else
                    {

                        foreach (var walker in data)
                        {

                            var bufy = Math.Max(0, Math.Min(h - 1, searching[i].y + walker[1]));
                            var bufx = Math.Max(0, Math.Min(w - 1, searching[i].x + walker[0]));

                            if (map[bufy][bufx] && (!explored[bufy][bufx]))
                            {
                                explored[bufy][bufx] = true;
                                searching.Add(new Point(bufx, bufy));
                            }
                        }


                    }

                nextsearch = searching;
                searching = new List<Point>();

                var listing = new List<Point>();
                foreach (var nextsearching in nextsearch)
                    for(int i = Math.Max(0, nextsearching.x - 2), l = Math.Min(w, nextsearching.x + 3); i < l; i++)
                        for (int j = Math.Max(0, nextsearching.y - 2), k = Math.Min(h, nextsearching.y + 3); j < k; j++)
                            if (map[j][i] && (!explored[j][i]))
                                if (j == gl.y && i == gl.x)
                                {

                                    ans++;
                                    Answer();
                                    return;

                                }
                                else
                                {

                                    searching.Add(new Point(i, j));
                                    explored[j][i] = true;

                                }

            }

            void Answer()
                => Console.WriteLine(ans);
            
        }

    }

}

Measured memory_bytes: 46884