from collections import deque



h, w = (int(x) for x in input().split())

ch, cw = (int(x) - 1 for x in input().split())

dh, dw = (int(x) - 1 for x in input().split())

S = [list(eval(input())) for _ in range(h)]

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

grid[ch][cw] = 0

walk = [[0, 1], [0, -1], [1, 0], [-1, 0]]

warp = [[2, 2], [2, 1], [2, 0], [2, -1], [2, -2], [1, 2], [1, 1], [1, -1], [1, -2], [0, 2], [0, -2], [-1, 2], [-1, 1],

     [-1, -1], [-1, -2], [-2, 2], [-2, 1], [-2, 0], [-2, -1], [-2, -2]]



que = deque([(0, ch, cw)])

while que:

    count, y, x = que.popleft()

    if grid[y][x] < count:

        continue

    for NY, NX in walk:

        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != '#':

            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count:

                grid[y + NY][x + NX] = count

                que.appendleft((count, y + NY, x + NX))

    for NY, NX in warp:

        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != '#':

            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count + 1:

                grid[y + NY][x + NX] = count + 1

                que.append((count + 1, y + NY, x + NX))



print((grid[dh][dw]))
from collections import deque



h, w = (int(x) for x in input().split())

ch, cw = (int(x) - 1 for x in input().split())

dh, dw = (int(x) - 1 for x in input().split())

S = [list(eval(input())) for _ in range(h)]

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

grid[ch][cw] = 0

NX = [1, 0, -1, 0]

NY = [0, 1, 0, -1]

PX = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]

PY = [-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2]



que = deque([(0, ch, cw)])

while que:

    count, y, x = que.popleft()

    if grid[y][x] < count:

        continue

    for i in range(4):

        nx = x + NX[i]

        ny = y + NY[i]

        if 0 <= nx < w and 0 <= ny < h and S[ny][nx] != "#":

            if grid[ny][nx] == -1 or grid[ny][nx] > count:

                grid[ny][nx] = count

                que.appendleft((count, ny, nx))

    for i in range(20):

        px = x + PX[i]

        py = y + PY[i]

        if 0 <= px < w and 0 <= py < h and S[py][px] != "#":

            if grid[py][px] == -1 or grid[py][px] > count + 1:

                grid[py][px] = count + 1

                que.append((count + 1, py, px))



print((grid[dh][dw]))

from collections import deque

h, w = (int(x) for x in input().split())
ch, cw = (int(x) - 1 for x in input().split())
dh, dw = (int(x) - 1 for x in input().split())
S = [list(eval(input())) for _ in range(h)]
grid = [[-1] * w for _ in range(h)]
grid[ch][cw] = 0
walk = [[0, 1], [0, -1], [1, 0], [-1, 0]]
warp = [
    [2, 2],
    [2, 1],
    [2, 0],
    [2, -1],
    [2, -2],
    [1, 2],
    [1, 1],
    [1, -1],
    [1, -2],
    [0, 2],
    [0, -2],
    [-1, 2],
    [-1, 1],
    [-1, -1],
    [-1, -2],
    [-2, 2],
    [-2, 1],
    [-2, 0],
    [-2, -1],
    [-2, -2],
]
que = deque([(0, ch, cw)])
while que:
    count, y, x = que.popleft()
    if grid[y][x] < count:
        continue
    for NY, NX in walk:
        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != "#":
            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count:
                grid[y + NY][x + NX] = count
                que.appendleft((count, y + NY, x + NX))
    for NY, NX in warp:
        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != "#":
            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count + 1:
                grid[y + NY][x + NX] = count + 1
                que.append((count + 1, y + NY, x + NX))
print((grid[dh][dw]))

from collections import deque

h, w = (int(x) for x in input().split())
ch, cw = (int(x) - 1 for x in input().split())
dh, dw = (int(x) - 1 for x in input().split())
S = [list(eval(input())) for _ in range(h)]
grid = [[-1] * w for _ in range(h)]
grid[ch][cw] = 0
NX = [1, 0, -1, 0]
NY = [0, 1, 0, -1]
PX = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]
PY = [-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2]
que = deque([(0, ch, cw)])
while que:
    count, y, x = que.popleft()
    if grid[y][x] < count:
        continue
    for i in range(4):
        nx = x + NX[i]
        ny = y + NY[i]
        if 0 <= nx < w and 0 <= ny < h and S[ny][nx] != "#":
            if grid[ny][nx] == -1 or grid[ny][nx] > count:
                grid[ny][nx] = count
                que.appendleft((count, ny, nx))
    for i in range(20):
        px = x + PX[i]
        py = y + PY[i]
        if 0 <= px < w and 0 <= py < h and S[py][px] != "#":
            if grid[py][px] == -1 or grid[py][px] > count + 1:
                grid[py][px] = count + 1
                que.append((count + 1, py, px))
print((grid[dh][dw]))

-walk = [[0, 1], [0, -1], [1, 0], [-1, 0]] -warp = [ -    [2, 2], -    [2, 1], -    [2, 0], -    [2, -1], -    [2, -2], -    [1, 2], -    [1, 1], -    [1, -1], -    [1, -2], -    [0, 2], -    [0, -2], -    [-1, 2], -    [-1, 1], -    [-1, -1], -    [-1, -2], -    [-2, 2], -    [-2, 1], -    [-2, 0], -    [-2, -1], -    [-2, -2], -] +NX = [1, 0, -1, 0] +NY = [0, 1, 0, -1] +PX = [-2, -2, -2, -2, -2, -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2] +PY = [-2, -1, 0, 1, 2, -2, -1, 1, 2, -2, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2] -    for NY, NX in walk: -        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != "#": -            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count: -                grid[y + NY][x + NX] = count -                que.appendleft((count, y + NY, x + NX)) -    for NY, NX in warp: -        if 0 <= y + NY < h and 0 <= x + NX < w and S[y + NY][x + NX] != "#": -            if grid[y + NY][x + NX] == -1 or grid[y + NY][x + NX] > count + 1: -                grid[y + NY][x + NX] = count + 1 -                que.append((count + 1, y + NY, x + NX)) +    for i in range(4): +        nx = x + NX[i] +        ny = y + NY[i] +        if 0 <= nx < w and 0 <= ny < h and S[ny][nx] != "#": +            if grid[ny][nx] == -1 or grid[ny][nx] > count: +                grid[ny][nx] = count +                que.appendleft((count, ny, nx)) +    for i in range(20): +        px = x + PX[i] +        py = y + PY[i] +        if 0 <= px < w and 0 <= py < h and S[py][px] != "#": +            if grid[py][px] == -1 or grid[py][px] > count + 1: +                grid[py][px] = count + 1 +                que.append((count + 1, py, px))