using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.IO;
using System.Text;
using N = System.Int32;

static class Program
{
    static public void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var solver = new Solver();
        solver.Solve();
        Console.Out.Flush();
    }
}

public class Solver
{
    N h, w, ch, cw, dh, dw;
    bool[,] moveflag;
    bool[,] warpflag;
    List<N[]> stack = new List<N[]>();
    public void Solve()
    {
        h = rn;
        w = rn;
        ch = rn - 1;
        cw = rn - 1;
        dh = rn - 1;
        dw = rn - 1;//index用に1減算
        string s = "";
        moveflag = new bool[h, w];
        warpflag = new bool[h, w];
        for (int x = 0; x < h; x++) {
            s = rs;
            for (int y = 0; y < w; y++)
            {
                moveflag[x, y] = (s[y] == '#');
                warpflag[x, y] = (s[y] == '#');
            }
        }
        N count = 0;
        stack.Add(new N[] { ch, cw });
        var stackcopy = new List<N[]>();
        while (stack.Count>0)
        {
            stackcopy = new List<N[]>(stack);
            stack = new List<N[]>();
            while (stackcopy.Count > 0)
            {
                HelperMove(stackcopy[0][0], stackcopy[0][1]);
                HelperWarp(stackcopy[0][0], stackcopy[0][1]);
                stackcopy.RemoveAt(0);
            }
            if (moveflag[dh, dw])
            {
                Write(count);
                return;
            }
            count++;
        }
        Write(-1);
    }

    public void HelperMove(N x, N y) //塗り
    {
        moveflag[x, y] = true;
        if (x > 0 && !moveflag[x - 1,y]) { HelperMove(x - 1, y); }
        if (x < h - 1 && !moveflag[x + 1, y]) { HelperMove(x + 1, y); }
        if (y > 0 && !moveflag[x, y - 1]) { HelperMove(x, y - 1); }
        if (y < w - 1 && !moveflag[x, y + 1]) { HelperMove(x, y + 1); }
    }

    public void HelperWarp(N x, N y) //ワープスタック
    {
        if (warpflag[x,y]) return;
        warpflag[x, y] = true;
        for(int xx = -2; xx <= 2; xx++)
        {
            for (int yy = -2; yy <= 2; yy++)
            {
                if (x + xx >= 0 && x + xx < h && y + yy >= 0 && y + yy < w && !moveflag[x + xx, y + yy])
                {
                    stack.Add(new N[] { x + xx, y + yy });
                }
            }
        }
        if (x > 0 && !warpflag[x - 1, y]) { HelperWarp(x - 1, y); }
        if (x < h - 1 && !warpflag[x + 1, y]) { HelperWarp(x + 1, y); }
        if (y > 0 && !warpflag[x, y - 1]) { HelperWarp(x, y - 1); }
        if (y < w - 1 && !warpflag[x, y + 1]) { HelperWarp(x, y + 1); }
    }


    N rn { get { return sc.Integer(); } }
    double rd { get { return sc.Double(); } }
    string rs { get { return sc.Scan(); } }
    public StreamScanner sc = new StreamScanner(Console.OpenStandardInput());

    void Write<T>(T t) { Console.WriteLine(t.ToString()); }
    void YN(bool t) { Console.WriteLine(t ? "YES" : "NO"); }
    void Yn(bool t) { Console.WriteLine(t ? "Yes" : "No"); }
    void yn(bool t) { Console.WriteLine(t ? "yes" : "no"); }
}



public class StreamScanner
{
    public StreamScanner(Stream stream) { str = stream; }
    private readonly Stream str;
    private readonly byte[] buf = new byte[1024];
    private int len, ptr;
    public bool isEof = false;
    public bool IsEndOfStream { get { return isEof; } }
    private byte read()
    {
        if (isEof) throw new EndOfStreamException();
        if (ptr >= len)
        {
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) <= 0)
            {
                isEof = true;
                return 0;
            }
        }
        return buf[ptr++];
    }
    public char Char()
    {
        byte b = 0;
        do b = read();
        while (b < 33 || 126 < b);
        return (char)b;
    }
    public string Scan()
    {
        var sb = new StringBuilder();
        for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
            sb.Append(b);
        return sb.ToString();
    }
    public N Integer()
    {
        N ret = 0; byte b = 0; var ng = false;
        do b = read();
        while (b != '-' && (b < '0' || '9' < b));
        if (b == '-') { ng = true; b = read(); }
        for (; true; b = read())
        {
            if (b < '0' || '9' < b)
                return ng ? -ret : ret;
            else ret = ret * 10 + b - '0';
        }
    }
    public double Double() { return double.Parse(Scan()); }
}


Measured memory_bytes: 43432