#abc176-d

import sys,math,collections,itertools

input = sys.stdin.readline

 

H,W=list(map(int,input().split()))

Ch,Cw=list(map(int,input().split()))

Dh,Dw=list(map(int,input().split()))

s=[ input().rstrip() for _ in range(H)]

 

#DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする

dp = [ [float('inf') ]*W  for _ in range(H)]

 

q = collections.deque([])

q.append((Ch-1,Cw-1))

dp[Ch-1][Cw-1]=0

while q:

    visit = []

    while q:

        nh,nw = q.popleft()

        visit.append((nh,nw))

        #徒歩での遷移

        if nh>0 and s[nh-1][nw] =='.' and dp[nh-1][nw]>dp[nh][nw]:

            q.append((nh-1,nw))

            dp[nh-1][nw] = dp[nh][nw]

        if nh<H-1 and s[nh+1][nw] =='.' and dp[nh+1][nw]>dp[nh][nw]:

            q.append((nh+1,nw))

            dp[nh+1][nw] = dp[nh][nw]

        if nw>0 and s[nh][nw-1] =='.' and dp[nh][nw-1]>dp[nh][nw]:

            q.append((nh,nw-1))

            dp[nh][nw-1] = dp[nh][nw]

        if nw<W-1 and s[nh][nw+1] =='.' and dp[nh][nw+1]>dp[nh][nw]:

            q.append((nh,nw+1))

            dp[nh][nw+1] = dp[nh][nw]

    while visit:

    #ワープで遷移

        nh,nw = visit.pop()

        for ih in range(-2,3,1):

            for iw in range(-2,3,1):

                if (0<= nh+ih<=H-1) and (0<=nw+iw<=W-1):

                    if s[nh+ih][nw+iw] =='.' and dp[nh+ih][nw+iw]>dp[nh][nw]:

                        q.append((nh+ih,nw+iw))

                        dp[nh+ih][nw+iw] = dp[nh][nw]+1

     

 

ans = dp[Dh-1][Dw-1]

print((-1 if ans==float('inf') else ans))    

    

#abc176-d

import sys,math,collections,itertools

input = sys.stdin.readline

 

H,W=list(map(int,input().split()))

Ch,Cw=list(map(int,input().split()))

Dh,Dw=list(map(int,input().split()))

s=[ input().rstrip() for _ in range(H)]

 

#DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする

dp = [ [float('inf') ]*W  for _ in range(H)]

 

q = collections.deque([])

q.append((Ch-1,Cw-1))

dp[Ch-1][Cw-1]=0

while q:#qがある間の遷移 BSF

    visit = [] #徒歩で動いたところをメモする

    while q:#徒歩用のq

        nh,nw = q.popleft()

        visit.append((nh,nw))

        #徒歩での遷移

        move = [(-1,0),(1,0),(0,-1),(0,1)]

        for ih,iw in move:

            if  (0<= nh+ih<=H-1) and (0<=nw+iw<=W-1):

                if s[nh+ih][nw+iw] =='.' and dp[nh+ih][nw+iw]>dp[nh][nw]:

                    q.append((nh+ih,nw+iw))

                    dp[nh+ih][nw+iw] = dp[nh][nw]

    while visit:#徒歩で訪れたところからワープ先を探す

    #ワープで遷移

        nh,nw = visit.pop()

        for ih in range(-2,3,1):

            for iw in range(-2,3,1):

                if (0<= nh+ih<=H-1) and (0<=nw+iw<=W-1):

                    if s[nh+ih][nw+iw] =='.' and dp[nh+ih][nw+iw]>dp[nh][nw]:

                        q.append((nh+ih,nw+iw))

                        dp[nh+ih][nw+iw] = dp[nh][nw]+1

     

 

ans = dp[Dh-1][Dw-1]

print((-1 if ans==float('inf') else ans))    

# abc176-d
import sys, math, collections, itertools

input = sys.stdin.readline
H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
s = [input().rstrip() for _ in range(H)]
# DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする
dp = [[float("inf")] * W for _ in range(H)]
q = collections.deque([])
q.append((Ch - 1, Cw - 1))
dp[Ch - 1][Cw - 1] = 0
while q:
    visit = []
    while q:
        nh, nw = q.popleft()
        visit.append((nh, nw))
        # 徒歩での遷移
        if nh > 0 and s[nh - 1][nw] == "." and dp[nh - 1][nw] > dp[nh][nw]:
            q.append((nh - 1, nw))
            dp[nh - 1][nw] = dp[nh][nw]
        if nh < H - 1 and s[nh + 1][nw] == "." and dp[nh + 1][nw] > dp[nh][nw]:
            q.append((nh + 1, nw))
            dp[nh + 1][nw] = dp[nh][nw]
        if nw > 0 and s[nh][nw - 1] == "." and dp[nh][nw - 1] > dp[nh][nw]:
            q.append((nh, nw - 1))
            dp[nh][nw - 1] = dp[nh][nw]
        if nw < W - 1 and s[nh][nw + 1] == "." and dp[nh][nw + 1] > dp[nh][nw]:
            q.append((nh, nw + 1))
            dp[nh][nw + 1] = dp[nh][nw]
    while visit:
        # ワープで遷移
        nh, nw = visit.pop()
        for ih in range(-2, 3, 1):
            for iw in range(-2, 3, 1):
                if (0 <= nh + ih <= H - 1) and (0 <= nw + iw <= W - 1):
                    if s[nh + ih][nw + iw] == "." and dp[nh + ih][nw + iw] > dp[nh][nw]:
                        q.append((nh + ih, nw + iw))
                        dp[nh + ih][nw + iw] = dp[nh][nw] + 1
ans = dp[Dh - 1][Dw - 1]
print((-1 if ans == float("inf") else ans))

# abc176-d
import sys, math, collections, itertools

input = sys.stdin.readline
H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
s = [input().rstrip() for _ in range(H)]
# DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする
dp = [[float("inf")] * W for _ in range(H)]
q = collections.deque([])
q.append((Ch - 1, Cw - 1))
dp[Ch - 1][Cw - 1] = 0
while q:  # qがある間の遷移 BSF
    visit = []  # 徒歩で動いたところをメモする
    while q:  # 徒歩用のq
        nh, nw = q.popleft()
        visit.append((nh, nw))
        # 徒歩での遷移
        move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        for ih, iw in move:
            if (0 <= nh + ih <= H - 1) and (0 <= nw + iw <= W - 1):
                if s[nh + ih][nw + iw] == "." and dp[nh + ih][nw + iw] > dp[nh][nw]:
                    q.append((nh + ih, nw + iw))
                    dp[nh + ih][nw + iw] = dp[nh][nw]
    while visit:  # 徒歩で訪れたところからワープ先を探す
        # ワープで遷移
        nh, nw = visit.pop()
        for ih in range(-2, 3, 1):
            for iw in range(-2, 3, 1):
                if (0 <= nh + ih <= H - 1) and (0 <= nw + iw <= W - 1):
                    if s[nh + ih][nw + iw] == "." and dp[nh + ih][nw + iw] > dp[nh][nw]:
                        q.append((nh + ih, nw + iw))
                        dp[nh + ih][nw + iw] = dp[nh][nw] + 1
ans = dp[Dh - 1][Dw - 1]
print((-1 if ans == float("inf") else ans))

-while q: -    visit = [] -    while q: +while q:  # qがある間の遷移 BSF +    visit = []  # 徒歩で動いたところをメモする +    while q:  # 徒歩用のq -        if nh > 0 and s[nh - 1][nw] == "." and dp[nh - 1][nw] > dp[nh][nw]: -            q.append((nh - 1, nw)) -            dp[nh - 1][nw] = dp[nh][nw] -        if nh < H - 1 and s[nh + 1][nw] == "." and dp[nh + 1][nw] > dp[nh][nw]: -            q.append((nh + 1, nw)) -            dp[nh + 1][nw] = dp[nh][nw] -        if nw > 0 and s[nh][nw - 1] == "." and dp[nh][nw - 1] > dp[nh][nw]: -            q.append((nh, nw - 1)) -            dp[nh][nw - 1] = dp[nh][nw] -        if nw < W - 1 and s[nh][nw + 1] == "." and dp[nh][nw + 1] > dp[nh][nw]: -            q.append((nh, nw + 1)) -            dp[nh][nw + 1] = dp[nh][nw] -    while visit: +        move = [(-1, 0), (1, 0), (0, -1), (0, 1)] +        for ih, iw in move: +            if (0 <= nh + ih <= H - 1) and (0 <= nw + iw <= W - 1): +                if s[nh + ih][nw + iw] == "." and dp[nh + ih][nw + iw] > dp[nh][nw]: +                    q.append((nh + ih, nw + iw)) +                    dp[nh + ih][nw + iw] = dp[nh][nw] +    while visit:  # 徒歩で訪れたところからワープ先を探す