import sys

from collections import deque



H, W = list(map(int, sys.stdin.readline().split()))

cy, cx = list(map(int, sys.stdin.readline().split()))

gy, gx = list(map(int, sys.stdin.readline().split()))



cy -= 1

cx -= 1

gy -= 1

gx -= 1



grid = []

for _ in range(H):

    grid.append(sys.stdin.readline().strip())



q = deque()

# x, y, 累積移動距離, ブロック通過数, ワープ利用数

q.append((cx, cy, 0))

# visited = {}

visited = [[float("inf") for _ in range(W)] for _ in range(H)]

# visited[(cx, cy)] = 0

visited[cy][cx] = 0

ans = float("inf")

# 01 BFS

while q:

    x, y, c = q.popleft()

    # if (x, y) in visited and visited[(x, y)] < c:

    if visited[y][x] < c:

        # print("here1")

        continue

    # print(x, y, c, d, e)

    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):

        nx = x + dx

        ny = y + dy

        if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":

            # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:

            if visited[ny][nx] <= c:

                # print("here2")

                continue

            # visited[(nx, ny)] = c

            visited[ny][nx] = c

            if nx == gx and ny == gy:

                continue

            q.appendleft((nx, ny, c))

    for dx in range(-2, 3):

        for dy in range(-2, 3):

            if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0:

                continue

            nx = x + dx

            ny = y + dy

            # print("warp", dx, dy)

            if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":

                # print("warp append")

                # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:

                if visited[ny][nx] < c + 1:

                    # print("here3")

                    continue

                # visited[(nx, ny)] = c + 1

                visited[ny][nx] = c + 1

                if nx == gx and ny == gy:

                    continue

                q.append((nx, ny, c+1))

# print(visited)

if visited[gy][gx] == float("inf"):

    print((-1))

else:

    print((visited[gy][gx]))
import sys

from collections import deque



H, W = list(map(int, sys.stdin.readline().split()))

cy, cx = list(map(int, sys.stdin.readline().split()))

gy, gx = list(map(int, sys.stdin.readline().split()))



cy -= 1

cx -= 1

gy -= 1

gx -= 1



grid = []

for _ in range(H):

    grid.append(sys.stdin.readline().strip())



q = deque()

q2 = set()

# x, y, 累積移動距離, ブロック通過数, ワープ利用数

q.append((cx, cy, 0))

# visited = {}

visited = [[float("inf") for _ in range(W)] for _ in range(H)]

# visited[(cx, cy)] = 0

visited[cy][cx] = 0

ans = float("inf")

# 01 BFS

while q:

    while q:

        x, y, c = q.popleft()

        # if (x, y) in visited and visited[(x, y)] < c:

        if visited[y][x] < c:

            # print("here1")

            continue

        # print(x, y, c, d, e)

        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):

            nx = x + dx

            ny = y + dy

            if 0 <= nx < W and 0 <= ny < H:

                if grid[ny][nx] == "#":

                    q2.add((x, y, c))

                else:

                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:

                    if visited[ny][nx] <= c:

                        # print("here2")

                        continue

                    # visited[(nx, ny)] = c

                    visited[ny][nx] = c

                    if nx == gx and ny == gy:

                        continue

                    q.append((nx, ny, c))



    for x, y, c in q2:

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0:

                    continue

                nx = x + dx

                ny = y + dy

                # print("warp", dx, dy)

                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":

                    # print("warp append")

                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:

                    if visited[ny][nx] < c + 1:

                        # print("here3")

                        continue

                    # visited[(nx, ny)] = c + 1

                    visited[ny][nx] = c + 1

                    if nx == gx and ny == gy:

                        continue

                    q.append((nx, ny, c+1))

    q2 = set()



# print(visited)

if visited[gy][gx] == float("inf"):

    print((-1))

else:

    print((visited[gy][gx]))
import sys
from collections import deque

H, W = list(map(int, sys.stdin.readline().split()))
cy, cx = list(map(int, sys.stdin.readline().split()))
gy, gx = list(map(int, sys.stdin.readline().split()))
cy -= 1
cx -= 1
gy -= 1
gx -= 1
grid = []
for _ in range(H):
    grid.append(sys.stdin.readline().strip())
q = deque()
# x, y, 累積移動距離, ブロック通過数, ワープ利用数
q.append((cx, cy, 0))
# visited = {}
visited = [[float("inf") for _ in range(W)] for _ in range(H)]
# visited[(cx, cy)] = 0
visited[cy][cx] = 0
ans = float("inf")
# 01 BFS
while q:
    x, y, c = q.popleft()
    # if (x, y) in visited and visited[(x, y)] < c:
    if visited[y][x] < c:
        # print("here1")
        continue
    # print(x, y, c, d, e)
    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        nx = x + dx
        ny = y + dy
        if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":
            # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:
            if visited[ny][nx] <= c:
                # print("here2")
                continue
            # visited[(nx, ny)] = c
            visited[ny][nx] = c
            if nx == gx and ny == gy:
                continue
            q.appendleft((nx, ny, c))
    for dx in range(-2, 3):
        for dy in range(-2, 3):
            if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0:
                continue
            nx = x + dx
            ny = y + dy
            # print("warp", dx, dy)
            if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":
                # print("warp append")
                # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:
                if visited[ny][nx] < c + 1:
                    # print("here3")
                    continue
                # visited[(nx, ny)] = c + 1
                visited[ny][nx] = c + 1
                if nx == gx and ny == gy:
                    continue
                q.append((nx, ny, c + 1))
# print(visited)
if visited[gy][gx] == float("inf"):
    print((-1))
else:
    print((visited[gy][gx]))

import sys
from collections import deque

H, W = list(map(int, sys.stdin.readline().split()))
cy, cx = list(map(int, sys.stdin.readline().split()))
gy, gx = list(map(int, sys.stdin.readline().split()))
cy -= 1
cx -= 1
gy -= 1
gx -= 1
grid = []
for _ in range(H):
    grid.append(sys.stdin.readline().strip())
q = deque()
q2 = set()
# x, y, 累積移動距離, ブロック通過数, ワープ利用数
q.append((cx, cy, 0))
# visited = {}
visited = [[float("inf") for _ in range(W)] for _ in range(H)]
# visited[(cx, cy)] = 0
visited[cy][cx] = 0
ans = float("inf")
# 01 BFS
while q:
    while q:
        x, y, c = q.popleft()
        # if (x, y) in visited and visited[(x, y)] < c:
        if visited[y][x] < c:
            # print("here1")
            continue
        # print(x, y, c, d, e)
        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            nx = x + dx
            ny = y + dy
            if 0 <= nx < W and 0 <= ny < H:
                if grid[ny][nx] == "#":
                    q2.add((x, y, c))
                else:
                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:
                    if visited[ny][nx] <= c:
                        # print("here2")
                        continue
                    # visited[(nx, ny)] = c
                    visited[ny][nx] = c
                    if nx == gx and ny == gy:
                        continue
                    q.append((nx, ny, c))
    for x, y, c in q2:
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0:
                    continue
                nx = x + dx
                ny = y + dy
                # print("warp", dx, dy)
                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#":
                    # print("warp append")
                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1:
                    if visited[ny][nx] < c + 1:
                        # print("here3")
                        continue
                    # visited[(nx, ny)] = c + 1
                    visited[ny][nx] = c + 1
                    if nx == gx and ny == gy:
                        continue
                    q.append((nx, ny, c + 1))
    q2 = set()
# print(visited)
if visited[gy][gx] == float("inf"):
    print((-1))
else:
    print((visited[gy][gx]))

+q2 = set() -    x, y, c = q.popleft() -    # if (x, y) in visited and visited[(x, y)] < c: -    if visited[y][x] < c: -        # print("here1") -        continue -    # print(x, y, c, d, e) -    for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): -        nx = x + dx -        ny = y + dy -        if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#": -            # if (nx, ny) in visited and visited[(nx, ny)] < c + 1: -            if visited[ny][nx] <= c: -                # print("here2") -                continue -            # visited[(nx, ny)] = c -            visited[ny][nx] = c -            if nx == gx and ny == gy: -                continue -            q.appendleft((nx, ny, c)) -    for dx in range(-2, 3): -        for dy in range(-2, 3): -            if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0: -                continue +    while q: +        x, y, c = q.popleft() +        # if (x, y) in visited and visited[(x, y)] < c: +        if visited[y][x] < c: +            # print("here1") +            continue +        # print(x, y, c, d, e) +        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): -            # print("warp", dx, dy) -            if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#": -                # print("warp append") -                # if (nx, ny) in visited and visited[(nx, ny)] < c + 1: -                if visited[ny][nx] < c + 1: -                    # print("here3") +            if 0 <= nx < W and 0 <= ny < H: +                if grid[ny][nx] == "#": +                    q2.add((x, y, c)) +                else: +                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1: +                    if visited[ny][nx] <= c: +                        # print("here2") +                        continue +                    # visited[(nx, ny)] = c +                    visited[ny][nx] = c +                    if nx == gx and ny == gy: +                        continue +                    q.append((nx, ny, c)) +    for x, y, c in q2: +        for dx in range(-2, 3): +            for dy in range(-2, 3): +                if abs(dx) == 1 and dy == 0 or abs(dy) == 1 and dx == 0: -                # visited[(nx, ny)] = c + 1 -                visited[ny][nx] = c + 1 -                if nx == gx and ny == gy: -                    continue -                q.append((nx, ny, c + 1)) +                nx = x + dx +                ny = y + dy +                # print("warp", dx, dy) +                if 0 <= nx < W and 0 <= ny < H and grid[ny][nx] != "#": +                    # print("warp append") +                    # if (nx, ny) in visited and visited[(nx, ny)] < c + 1: +                    if visited[ny][nx] < c + 1: +                        # print("here3") +                        continue +                    # visited[(nx, ny)] = c + 1 +                    visited[ny][nx] = c + 1 +                    if nx == gx and ny == gy: +                        continue +                    q.append((nx, ny, c + 1)) +    q2 = set()