### BUGGY CODE ###

from collections import deque

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
S = [None]*(H+4)
S[0] = S[1] = S[H+2] = S[H+3] = ["#"]*(W+4)
for i in range(2, H+2):
    S[i] = ["#"] + ["#"] + list(input()) + ["#"] + ["#"]

cost = [[-1 for _ in range(W+3)] for _ in range(H+3)]

Ch += 1
Cw += 1
Dh += 1
Dw += 1

# bfs

warp_times = 0

def warp(i, j, cost, warp_times, q):
    change = 0
    for h in range(i-2, i+3):
        for w in range(j-2, j+3):
            # print(h, w, i, j)
            # print(S[h][w])
            if S[h][w] == "." and cost[h][w] == -1:
                q.append([h, w])
                cost[h][w] = warp_times + 1
                change = 1
    
    return change

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

while True:
    

    same_cost = list()
    no_change = 1

    while len(q):
        # print(q)
        now = q.popleft()
        i = now[0]
        j = now[1]

        cost[i][j] = warp_times
        same_cost.append([i, j])

        if S[i][j-1] == "." and cost[i][j-1] == -1:
            q.append([i, j-1])
        if S[i][j+1] == "." and cost[i][j+1] == -1:
            q.append([i, j+1])
        if S[i-1][j] == "." and cost[i-1][j] == -1:
            q.append([i-1, j])
        if S[i+1][j] == "." and cost[i+1][j] == -1:
            q.append([i+1, j])
    

    for point in same_cost:
        change = warp(point[0], point[1], cost, warp_times, q)
        if change :
            no_change = 0
    
    warp_times += 1
    
    if no_change:
        break

print(cost[Dh][Dw])


### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())
S = [None]*(H+4)
S[0] = S[1] = S[H+2] = S[H+3] = ["#"]*(W+4)
for i in range(2, H+2):
    S[i] = ["#"] + ["#"] + list(input()) + ["#"] + ["#"]

cost = [[-1 for _ in range(W+3)] for _ in range(H+3)]

Ch += 1
Cw += 1
Dh += 1
Dw += 1

# bfs

warp_times = 0

def warp(i, j, cost, warp_times, q):
    change = 0
    for h in range(i-2, i+3):
        for w in range(j-2, j+3):
            # print(h, w, i, j)
            # print(S[h][w])
            if S[h][w] == "." and cost[h][w] == -1:
                q.append([h, w])
                cost[h][w] = warp_times + 1
                change = 1
    
    return change

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

while True:
    

    same_cost = list()
    no_change = 1

    while len(q):
        # print(q)
        now = q.popleft()
        i = now[0]
        j = now[1]

        cost[i][j] = warp_times
        same_cost.append([i, j])

        if S[i][j-1] == "." and cost[i][j-1] == -1:
            q.append([i, j-1])
            cost[i][j-1] = warp_times
        if S[i][j+1] == "." and cost[i][j+1] == -1:
            q.append([i, j+1])
            cost[i][j+1] = warp_times
        if S[i-1][j] == "." and cost[i-1][j] == -1:
            q.append([i-1, j])
            cost[i-1][j] = warp_times
        if S[i+1][j] == "." and cost[i+1][j] == -1:
            q.append([i+1, j])
            cost[i-1][j] = warp_times
    

    for point in same_cost:
        change = warp(point[0], point[1], cost, warp_times, q)
        if change :
            no_change = 0
    
    warp_times += 1
    
    if no_change:
        break

print(cost[Dh][Dw])