### BUGGY CODE ###

from collections import deque

diffs = [(-1, 0), (0, -1), (0, 1), (1, 0)]

h, w = map(int, input().split())
start_point = tuple(map(lambda x: int(x) - 1, input().split()))
goal_point = tuple(map(lambda x: int(x) - 1, input().split()))
maze = [input() for _ in range(h)]

counts = [[-1] * w for _ in range(h)]

counts[start_point[0]][start_point[1]] = 0
point_queue = deque([start_point])

while point_queue:
    connected_queue = deque()
    while point_queue:
        curr_h, curr_w = point_queue.pop()
        connected_queue.append((curr_h, curr_w))

        for diff_h, diff_w in diffs:
            next_h = curr_h + diff_h
            next_w = curr_w + diff_w

            if not (0 <= next_h < h and 0 <= next_w < w):
                continue

            if maze[next_h][next_w] == '#':
                continue

            if (next_h, next_w) == goal_point:
                print(counts[curr_h][curr_w])
                exit(0)

            if counts[next_h][next_w] != -1:
                continue

            counts[next_h][next_w] = counts[curr_h][curr_w]
            point_queue.append((next_h, next_w))
            connected_queue.append((next_h, next_w))

    while connected_queue:
        curr_h, curr_w = connected_queue.pop()
        for diff_h in range(-2, 3):
            for diff_w in range(-2, 3):
                next_h = curr_h + diff_h
                next_w = curr_w + diff_w

                if not (0 <= next_h < h and 0 <= next_w < w):
                    continue

                if maze[next_h][next_w] == '#':
                    continue

                if (next_h, next_w) == goal_point:
                    print(counts[curr_h][curr_w] + 1)
                    exit(0)

                if counts[next_h][next_w] != -1:
                    continue

                counts[next_h][next_w] = counts[curr_h][curr_w] + 1
                point_queue.append((next_h, next_w))

print(-1)


### FIXED CODE ###

from collections import deque

diffs = [(-1, 0), (0, -1), (0, 1), (1, 0)]

h, w = map(int, input().split())
start_point = tuple(map(lambda x: int(x) - 1, input().split()))
goal_point = tuple(map(lambda x: int(x) - 1, input().split()))
maze = [input() for _ in range(h)]

counts = [[-1] * w for _ in range(h)]

counts[start_point[0]][start_point[1]] = 0
point_queue = deque([start_point])

while point_queue:
    connected_queue = deque()
    while point_queue:
        curr_h, curr_w = point_queue.pop()
        connected_queue.append((curr_h, curr_w))

        for diff_h, diff_w in diffs:
            next_h = curr_h + diff_h
            next_w = curr_w + diff_w

            if not (0 <= next_h < h and 0 <= next_w < w):
                continue

            if maze[next_h][next_w] == '#':
                continue

            if (next_h, next_w) == goal_point:
                print(counts[curr_h][curr_w])
                exit(0)

            if counts[next_h][next_w] != -1:
                continue

            counts[next_h][next_w] = counts[curr_h][curr_w]
            point_queue.append((next_h, next_w))
            connected_queue.append((next_h, next_w))

    while connected_queue:
        curr_h, curr_w = connected_queue.pop()
        for diff_h in range(-2, 3):
            for diff_w in range(-2, 3):
                next_h = curr_h + diff_h
                next_w = curr_w + diff_w

                if not (0 <= next_h < h and 0 <= next_w < w):
                    continue

                if maze[next_h][next_w] == '#':
                    continue

                if (next_h, next_w) == goal_point:
                    print(counts[curr_h][curr_w] + 1)
                    exit(0)

                if counts[next_h][next_w] != -1:
                    continue

                counts[next_h][next_w] = counts[curr_h][curr_w] + 1
                point_queue.append((next_h, next_w))

print(-1)