### BUGGY CODE ###

def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    H, W = map(int, input().split())
    sy, sx = (int(x) - 1 for x in input().split())
    gy, gx = (int(x) - 1 for x in input().split())
    G = [input().rstrip() for _ in range(H)]

    dq = deque()
    dq.append((sy, sx, 0))

    INF = H * W + 10

    ctr = [[INF] * W for _ in range(H)]
    ctr[sy][sx] = 0

    while dq:
        y, x, cnt = dq.popleft()
        if cnt > ctr[y][x]:
            continue

        if y == gy and x == gx:
            print(cnt)
            return

        if y > 0 and G[y - 1][x] == '.' and ctr[y - 1][x] > cnt:
            dq.appendleft((y - 1, x, cnt))
            ctr[y - 1][x] = cnt

        if x > 0 and G[y][x - 1] == '.' and ctr[y][x - 1] > cnt:
            dq.appendleft((y, x - 1, cnt))
            ctr[y][x - 1] = cnt

        if y < H - 1 and G[y + 1][x] == '.' and ctr[y + 1][x] > cnt:
            dq.appendleft((y + 1, x, cnt))
            ctr[y + 1][x] = cnt

        if x < W - 1 and G[y][x + 1] == '.' and ctr[y][x + 1] > cnt:
            dq.appendleft((y, x + 1, cnt))
            ctr[y][x + 1] = cnt

        ncnt = cnt + 1
        for dr in range(-2, 3):
            ny = y + dr
            if ny < 0 or ny >= H:
                continue
            for dc in range(-2, 3):
                nx = x + dc
                if nx < 0 or nx >= W:
                    continue
                if G[ny][nx] == '.' and ctr[ny][nx] > ncnt:
                    dq.append((ny, nx, ncnt))
                    ctr[ny][nx] = ncnt

    print(-1)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    H, W = map(int, input().split())
    sy, sx = (int(x) - 1 for x in input().split())
    gy, gx = (int(x) - 1 for x in input().split())
    G = [input().rstrip() for _ in range(H)]

    dq = deque()
    dq.append((sy, sx, 0))

    INF = H * W + 10

    ctr = [[INF] * W for _ in range(H)]
    ctr[sy][sx] = 0

    while dq:
        y, x, cnt = dq.popleft()
        if cnt > ctr[y][x]:
            continue

        if y > 0 and G[y - 1][x] == '.' and ctr[y - 1][x] > cnt:
            dq.appendleft((y - 1, x, cnt))
            ctr[y - 1][x] = cnt
            if y - 1 == gy and x == gx:
                print(cnt)
                return

        if x > 0 and G[y][x - 1] == '.' and ctr[y][x - 1] > cnt:
            dq.appendleft((y, x - 1, cnt))
            ctr[y][x - 1] = cnt
            if y == gy and x - 1 == gx:
                print(cnt)
                return

        if y < H - 1 and G[y + 1][x] == '.' and ctr[y + 1][x] > cnt:
            dq.appendleft((y + 1, x, cnt))
            ctr[y + 1][x] = cnt
            if y + 1 == gy and x == gx:
                print(cnt)
                return

        if x < W - 1 and G[y][x + 1] == '.' and ctr[y][x + 1] > cnt:
            dq.appendleft((y, x + 1, cnt))
            ctr[y][x + 1] = cnt
            if y == gy and x + 1 == gx:
                print(cnt)
                return

        ncnt = cnt + 1
        for dr in range(-2, 3):
            ny = y + dr
            if ny < 0 or ny >= H:
                continue
            for dc in range(-2, 3):
                nx = x + dc
                if nx < 0 or nx >= W:
                    continue
                if G[ny][nx] == '.' and ctr[ny][nx] > ncnt:
                    dq.append((ny, nx, ncnt))
                    ctr[ny][nx] = ncnt

        if ctr[gy][gx] <= cnt:
            print(cnt)
            return

    print(-1)


if __name__ == '__main__':
    main()