### BUGGY CODE ###

from collections import deque

h,w = map(int,input().split())#行,列
C = list(map(int,input().split()))
D = list(map(int,input().split()))
S = [list(input()) for _ in range(h)]

cost = {i:{j:-1 for j in range(w)} for i in range(h)}
cost[C[0]-1][C[1]-1]=0
cost_base = 0

ok = True

q=deque()
while cost[D[0]-1][D[1]-1]==-1:
    #スタート(初回)と、前回ループで移動Bで初めて行けたマスの登録
    for i in range(h):
        for j in range(w):
            if cost[i][j]==cost_base:
                q.append([i+1,j+1])
                
    #どちらの移動でも、ゴール踏破前に未踏破のマスに行けなかった場合
    if len(q)==0:
        ok = False
        break
                
    #移動Aで行けるマスを全探索
    while len(q)>0:
        now = q.popleft()
        dh = [-1,1]
        dw = [-1,1]
        for i in dh:
            if now[0]+i<1 or now[0]+i>h:
                continue
            if S[now[0]+i-1][now[1]-1]=='.' and cost[now[0]+i-1][now[1]-1]==-1:
                cost[now[0]+i-1][now[1]-1]=cost_base
                q.append([now[0]+i,now[1]])
        for j in dw:
            if now[1]+j<1 or now[1]+j>w:
                continue
            if S[now[0]-1][now[1]+j-1]=='.' and cost[now[0]-1][now[1]+j-1]==-1:
                cost[now[0]-1][now[1]+j-1]=cost_base
                q.append([now[0],now[1]+j])

    #移動Aで行けたマスから移動Bで行け、かつ今まで行けなかったマスの探索
    for _h in range(h):
        for _w in range(w):
            if cost[_h][_w]==cost_base:
                
                dr = [-2,-1,0,1,2]
                for i in dr:
                    for j in dr:
                        if _h+1+i<1 or _h+1+i>h:
                            continue
                        if _w+1+j<1 or _w+1+j>w:
                            continue

                        if S[_h+i][_w+j]=='.' and cost[_h+i][_w+j]==-1:
                            cost[_h+i][_w+j]=cost_base+1
    cost_base+=1
    
if ok:
    print(cost[D[0]-1][D[1]-1])
else:
    print(-1)

### FIXED CODE ###

from collections import deque

h,w = map(int,input().split())#行,列
C = list(map(int,input().split()))
D = list(map(int,input().split()))
S = [list(input()) for _ in range(h)]

seen = [[0]*w for _ in range(h)]
cost = [[-1]*w for _ in range(h)]
cost[C[0]-1][C[1]-1]=0
cost_base = 0
for i in range(h):
    for j in range(w):
        if S[i][j]=='#':
            cost[i][j]=-2

ok = True

q=deque()
q.append(C)
                
while len(q)>0:
    #移動A
    now = q.popleft()
    if seen[now[0]-1][now[1]-1] == 1:
        continue
    costnow = cost[now[0]-1][now[1]-1]
    seen[now[0]-1][now[1]-1] = 1

    for i in range(-1,2,2):
        if now[0]+i<1 or now[0]+i>h:
            continue
        if cost[now[0]+i-1][now[1]-1]==-1 or cost[now[0]+i-1][now[1]-1]>costnow:
            cost[now[0]+i-1][now[1]-1]=costnow
            q.appendleft([now[0]+i,now[1]])
    for i in range(-1,2,2):
        if now[1]+i<1 or now[1]+i>w:
            continue
        if cost[now[0]-1][now[1]+i-1]==-1 or cost[now[0]-1][now[1]+i-1]>costnow:
            cost[now[0]-1][now[1]+i-1]=costnow
            q.appendleft([now[0],now[1]+i])

    #移動B
    for i in range(-2,3):
        for j in range(-2,3):
            if now[0]+i<1 or now[0]+i>h:
                continue
            if now[1]+j<1 or now[1]+j>w:
                continue

            if cost[now[0]+i-1][now[1]+j-1]==-1:
                cost[now[0]+i-1][now[1]+j-1]=costnow+1
                q.append([now[0]+i,now[1]+j])

print(cost[D[0]-1][D[1]-1])