def main():
    from collections import deque
    import sys
    sys.setrecursionlimit(100000)

    h, w = map(int, input().split())
    start = tuple(map(lambda x: int(x) - 1, input().split()))
    goal = tuple(map(lambda x: int(x) - 1, input().split()))

    maze = tuple([tuple(input()) for _ in range(h)])
    cost = [[float('Inf')] * w for _ in range(h)]
    cost[start[0]][start[1]] = 0
    vis = [[False] * w for _ in range(h)]

    def fill(nx, c):
        if len(nx) == 0:
            return
        q = deque(nx)
        wall = []
        while q:
            i, j = q.pop()
            cost[i][j] = c
            vis[i][j] = True
            for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                if 0 <= i + di < h and 0 <= j + dj < w:
                    if (i + di, j + dj) == goal:
                        print(c)
                        exit()
                    if vis[i + di][j + dj]:
                        continue
                    if maze[i + di][j + dj] == '#':
                        wall.append((i + di, j + dj))
                    else:
                        q.append((i + di, j + dj))

        jump(wall, c + 1)

    def jump(wall, c):
        nx = []
        q = deque(wall)
        while q:
            i, j = q.pop()
            cost[i][j] = c
            for di in range(-1, 2):
                for dj in range(-1, 2):
                    if 0 <= i + di < h and 0 <= j + dj < w:
                        if (i + di, j + dj) == goal:
                            print(c)
                            exit()
                        if vis[i + di][j + dj]:
                            continue
                        if maze[i + di][j + dj] == '.':
                            cost[i + di][j + dj] = c
                            nx.append((i + di, j + dj))

        fill(nx, c)

    fill([start], 0)
    print(-1)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    from collections import deque

    h, w = map(int, input().split())
    ch, cw = map(lambda x: int(x) - 1, input().split())
    dh, dw = map(lambda x: int(x) - 1, input().split())

    m = [list(input()) for _ in range(h)]
    vis = [[False for _ in range(w + 2)] for _ in range(h + 2)]

    def walk(i, j):
        q = deque()
        q.append((i, j))
        edge = []
        while q:
            y, x = q.popleft()
            if vis[y][x]:
                continue
            vis[y][x] = True
            for s, t in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                if not 0 <= y + s <= h - 1 or not 0 <= x + t <= w - 1:
                    continue
                if m[y + s][x + t] == '#':
                    edge.append((y, x))
                    continue
                q.appendleft((y + s, x + t))
        return edge

    def jump(i, j, c):
        q = walk(i, j)
        if vis[dh][dw]:
            return c
        nx = []
        c += 1
        while q:
            y, x = q.pop()
            for s in range(-2, 3):
                for t in range(-2, 3):
                    if not 0 <= y + s <= h - 1 or not 0 <= x + t <= w - 1:
                        continue
                    if vis[y + s][x + t]:
                        continue
                    if m[y + s][x + t] == '#':
                        continue
                    nx += walk(y + s, x + t)
                    if vis[dh][dw]:
                        return c
            if len(q) == 0:
                c += 1
                q = nx
                nx = []

    ans = jump(ch, cw, 0)
    print(ans if ans is not None else -1)


if __name__ == '__main__':
    main()