### BUGGY CODE ###

def solve(now_x, now_y):
    for next_y in range(-2, 3):
        for next_x in range(-2, 3):
            x, y = now_x + next_x, now_y + next_y
            if 0 <= x < w and 0 <= y < h:
                if c[y][x] != "#" and c[y][x] != "=" and c[y][x] != "!":
                    c[y] = c[y][:x] + "!" + c[y][x + 1 :]
                    d2.append([x, y])


from collections import deque
import sys

input = sys.stdin.readline

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
c = [input() for _ in range(h)]
cw -= 1
ch -= 1
dw -= 1
dh -= 1
c[ch] = c[ch][:cw] + "=" + c[ch][cw + 1 :]
d1 = deque([[cw, ch]])
d2 = deque([])

cnt = 0
while True:
    while True:
        for _ in range(len(d1)):
            now_x, now_y = d1.popleft()
            for next_x, next_y in zip([1, 0, -1, 0], [0, 1, 0, -1]):
                x, y = now_x + next_x, now_y + next_y
                if 0 <= x < w and 0 <= y < h:
                    if x == dw and y == dh:
                        print(cnt)
                        exit()
                    if c[y][x] == "#":
                        solve(now_x, now_y)
                    if c[y][x] != "#" and c[y][x] != "=":
                        c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                        d1.append([x, y])

    else:
        cnt += 1
        if [dw, dh] in d2:
            print(cnt)
            exit()
        if not d2:
            print(-1)
            exit()
        d1 = d2.copy()
        d2.clear()

### FIXED CODE ###

def main():
    from collections import deque
    import sys

    input = sys.stdin.readline
    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    c = [input().rstrip() for _ in range(h)]
    cw -= 1
    ch -= 1
    dw -= 1
    dh -= 1
    c[ch] = c[ch][:cw] + "=" + c[ch][cw + 1 :]
    bfs = deque([[cw, ch]])
    warp_before = set()
    warp_after = set()

    for cnt in range(1000000):  # ワープ回数のカウント
        while bfs:  # ワープせずに歩いてどこまで行けるか
            for _ in range(len(bfs)):
                now_x, now_y = bfs.popleft()
                for next_x, next_y in zip([1, 0, -1, 0], [0, 1, 0, -1]):
                    x, y = now_x + next_x, now_y + next_y
                    if 0 <= x < w and 0 <= y < h:
                        if x == dw and y == dh:
                            print(cnt)
                            exit()
                        c_next = c[y][x]
                        if c_next == "#":  # 壁の隣マスの座標をwarp_beforeに加える
                            warp_before.add((now_x, now_y))
                        if c_next == ".":  # 走査済マーク
                            c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                            bfs.append([x, y])
        else:
            if not warp_before:
                print(-1)
                exit()
            for now_x, now_y in warp_before:
                # 壁の隣マスの座標を中心に5*5マスの範囲で、壁の外側の未到達マスの座標をwarp_afterに加える
                if c[now_y][now_x] == "=":
                    for next_y in range(-2, 3):
                        for next_x in range(-2, 3):
                            x, y = now_x + next_x, now_y + next_y
                            if 0 <= x < w and 0 <= y < h:
                                warp_after.add((x, y))
            for x, y in warp_after:
                # 壁の外側の未到達マスの座標のゴール判定・走査済マーク「=」を付けてbfsに格納
                if c[y][x] == ".":
                    if x == dw and y == dh:  # 終了(ゴール)判定
                        print(cnt + 1)
                        exit()
                    c[y] = c[y][:x] + "=" + c[y][x + 1 :]
                    bfs.append([x, y])
            warp_before = set()
            warp_after = set()


if __name__ == "__main__":
    main()