H, W, ch, cw, dh, dw, *S = open(0).read().split()

H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]]

ch -= 1

cw -= 1

dh -= 1

dw -= 1



stack = [[ch, cw]]

dist = [[None] * W for _ in range(H)]

d = 0

while True:

    stack2 = []

    warp = set()

    while stack:

        x, y = stack.pop()

        if (x, y) == (dh, dw):

            print(d)

            exit()

        if dist[x][y] is not None:

            continue

        dist[x][y] = d

        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

            nx, ny = x + dx, y + dy

            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None:

                if S[nx][ny] == '.':

                    stack += [[nx, ny]]

                else:

                    warp.add((x, y))

    for x, y in warp:

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                nx, ny = x + dx, y + dy

                if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None and S[

                        nx][ny] == '.':

                    stack2 += [[nx, ny]]

    if stack2 == []:

        break

    else:

        d += 1

        stack = stack2

print((-1))

H, W, ch, cw, dh, dw, *S = open(0).read().split()

H, W, ch, cw, dh, dw = [int(_) - 1 for _ in [H, W, ch, cw, dh, dw]]

H += 1

W += 1



stack = [[ch, cw]]

d = 0

INF = 10**10

dists = [[INF] * W for _ in range(H)]

dists[ch][cw] = 0





def valid(x, y):

    return 0 <= x < H and 0 <= y < W





while stack:

    se = set()

    while stack:

        x, y = stack.pop()

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

            nx, ny = x + dx, y + dy

            if valid(nx, ny) and dists[nx][ny] == INF:

                if S[nx][ny] == '.':

                    dists[nx][ny] = d

                    stack += [[nx, ny]]

                else:

                    se.add((x, y))

    stack2 = []

    d += 1

    for x, y in se:

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                nx, ny = x + dx, y + dy

                if valid(nx, ny) and dists[nx][ny] == INF and S[nx][ny] == '.':

                    stack2 += [[nx, ny]]

                    dists[nx][ny] = d

    stack = stack2



ans = dists[dh][dw]

print((ans if ans < INF else -1))

H, W, ch, cw, dh, dw, *S = open(0).read().split()
H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]]
ch -= 1
cw -= 1
dh -= 1
dw -= 1
stack = [[ch, cw]]
dist = [[None] * W for _ in range(H)]
d = 0
while True:
    stack2 = []
    warp = set()
    while stack:
        x, y = stack.pop()
        if (x, y) == (dh, dw):
            print(d)
            exit()
        if dist[x][y] is not None:
            continue
        dist[x][y] = d
        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None:
                if S[nx][ny] == ".":
                    stack += [[nx, ny]]
                else:
                    warp.add((x, y))
    for x, y in warp:
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if (
                    0 <= nx < H
                    and 0 <= ny < W
                    and dist[nx][ny] is None
                    and S[nx][ny] == "."
                ):
                    stack2 += [[nx, ny]]
    if stack2 == []:
        break
    else:
        d += 1
        stack = stack2
print((-1))

H, W, ch, cw, dh, dw, *S = open(0).read().split()
H, W, ch, cw, dh, dw = [int(_) - 1 for _ in [H, W, ch, cw, dh, dw]]
H += 1
W += 1
stack = [[ch, cw]]
d = 0
INF = 10**10
dists = [[INF] * W for _ in range(H)]
dists[ch][cw] = 0


def valid(x, y):
    return 0 <= x < H and 0 <= y < W


while stack:
    se = set()
    while stack:
        x, y = stack.pop()
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if valid(nx, ny) and dists[nx][ny] == INF:
                if S[nx][ny] == ".":
                    dists[nx][ny] = d
                    stack += [[nx, ny]]
                else:
                    se.add((x, y))
    stack2 = []
    d += 1
    for x, y in se:
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if valid(nx, ny) and dists[nx][ny] == INF and S[nx][ny] == ".":
                    stack2 += [[nx, ny]]
                    dists[nx][ny] = d
    stack = stack2
ans = dists[dh][dw]
print((ans if ans < INF else -1))

-H, W, ch, cw, dh, dw = [int(_) for _ in [H, W, ch, cw, dh, dw]] -ch -= 1 -cw -= 1 -dh -= 1 -dw -= 1 +H, W, ch, cw, dh, dw = [int(_) - 1 for _ in [H, W, ch, cw, dh, dw]] +H += 1 +W += 1 -dist = [[None] * W for _ in range(H)] -while True: -    stack2 = [] -    warp = set() +INF = 10**10 +dists = [[INF] * W for _ in range(H)] +dists[ch][cw] = 0 + + +def valid(x, y): +    return 0 <= x < H and 0 <= y < W + + +while stack: +    se = set() -        if (x, y) == (dh, dw): -            print(d) -            exit() -        if dist[x][y] is not None: -            continue -        dist[x][y] = d -        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]: +        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)): -            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None: +            if valid(nx, ny) and dists[nx][ny] == INF: +                    dists[nx][ny] = d -                    warp.add((x, y)) -    for x, y in warp: +                    se.add((x, y)) +    stack2 = [] +    d += 1 +    for x, y in se: -                if ( -                    0 <= nx < H -                    and 0 <= ny < W -                    and dist[nx][ny] is None -                    and S[nx][ny] == "." -                ): +                if valid(nx, ny) and dists[nx][ny] == INF and S[nx][ny] == ".": -    if stack2 == []: -        break -    else: -        d += 1 -        stack = stack2 -print((-1)) +                    dists[nx][ny] = d +    stack = stack2 +ans = dists[dh][dw] +print((ans if ans < INF else -1))