// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
    long H, W;
    scan(H, W);
    long CH, CW;
    scan(CH, CW);
    CH--, CW--;
    long DH, DW;
    scan(DH, DW);
    DH--, DW--;
    auto S = new string[](H);
    foreach (i; 0 .. H)
    {
        S[i] = sread();
    }
    alias T = Tuple!(long, long, long);
    PQueue!T Q;
    Q.insert(T(0, CH, CW));
    auto visited = new bool[][](H, W);
    long cnt;
    while (!Q.empty)
    {
        cnt++;
        auto t = Q.front;
        auto d = t[0];
        auto y = t[1];
        auto x = t[2];
        Q.removeFront();
        if (visited[y][x])
            continue;
        visited[y][x] = true;
        dprint(t);
        if (S[y][x] == '#')
            continue;
        if (y == DH && x == DW)
        {
            writeln(d);
            return;
        }
        foreach (a; -2 .. 2 + 1)
            foreach (b; -2 .. 2 + 1)
            {
                if (a == 0 && b == 0)
                    continue;
                if (y + a < 0 || H <= y + a)
                    continue;
                if (x + b < 0 || W <= x + b)
                    continue;
                if (visited[y + a][x + b])
                    continue;
                if (S[y + a][x + b] == '#')
                    continue;
                if (abs(a) <= 1 && b == 0)
                {
                    Q.insert(T(d, y + a, x + b));
                    continue;
                }
                if (a == 0 && abs(b) <= 1)
                {
                    Q.insert(T(d, y + a, x + b));
                    continue;
                }
                Q.insert(T(d + 1, y + a, x + b));
            }
    }
    writeln(-1);
    dprint(cnt);
}
