### BUGGY CODE ###

import heapq


def main():
    INF = 10 ** 7
    H, W = list(map(int, input().split()))
    CH, CW = list(map(lambda x: int(x) - 1, input().split()))
    DH, DW = list(map(lambda x: int(x) - 1, input().split()))
    S = [input() for _ in range(H)]
    if S[DH][DW] == '#':
        print(-1)
        return
    # 使うワープの最小回数
    cnt = [[INF for _ in range(W)] for _ in range(H)]

    que = [(0, DH, DW)]
    cnt[DH][DW] = 0
    while len(que) > 0:
        c, h, w = heapq.heappop(que)
        for dh, dw in [[-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], \
            [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], \
            [0, -2], [0, -1], [0, 1], [0, 2], \
            [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], \
            [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]]:
            nh, nw = h + dh, w + dw
            if not (0 <= nh < H) or not (0 <= nw < W) or S[nh][nw] == '#':
                continue
            nc = c + 1
            if [dh, dw] in [[1, 0], [-1, 0], [0, -1], [0, 1]]:
                nc = c
            if cnt[nh][nw] > nc:
                cnt[nh][nw] = nc
                heapq.heappush(que, (nc, nh, nw))

    if cnt[CH][CW] == INF:
        print(-1)
    else:
        print(cnt[CH][CW])


if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import deque


def main():
    INF = 10 ** 7
    H, W = list(map(int, input().split()))
    CH, CW = list(map(lambda x: int(x) - 1, input().split()))
    DH, DW = list(map(lambda x: int(x) - 1, input().split()))
    S = [input() for _ in range(H)]
    if S[DH][DW] == '#':
        print(-1)
        return
    # 歩き
    walk_list = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    # ワープ
    warp_list = [[-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], \
            [-1, -2], [-1, -1], [-1, 1], [-1, 2], \
            [0, -2], [0, 2], \
            [1, -2], [1, -1], [1, 1], [1, 2], \
            [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]]
    # 使うワープの最小回数
    cnt = [[INF for _ in range(W)] for _ in range(H)]

    warp_checked = [[0 for _ in range(W)] for _ in range(H)]
    warp_que = deque([(DH, DW)])
    c = 0
    while len(warp_que) > 0:
        # warp_queから歩きのみでいける範囲の探索
        walk_que = deque()
        while len(warp_que) > 0:
            h, w = warp_que.pop()
            cnt[h][w] = c
            walk_que.append((h, w))
            tmp_walk_que = deque([(h, w)])
            while len(tmp_walk_que) > 0:
                h, w = tmp_walk_que.pop()
                for dh, dw in walk_list:
                    nh, nw = h + dh, w + dw
                    if 0 <= nh < H and 0 <= nw < W and \
                        S[nh][nw] == '.' and cnt[nh][nw] == INF:
                        cnt[nh][nw] = c
                        tmp_walk_que.append((nh, nw))
                        walk_que.append((nh, nw))
        # ワープでしかいけない範囲の探索
        while len(walk_que) > 0:
            h, w = walk_que.pop()
            for dh, dw in warp_list:
                nh, nw = h + dh, w + dw
                if 0 <= nh < H and 0 <= nw < W and S[nh][nw] == '.' and \
                    warp_checked[nh][nw] == 0 and cnt[nh][nw] == INF:
                    warp_checked[nh][nw] = 1
                    warp_que.append((nh, nw))
        c += 1

    if cnt[CH][CW] == INF:
        print(-1)
    else:
        print(cnt[CH][CW])


if __name__ == '__main__':
    main()