from _collections import deque

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

cx,cy=list(map(int,input().split()))

dx,dy=list(map(int,input().split()))

s=[[] for _ in range(h+2)]

for i in range(h):

    x=list(eval(input()))

    x.insert(0,"#")

    x.append("#")

    s[i+1]=x

s[0]=s[-1]=["#"]*(w+2)





dep=[[10**10]*(w+2) for _ in range(h+2)]

dep[cx][cy]=0

data=deque([[cx,cy]])

data2=deque([])

while len(data)>0:

    while len(data)>0:

        xy=data.popleft()

        x,y=xy[0],xy[1]

        data2.append([x,y])

        if s[x-1][y]=="." and dep[x-1][y]>dep[x][y]:

            dep[x-1][y]=dep[x][y]

            data.append([x-1,y])



        if s[x+1][y] == "." and dep[x+1][y] > dep[x][y]:

            dep[x+1][y] = dep[x][y]

            data.append([x + 1,y])



        if s[x][y-1] == "." and dep[x][y-1] > dep[x][y]:

            dep[x][y-1] = dep[x][y]

            data.append([x,y-1])



        if s[x][y+1] == "." and dep[x][y+1] > dep[x][y]:

            dep[x][y+1] = dep[x][y]

            data.append([x,y+1])





    while len(data2)>0:

        xy=data2.popleft()

        x,y=xy[0],xy[1]

        for i in range(5):

            for j in range(5):

                if 0<=x-2+i<=h+1 and 0<=y-2+j<=w+1 and s[x-2+i][y-2+j]=="." and dep[x-2+i][y-2+j]>dep[x][y]+1:

                    dep[x-2+i][y-2+j]=dep[x][y]+1

                    data.append([x-2+i,y-2+j])



print((dep[dx][dy] if dep[dx][dy]<10**10 else -1))



from _collections import deque





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

cx,cy=list(map(int,input().split()))

dx,dy=list(map(int,input().split()))

s=[[] for _ in range(h+2)]

for i in range(h):

    x=list(eval(input()))

    x.insert(0,"#")

    x.append("#")

    s[i+1]=x

s[0]=s[-1]=["#"]*(w+2)









dep=[[10**10]*(w+2) for _ in range(h+2)]

dep[cx][cy]=0

data=deque([[cx,cy,0]])

while len(data)>0:

  xy=data.popleft()

  x,y=xy[0],xy[1]

  if xy[2]==dep[x][y]:

    if s[x-1][y]=="." and dep[x-1][y]>dep[x][y]:

        dep[x-1][y]=dep[x][y]

        data.appendleft([x-1,y,dep[x][y]])

    if s[x+1][y] == "." and dep[x+1][y] > dep[x][y]:

        dep[x+1][y] = dep[x][y]

        data.appendleft([x + 1,y,dep[x][y]])

    if s[x][y-1] == "." and dep[x][y-1] > dep[x][y]:

        dep[x][y-1] = dep[x][y]

        data.appendleft([x,y-1,dep[x][y]])

    if s[x][y+1] == "." and dep[x][y+1] > dep[x][y]:

        dep[x][y+1] = dep[x][y]

        data.appendleft([x,y+1,dep[x][y]])



    for i in range(5):

        for j in range(5):

            if 0<=x-2+i<=h+1 and 0<=y-2+j<=w+1 and s[x-2+i][y-2+j]=="." and dep[x-2+i][y-2+j]>dep[x][y]+1:

                dep[x-2+i][y-2+j]=dep[x][y]+1

                data.append([x-2+i,y-2+j,dep[x][y]+1])



print((dep[dx][dy] if dep[dx][dy]<10**10 else -1))









from _collections import deque

h, w = list(map(int, input().split()))
cx, cy = list(map(int, input().split()))
dx, dy = list(map(int, input().split()))
s = [[] for _ in range(h + 2)]
for i in range(h):
    x = list(eval(input()))
    x.insert(0, "#")
    x.append("#")
    s[i + 1] = x
s[0] = s[-1] = ["#"] * (w + 2)
dep = [[10**10] * (w + 2) for _ in range(h + 2)]
dep[cx][cy] = 0
data = deque([[cx, cy]])
data2 = deque([])
while len(data) > 0:
    while len(data) > 0:
        xy = data.popleft()
        x, y = xy[0], xy[1]
        data2.append([x, y])
        if s[x - 1][y] == "." and dep[x - 1][y] > dep[x][y]:
            dep[x - 1][y] = dep[x][y]
            data.append([x - 1, y])
        if s[x + 1][y] == "." and dep[x + 1][y] > dep[x][y]:
            dep[x + 1][y] = dep[x][y]
            data.append([x + 1, y])
        if s[x][y - 1] == "." and dep[x][y - 1] > dep[x][y]:
            dep[x][y - 1] = dep[x][y]
            data.append([x, y - 1])
        if s[x][y + 1] == "." and dep[x][y + 1] > dep[x][y]:
            dep[x][y + 1] = dep[x][y]
            data.append([x, y + 1])
    while len(data2) > 0:
        xy = data2.popleft()
        x, y = xy[0], xy[1]
        for i in range(5):
            for j in range(5):
                if (
                    0 <= x - 2 + i <= h + 1
                    and 0 <= y - 2 + j <= w + 1
                    and s[x - 2 + i][y - 2 + j] == "."
                    and dep[x - 2 + i][y - 2 + j] > dep[x][y] + 1
                ):
                    dep[x - 2 + i][y - 2 + j] = dep[x][y] + 1
                    data.append([x - 2 + i, y - 2 + j])
print((dep[dx][dy] if dep[dx][dy] < 10**10 else -1))

from _collections import deque

h, w = list(map(int, input().split()))
cx, cy = list(map(int, input().split()))
dx, dy = list(map(int, input().split()))
s = [[] for _ in range(h + 2)]
for i in range(h):
    x = list(eval(input()))
    x.insert(0, "#")
    x.append("#")
    s[i + 1] = x
s[0] = s[-1] = ["#"] * (w + 2)
dep = [[10**10] * (w + 2) for _ in range(h + 2)]
dep[cx][cy] = 0
data = deque([[cx, cy, 0]])
while len(data) > 0:
    xy = data.popleft()
    x, y = xy[0], xy[1]
    if xy[2] == dep[x][y]:
        if s[x - 1][y] == "." and dep[x - 1][y] > dep[x][y]:
            dep[x - 1][y] = dep[x][y]
            data.appendleft([x - 1, y, dep[x][y]])
        if s[x + 1][y] == "." and dep[x + 1][y] > dep[x][y]:
            dep[x + 1][y] = dep[x][y]
            data.appendleft([x + 1, y, dep[x][y]])
        if s[x][y - 1] == "." and dep[x][y - 1] > dep[x][y]:
            dep[x][y - 1] = dep[x][y]
            data.appendleft([x, y - 1, dep[x][y]])
        if s[x][y + 1] == "." and dep[x][y + 1] > dep[x][y]:
            dep[x][y + 1] = dep[x][y]
            data.appendleft([x, y + 1, dep[x][y]])
        for i in range(5):
            for j in range(5):
                if (
                    0 <= x - 2 + i <= h + 1
                    and 0 <= y - 2 + j <= w + 1
                    and s[x - 2 + i][y - 2 + j] == "."
                    and dep[x - 2 + i][y - 2 + j] > dep[x][y] + 1
                ):
                    dep[x - 2 + i][y - 2 + j] = dep[x][y] + 1
                    data.append([x - 2 + i, y - 2 + j, dep[x][y] + 1])
print((dep[dx][dy] if dep[dx][dy] < 10**10 else -1))

-data = deque([[cx, cy]]) -data2 = deque([]) +data = deque([[cx, cy, 0]]) -    while len(data) > 0: -        xy = data.popleft() -        x, y = xy[0], xy[1] -        data2.append([x, y]) +    xy = data.popleft() +    x, y = xy[0], xy[1] +    if xy[2] == dep[x][y]: -            data.append([x - 1, y]) +            data.appendleft([x - 1, y, dep[x][y]]) -            data.append([x + 1, y]) +            data.appendleft([x + 1, y, dep[x][y]]) -            data.append([x, y - 1]) +            data.appendleft([x, y - 1, dep[x][y]]) -            data.append([x, y + 1]) -    while len(data2) > 0: -        xy = data2.popleft() -        x, y = xy[0], xy[1] +            data.appendleft([x, y + 1, dep[x][y]]) -                    data.append([x - 2 + i, y - 2 + j]) +                    data.append([x - 2 + i, y - 2 + j, dep[x][y] + 1])