### BUGGY CODE ###

from collections import deque
H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    while q:
        v = q.popleft()
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    jump_list = []
    for i in range(H):
        for j in range(W):
            if dist[i][j] == cost:
                jump_list.append((i, j))

    cost += 1
    q = deque(list(jump_list))
    while q:
        v = q.popleft()
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == 0:
                    continue
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == 0:
                    continue
                # print(i, j, S[i][j])
                if i >= 0 and j >= 0 and i < H and j < W and (i != v[0] or j != v[1]) and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
    
    jump_list = []
    for i in range(H):
        for j in range(W):
            if dist[i][j] == cost:
                jump_list.append((i, j))
    q = deque(jump_list)

# p_dist()
print(dist[Dh][Dw])

### FIXED CODE ###

from collections import deque

H, W = [int(a) for a in input().split()]
Ch, Cw = [int(a)-1 for a in input().split()]
Dh, Dw = [int(a)-1 for a in input().split()]

S = []
dist = []

for _ in range(H):
    S.append([a for a in input()])
    dist.append([-1 for _ in range(len(S[-1]))])

dist[Ch][Cw] = 0
q = deque()
q.append((Ch, Cw))
cost = 0

def p_dist():
    print('---')
    for row in dist:
        print(row)

while q:
    jump_list = []
    while q:
        v = q.popleft()
        jump_list.append(v)
        # print('n', v)
        for i in [v[0]-1, v[0]+1]:
            j = v[1]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))
        for j in [v[1]-1, v[1]+1]:
            i = v[0]
            if i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                dist[i][j] = cost
                q.append((i, j))

    cost += 1
    for v in jump_list:
        # print('j', v)
        for i in range(v[0]-2, v[0]+3):
            for j in range(v[1]-2, v[1]+3):
                if ((i == v[0] + 1) or (i == v[0] -1)) and j == v[1]:
                    pass
                elif ((j == v[1] + 1) or (j == v[1] -1)) and i == v[0]:
                    pass
                elif i == v[0] and j == v[1]:
                    pass
                elif i >= 0 and j >= 0 and i < H and j < W and S[i][j]=='.' and dist[i][j] == -1:
                    # print(i, j, dist[i][j])
                    dist[i][j] = cost
                    q.append((i, j))

# p_dist()
print(dist[Dh][Dw])