from collections import deque

def main():

    h,w=list(map(int,input().split()))

    sx,sy=list(map(int,input().split()))

    sx-=1

    sy-=1

    gx,gy=list(map(int,input().split()))

    gx-=1

    gy-=1

    g=[tuple(eval(input())) for _ in range(h)]

    v=[[0 for j in range(w)] for i in range(h)]

    q=deque([[sx,sy]])

    v[sx][sy]=1

    ans=0

    k=[[0,1],[0,-1],[1,0],[-1,0]]

    while True:

        l=[]

        while q:

            x,y=q.popleft()

            l.append([x,y])

            if x==gx and y==gy:

                print(ans)

                exit()

            for a,b in k:

                nx=x+a

                ny=y+b

                if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:

                    q.append([nx,ny])

                    v[nx][ny]=1

        if len(l)==0:

            break

        for x,y in l:              

            for a in range(-2,3):

                for b in range(-2,3):

                    if a*b==0 and abs(a+b)<=1:

                        continue

                    nx=x+a

                    ny=y+b

                    if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:

                        q.append([nx,ny])

                        v[nx][ny]=1

        ans+=1 

    print((-1))

if __name__=='__main__':

    main()
from heapq import heappush,heappop

from collections import deque

h,w=list(map(int,input().split()))

sx,sy=list(map(int,input().split()))

sx-=1

sy-=1

gx,gy=list(map(int,input().split()))

gx-=1

gy-=1

g=[list(eval(input())) for _ in range(h)]

v=[[0 for j in range(w)] for i in range(h)]

v2=[[0 for j in range(w)] for i in range(h)]

q=deque([])

q.append([0,sx,sy])

while q:

    c,x,y=q.popleft()

    if x==gx and y==gy:

        print(c)

        exit()

    if v[x][y]==1:

        continue

    v[x][y]=1

    for a in range(-2,3):

        for b in range(-2,3):

            if a==b==0:

                continue

            if a*b==0 and abs(a+b)==1:

                nx=x+a

                ny=y+b

                if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0:

                    q.appendleft([c,nx,ny])

                continue

            nx=x+a

            ny=y+b

            if 0<=nx<h and 0<=ny<w and g[nx][ny]=='.' and v[nx][ny]==0 and v2[nx][ny]==0:

                q.append([c+1,nx,ny])

                v2[nx][ny]=1

print((-1))

from collections import deque


def main():
    h, w = list(map(int, input().split()))
    sx, sy = list(map(int, input().split()))
    sx -= 1
    sy -= 1
    gx, gy = list(map(int, input().split()))
    gx -= 1
    gy -= 1
    g = [tuple(eval(input())) for _ in range(h)]
    v = [[0 for j in range(w)] for i in range(h)]
    q = deque([[sx, sy]])
    v[sx][sy] = 1
    ans = 0
    k = [[0, 1], [0, -1], [1, 0], [-1, 0]]
    while True:
        l = []
        while q:
            x, y = q.popleft()
            l.append([x, y])
            if x == gx and y == gy:
                print(ans)
                exit()
            for a, b in k:
                nx = x + a
                ny = y + b
                if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == "." and v[nx][ny] == 0:
                    q.append([nx, ny])
                    v[nx][ny] = 1
        if len(l) == 0:
            break
        for x, y in l:
            for a in range(-2, 3):
                for b in range(-2, 3):
                    if a * b == 0 and abs(a + b) <= 1:
                        continue
                    nx = x + a
                    ny = y + b
                    if (
                        0 <= nx < h
                        and 0 <= ny < w
                        and g[nx][ny] == "."
                        and v[nx][ny] == 0
                    ):
                        q.append([nx, ny])
                        v[nx][ny] = 1
        ans += 1
    print((-1))


if __name__ == "__main__":
    main()

from heapq import heappush, heappop
from collections import deque

h, w = list(map(int, input().split()))
sx, sy = list(map(int, input().split()))
sx -= 1
sy -= 1
gx, gy = list(map(int, input().split()))
gx -= 1
gy -= 1
g = [list(eval(input())) for _ in range(h)]
v = [[0 for j in range(w)] for i in range(h)]
v2 = [[0 for j in range(w)] for i in range(h)]
q = deque([])
q.append([0, sx, sy])
while q:
    c, x, y = q.popleft()
    if x == gx and y == gy:
        print(c)
        exit()
    if v[x][y] == 1:
        continue
    v[x][y] = 1
    for a in range(-2, 3):
        for b in range(-2, 3):
            if a == b == 0:
                continue
            if a * b == 0 and abs(a + b) == 1:
                nx = x + a
                ny = y + b
                if 0 <= nx < h and 0 <= ny < w and g[nx][ny] == "." and v[nx][ny] == 0:
                    q.appendleft([c, nx, ny])
                continue
            nx = x + a
            ny = y + b
            if (
                0 <= nx < h
                and 0 <= ny < w
                and g[nx][ny] == "."
                and v[nx][ny] == 0
                and v2[nx][ny] == 0
            ):
                q.append([c + 1, nx, ny])
                v2[nx][ny] = 1
print((-1))

+from heapq import heappush, heappop - -def main(): -    h, w = list(map(int, input().split())) -    sx, sy = list(map(int, input().split())) -    sx -= 1 -    sy -= 1 -    gx, gy = list(map(int, input().split())) -    gx -= 1 -    gy -= 1 -    g = [tuple(eval(input())) for _ in range(h)] -    v = [[0 for j in range(w)] for i in range(h)] -    q = deque([[sx, sy]]) -    v[sx][sy] = 1 -    ans = 0 -    k = [[0, 1], [0, -1], [1, 0], [-1, 0]] -    while True: -        l = [] -        while q: -            x, y = q.popleft() -            l.append([x, y]) -            if x == gx and y == gy: -                print(ans) -                exit() -            for a, b in k: +h, w = list(map(int, input().split())) +sx, sy = list(map(int, input().split())) +sx -= 1 +sy -= 1 +gx, gy = list(map(int, input().split())) +gx -= 1 +gy -= 1 +g = [list(eval(input())) for _ in range(h)] +v = [[0 for j in range(w)] for i in range(h)] +v2 = [[0 for j in range(w)] for i in range(h)] +q = deque([]) +q.append([0, sx, sy]) +while q: +    c, x, y = q.popleft() +    if x == gx and y == gy: +        print(c) +        exit() +    if v[x][y] == 1: +        continue +    v[x][y] = 1 +    for a in range(-2, 3): +        for b in range(-2, 3): +            if a == b == 0: +                continue +            if a * b == 0 and abs(a + b) == 1: -                    q.append([nx, ny]) -                    v[nx][ny] = 1 -        if len(l) == 0: -            break -        for x, y in l: -            for a in range(-2, 3): -                for b in range(-2, 3): -                    if a * b == 0 and abs(a + b) <= 1: -                        continue -                    nx = x + a -                    ny = y + b -                    if ( -                        0 <= nx < h -                        and 0 <= ny < w -                        and g[nx][ny] == "." -                        and v[nx][ny] == 0 -                    ): -                        q.append([nx, ny]) -                        v[nx][ny] = 1 -        ans += 1 -    print((-1)) - - -if __name__ == "__main__": -    main() +                    q.appendleft([c, nx, ny]) +                continue +            nx = x + a +            ny = y + b +            if ( +                0 <= nx < h +                and 0 <= ny < w +                and g[nx][ny] == "." +                and v[nx][ny] == 0 +                and v2[nx][ny] == 0 +            ): +                q.append([c + 1, nx, ny]) +                v2[nx][ny] = 1 +print((-1))