### BUGGY CODE ###

import sys
from collections import deque
from itertools import product
input = sys.stdin.readline


def validate_pos(y, x, h, w):
    if y < 0 or y >= h or x < 0 or x >= w:
        return False
    else:
        return True


def main():
    h, w = map(int, input().split())
    sy, sx = map(int, input().split())
    gy, gx = map(int, input().split())
    field = [input().rstrip() for _ in range(h)]
    seen = set()
    todo = deque([(sy-1, sx-1)])
    tmp_visited = []

    shifts = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    warp_dist = [-2, -1, 0, 1, 2]
    warps = list(product(warp_dist, repeat=2))
    ans = 0
    while True:
        while todo:
            y, x = todo.popleft()
            tmp_visited.append((y, x))

            for dy, dx in shifts:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if field[ny][nx] == "#":
                    continue
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))
                tmp_visited.append((ny, nx))

        ans += 1
        if not tmp_visited:
            print(-1)
            return

        for y, x in tmp_visited:
            for dy, dx in warps:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if field[ny][nx] == "#":
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))

        tmp_visited = []


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
from collections import deque
from itertools import product
input = sys.stdin.readline


def validate_pos(y, x, h, w):
    if y < 0 or y >= h or x < 0 or x >= w:
        return False
    else:
        return True


def main():
    h, w = map(int, input().split())
    sy, sx = map(int, input().split())
    gy, gx = map(int, input().split())
    field = [input().rstrip() for _ in range(h)]
    seen = set()
    todo = deque([(sy-1, sx-1)])
    tmp_visited = []

    shifts = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    warp_dist = [-2, -1, 0, 1, 2]
    warps = list(product(warp_dist, repeat=2))
    ans = 0
    while True:
        while todo:
            y, x = todo.popleft()
            tmp_visited.append((y, x))

            for dy, dx in shifts:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if field[ny][nx] == "#":
                    continue
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))
                tmp_visited.append((ny, nx))

        ans += 1
        if not tmp_visited:
            print(-1)
            return

        for y, x in tmp_visited:
            for dy, dx in warps:
                ny, nx = y+dy, x+dx
                if not validate_pos(ny, nx, h, w):
                    continue
                if field[ny][nx] == "#":
                    continue
                if ny == gy-1 and nx == gx-1:
                    print(ans)
                    return
                if (ny, nx) in seen:
                    continue
                todo.append((ny, nx))
                seen.add((ny, nx))

        tmp_visited = []


if __name__ == "__main__":
    main()