import numpy as np

from collections import defaultdict

import heapq

import sys

sys.setrecursionlimit(10**9)



def mi(): return list(map(int,input().split()))

def ii(): return int(eval(input()))

def isp(): return input().split()

def deb(text): print(("-------\n{}\n-------".format(text)))



INF=1<<18



from numba import njit, b1, i4, i8, f8

@njit((i8,i8,i8), cache=True)

# @njit(cache=True)

def pos(W,h,w):

    return h * W + w



@njit((i8,i8,i8,i8,i8,i8,b1[:]), cache=True)

# @njit(cache=True)

def main(H,W,Ch,Cw,Dh,Dw,G):

    Ch-=1

    Cw-=1

    Dh-=1

    Dw-=1



    s = (Ch,Cw)

    t = (Dh,Dw)



    hq = [(0,s)]

    heapq.heapify(hq)

    

    dist = [INF] *(H*W) # sからxでの最短距離

    dist[pos(W,*s)] = 0

    while hq: 

        d,x = heapq.heappop(hq) # sからxまでの最短経路を探索する

        if dist[pos(W,*x)] < d: continue



        h,w = x

        normal = set([(h-1,w),(h+1,w),(h,w+1),(h,w-1)])

        for dh in range(-2,3):

            for dw in range(-2,3):

                new_h,new_w = h+dh,w+dw

                e = (new_h,new_w)

                cost = 1



                if dh == 0 and dw == 0: continue

                if not (0<=new_h<H and 0<=new_w<W): continue

                if G[pos(W,*e)] == 1:  continue

                if e in normal:

                    cost -= 1

                

                if dist[pos(W,*e)] > dist[pos(W,*x)] + cost:

                    dist[pos(W,*e)] = dist[pos(W,*x)] + cost

                    heapq.heappush(hq,(dist[pos(W,*e)],e))



    # print(dist)

    if dist[pos(W,*t)] == INF: 

        dist[pos(W,*t)] = -1

        

    return dist[pos(W,*t)]





if __name__ == "__main__":

    H,W=mi()

    Ch,Cw=mi()

    Dh,Dw=mi()



    G = [0] * H*W

    for i in range(H):

        g = []

        for j,s in enumerate(list(eval(input()))):

            G[i*W+j] = s == "#"



    ans = main(H,W,Ch,Cw,Dh,Dw,np.array(G))

    print(ans)



from numba import njit

import numpy as np

import heapq

import sys

sys.setrecursionlimit(10**9)



def mi(): return list(map(int,input().split()))

def ii(): return int(eval(input()))

def isp(): return input().split()

def deb(text): print(("-------\n{}\n-------".format(text)))



INF=1<<18



@njit(cache=True)

def pos(W,h,w):

    return h * W + w





# @njit(cache=True)

def solve(H,W,Ch,Cw,Dh,Dw,G):

    Ch-=1

    Cw-=1

    Dh-=1

    Dw-=1



    s = (Ch,Cw)

    t = (Dh,Dw)



    hq = [(0,s)]

    heapq.heapify(hq)

    

    dist = [INF] *(H*W) # sからxでの最短距離

    dist[pos(W,*s)] = 0

    while hq: 

        d,x = heapq.heappop(hq) # sからxまでの最短経路を探索する

        if dist[pos(W,*x)] < d: continue



        h,w = x

        normal = set([(h-1,w),(h+1,w),(h,w+1),(h,w-1)])

        for dh in range(-2,3):

            for dw in range(-2,3):

                new_h,new_w = h+dh,w+dw

                e = (new_h,new_w)

                cost = 1



                if dh == 0 and dw == 0: continue

                if not (0<=new_h<H and 0<=new_w<W): continue

                if G[pos(W,*e)] == 1:  continue

                if e in normal:

                    cost -= 1

                

                if dist[pos(W,*e)] > dist[pos(W,*x)] + cost:

                    dist[pos(W,*e)] = dist[pos(W,*x)] + cost

                    heapq.heappush(hq,(dist[pos(W,*e)],e))



    # print(dist)

    if dist[pos(W,*t)] == INF: 

        dist[pos(W,*t)] = -1

        

    return dist[pos(W,*t)]







def main():

    H,W=mi()

    Ch,Cw=mi()

    Dh,Dw=mi()



    G = [0] * H*W

    for i in range(H):

        g = []

        for j,s in enumerate(list(eval(input()))):

            G[i*W+j] = s == "#"



    ans = solve(H,W,Ch,Cw,Dh,Dw,np.array(G))

    print(ans)



def cc_export():

    from numba.pycc import CC

    cc = CC('my_module')



    cc.export('pos', '(i8,i8,i8,)')(pos)

    cc.export('solve', '(i8,i8,i8,i8,i8,i8,b1[:],)')(solve)

    cc.compile()



if __name__ == '__main__':

    try:

        from my_module import solve

        from my_module import pos

    except:

        cc_export()

        exit()



    main()

import numpy as np
from collections import defaultdict
import heapq
import sys

sys.setrecursionlimit(10**9)


def mi():
    return list(map(int, input().split()))


def ii():
    return int(eval(input()))


def isp():
    return input().split()


def deb(text):
    print(("-------\n{}\n-------".format(text)))


INF = 1 << 18
from numba import njit, b1, i4, i8, f8


@njit((i8, i8, i8), cache=True)
# @njit(cache=True)
def pos(W, h, w):
    return h * W + w


@njit((i8, i8, i8, i8, i8, i8, b1[:]), cache=True)
# @njit(cache=True)
def main(H, W, Ch, Cw, Dh, Dw, G):
    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1
    s = (Ch, Cw)
    t = (Dh, Dw)
    hq = [(0, s)]
    heapq.heapify(hq)
    dist = [INF] * (H * W)  # sからxでの最短距離
    dist[pos(W, *s)] = 0
    while hq:
        d, x = heapq.heappop(hq)  # sからxまでの最短経路を探索する
        if dist[pos(W, *x)] < d:
            continue
        h, w = x
        normal = set([(h - 1, w), (h + 1, w), (h, w + 1), (h, w - 1)])
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                new_h, new_w = h + dh, w + dw
                e = (new_h, new_w)
                cost = 1
                if dh == 0 and dw == 0:
                    continue
                if not (0 <= new_h < H and 0 <= new_w < W):
                    continue
                if G[pos(W, *e)] == 1:
                    continue
                if e in normal:
                    cost -= 1
                if dist[pos(W, *e)] > dist[pos(W, *x)] + cost:
                    dist[pos(W, *e)] = dist[pos(W, *x)] + cost
                    heapq.heappush(hq, (dist[pos(W, *e)], e))
    # print(dist)
    if dist[pos(W, *t)] == INF:
        dist[pos(W, *t)] = -1
    return dist[pos(W, *t)]


if __name__ == "__main__":
    H, W = mi()
    Ch, Cw = mi()
    Dh, Dw = mi()
    G = [0] * H * W
    for i in range(H):
        g = []
        for j, s in enumerate(list(eval(input()))):
            G[i * W + j] = s == "#"
    ans = main(H, W, Ch, Cw, Dh, Dw, np.array(G))
    print(ans)

from numba import njit
import numpy as np
import heapq
import sys

sys.setrecursionlimit(10**9)


def mi():
    return list(map(int, input().split()))


def ii():
    return int(eval(input()))


def isp():
    return input().split()


def deb(text):
    print(("-------\n{}\n-------".format(text)))


INF = 1 << 18


@njit(cache=True)
def pos(W, h, w):
    return h * W + w


# @njit(cache=True)
def solve(H, W, Ch, Cw, Dh, Dw, G):
    Ch -= 1
    Cw -= 1
    Dh -= 1
    Dw -= 1
    s = (Ch, Cw)
    t = (Dh, Dw)
    hq = [(0, s)]
    heapq.heapify(hq)
    dist = [INF] * (H * W)  # sからxでの最短距離
    dist[pos(W, *s)] = 0
    while hq:
        d, x = heapq.heappop(hq)  # sからxまでの最短経路を探索する
        if dist[pos(W, *x)] < d:
            continue
        h, w = x
        normal = set([(h - 1, w), (h + 1, w), (h, w + 1), (h, w - 1)])
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                new_h, new_w = h + dh, w + dw
                e = (new_h, new_w)
                cost = 1
                if dh == 0 and dw == 0:
                    continue
                if not (0 <= new_h < H and 0 <= new_w < W):
                    continue
                if G[pos(W, *e)] == 1:
                    continue
                if e in normal:
                    cost -= 1
                if dist[pos(W, *e)] > dist[pos(W, *x)] + cost:
                    dist[pos(W, *e)] = dist[pos(W, *x)] + cost
                    heapq.heappush(hq, (dist[pos(W, *e)], e))
    # print(dist)
    if dist[pos(W, *t)] == INF:
        dist[pos(W, *t)] = -1
    return dist[pos(W, *t)]


def main():
    H, W = mi()
    Ch, Cw = mi()
    Dh, Dw = mi()
    G = [0] * H * W
    for i in range(H):
        g = []
        for j, s in enumerate(list(eval(input()))):
            G[i * W + j] = s == "#"
    ans = solve(H, W, Ch, Cw, Dh, Dw, np.array(G))
    print(ans)


def cc_export():
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("pos", "(i8,i8,i8,)")(pos)
    cc.export("solve", "(i8,i8,i8,i8,i8,i8,b1[:],)")(solve)
    cc.compile()


if __name__ == "__main__":
    try:
        from my_module import solve
        from my_module import pos
    except:
        cc_export()
        exit()
    main()

+from numba import njit -from collections import defaultdict -from numba import njit, b1, i4, i8, f8 -@njit((i8, i8, i8), cache=True) -# @njit(cache=True) +@njit(cache=True) -@njit((i8, i8, i8, i8, i8, i8, b1[:]), cache=True) -def main(H, W, Ch, Cw, Dh, Dw, G): +def solve(H, W, Ch, Cw, Dh, Dw, G): -if __name__ == "__main__": +def main(): -    ans = main(H, W, Ch, Cw, Dh, Dw, np.array(G)) +    ans = solve(H, W, Ch, Cw, Dh, Dw, np.array(G)) + + +def cc_export(): +    from numba.pycc import CC + +    cc = CC("my_module") +    cc.export("pos", "(i8,i8,i8,)")(pos) +    cc.export("solve", "(i8,i8,i8,i8,i8,i8,b1[:],)")(solve) +    cc.compile() + + +if __name__ == "__main__": +    try: +        from my_module import solve +        from my_module import pos +    except: +        cc_export() +        exit() +    main()