### BUGGY CODE ###

import sys
from collections import deque
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = []
for _ in range(H):
    S.append(input())
P = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            P[i][j] = -1
P[Ch][Cw] = 1
P[Dh][Dw] = 100
yudlr = [0, 0, 1, -1]
xudlr = [1, -1, 0, 0]
D = deque()
Q = deque()
Q.append([Ch, Cw])
c = 0
while len(Q) > 0 or len(D) > 0:
    while len(Q) > 0:
        q = Q.popleft()
        y, x = q[0], q[1]
        for i in range(4):
            y1 = y + yudlr[i]
            x1 = x + xudlr[i]
            if y1 < 0 or y1 >= H or x1 < 0 or x1 >= W:
                continue
            if P[y1][x1] == 100:
                print(c)
                sys.exit()
            if P[y1][x1] == -1 and P[y][x] == 1:
                D.append([y, x])
                P[y][x] = 2
                continue
            if P[y1][x1] != 0:
                continue
            Q.append([y1, x1])
            P[y1][x1] = 1
    c += 1
    while len(D) > 0:
        d = D.popleft()
        y, x = d[0], d[1]
        for i in range(-2, 3):
            for j in range(-2, 3):
                y1 = y + i
                x1 = x + j
                if y1 < 0 or y1 >= H or x1 < 0 or x1 >= W:
                    continue
                if P[y1][x1] == 100:
                    print(c)
                    sys.exit()
                if P[y1][x1] != 0:
                    continue
                Q.append([y1, x1])
                P[y1][x1] = 1
print(-1)

### FIXED CODE ###

import sys
from collections import deque
H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Ch -= 1
Cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
S = []
for _ in range(H):
    S.append(input())
P = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            P[i][j] = -1
P[Ch][Cw] = 1
P[Dh][Dw] = 100
yudlr = [0, 0, 1, -1]
xudlr = [1, -1, 0, 0]
D = deque()
Q = deque()
Q.append([Ch, Cw])
c = 0
while len(Q) > 0 or len(D) > 0:
    while len(Q) > 0:
        q = Q.popleft()
        y, x = q[0], q[1]
        for i in range(4):
            y1 = y + yudlr[i]
            x1 = x + xudlr[i]
            if y1 < 0 or y1 >= H or x1 < 0 or x1 >= W:
                continue
            if P[y1][x1] == 100:
                print(c)
                sys.exit()
            if P[y1][x1] == -1 and P[y][x] == 1:
                D.append([y, x])
                P[y][x] = 2
                continue
            if P[y1][x1] != 0:
                continue
            Q.append([y1, x1])
            P[y1][x1] = 1
    c += 1
    while len(D) > 0:
        d = D.popleft()
        y, x = d[0], d[1]
        for i in range(-2, 3):
            for j in range(-2, 3):
                y1 = y + i
                x1 = x + j
                if y1 < 0 or y1 >= H or x1 < 0 or x1 >= W:
                    continue
                if P[y1][x1] == 100:
                    print(c)
                    sys.exit()
                if P[y1][x1] != 0:
                    continue
                Q.append([y1, x1])
                P[y1][x1] = 1
print(-1)