### BUGGY CODE ###

import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import deque

    h, w = map(int, readline().split())
    sr, sc = map(int, readline().split())
    gr, gc = map(int, readline().split())

    grid = [["#"] * (w + 2)]
    for _ in range(h):
        s = ["#"] + list(input()) + ["#"]
        grid.append(s)
    grid.append(["#"] * (w + 2))

    ans = 0
    que1 = deque()
    que1.append((sr, sc, 0))
    grid[sr][sc] = "!"

    while que1:
        cr, cc, cost = que1.popleft()
        if cr == gr and cc == gc:
            return print(cost)

        if grid[cr + 1][cc] == ".":
            que1.appendleft((cr + 1, cc, cost))
            grid[cr + 1][cc] = "!"
        if grid[cr - 1][cc] == ".":
            que1.appendleft((cr - 1, cc, cost))
            grid[cr - 1][cc] = "!"
        if grid[cr][cc + 1] == ".":
            que1.appendleft((cr, cc + 1, cost))
            grid[cr][cc + 1] = "!"
        if grid[cr][cc - 1] == ".":
            que1.appendleft((cr, cc - 1, cost))
            grid[cr][cc - 1] = "!"

        for mr in range(-2, 3):
            for mc in range(-2, 3):
                nr = cr + mr
                nc = cc + mc
                if 1 <= nr <= h and 1 <= nc <= w:
                    if grid[nr][nc] == ".":
                        que1.append((nr, nc, cost + 1))

    return print(-1)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import deque, defaultdict

    h, w = map(int, readline().split())
    sr, sc = map(int, readline().split())
    gr, gc = map(int, readline().split())

    grid = [["#"] * (w + 2)]
    for _ in range(h):
        s = ["#"] + list(input()) + ["#"]
        grid.append(s)
    grid.append(["#"] * (w + 2))

    ans = 0
    que1 = deque()
    que2 = deque()
    que1.append((sr, sc))
    grid[sr][sc] = "!"

    while que1 or que2:
        while que1:
            cr, cc = que1.popleft()
            que2.append((cr, cc))

            if cr == gr and cc == gc:
                return print(ans)

            if grid[cr + 1][cc] == ".":
                que1.appendleft((cr + 1, cc))
                grid[cr + 1][cc] = "!"
            if grid[cr - 1][cc] == ".":
                que1.appendleft((cr - 1, cc))
                grid[cr - 1][cc] = "!"
            if grid[cr][cc + 1] == ".":
                que1.appendleft((cr, cc + 1))
                grid[cr][cc + 1] = "!"
            if grid[cr][cc - 1] == ".":
                que1.appendleft((cr, cc - 1))
                grid[cr][cc - 1] = "!"

        ans += 1

        while que2:
            cr, cc = que2.popleft()
            for mr in range(-2, 3):
                for mc in range(-2, 3):
                    nr = cr + mr
                    nc = cc + mc
                    if 1 <= nr <= h and 1 <= nc <= w:
                        if grid[nr][nc] == ".":
                            que1.append((nr, nc))
                            grid[nr][nc] = "!"

    return print(-1)


if __name__ == '__main__':
    main()