import collections, itertools

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 += 3

W += 3

S = ['#' * W] + ['#' * W] + ['##' + s + '##'

                             for s in S] + ['#' * W] + ['#' * W]

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

Q = collections.deque([(0, ch, cw)])

while Q:

    d, ox, oy = Q.popleft()

    if Dist[ox][oy] is not None:

        continue

    stack = [[ox, oy]]

    warp = set()

    while stack:

        x, y = stack.pop()

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

            print(d)

            exit()

        Dist[x][y] = d

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

            nx, ny = x + dx, y + dy

            if Dist[nx][ny] is None:

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

                    stack += [[nx, ny]]

                else:

                    warp.add((x, y))

    for x, y in warp:

        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2):

            nx, ny = x + dx, y + dy

            if Dist[nx][ny] is None and S[nx][ny] == '.':

                Q += [(d + 1, nx, ny)]

print((-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))

import collections, itertools

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 += 3
W += 3
S = ["#" * W] + ["#" * W] + ["##" + s + "##" for s in S] + ["#" * W] + ["#" * W]
Dist = [[None] * W for _ in range(H)]
Q = collections.deque([(0, ch, cw)])
while Q:
    d, ox, oy = Q.popleft()
    if Dist[ox][oy] is not None:
        continue
    stack = [[ox, oy]]
    warp = set()
    while stack:
        x, y = stack.pop()
        if (x, y) == (dh, dw):
            print(d)
            exit()
        Dist[x][y] = d
        for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
            nx, ny = x + dx, y + dy
            if Dist[nx][ny] is None:
                if S[nx][ny] == ".":
                    stack += [[nx, ny]]
                else:
                    warp.add((x, y))
    for x, y in warp:
        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2):
            nx, ny = x + dx, y + dy
            if Dist[nx][ny] is None and S[nx][ny] == ".":
                Q += [(d + 1, nx, ny)]
print((-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))

-import collections, itertools - -H, W, ch, cw, dh, dw = [int(_) + 1 for _ in [H, W, ch, cw, dh, dw]] -H += 3 -W += 3 -S = ["#" * W] + ["#" * W] + ["##" + s + "##" for s in S] + ["#" * W] + ["#" * W] -Dist = [[None] * W for _ in range(H)] -Q = collections.deque([(0, ch, cw)]) -while Q: -    d, ox, oy = Q.popleft() -    if Dist[ox][oy] is not None: -        continue -    stack = [[ox, oy]] +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 = [] -        Dist[x][y] = d -        for dx, dy in [[-1, 0], [1, 0], [0, -1], [0, 1]]: +        if dist[x][y] is not None: +            continue +        dist[x][y] = d +        for dx, dy in [[-1, 0], [1, 0], [0, 1], [0, -1]]: -            if Dist[nx][ny] is None: +            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] is None: -        for dx, dy in itertools.product(list(range(-2, 3)), repeat=2): -            nx, ny = x + dx, y + dy -            if Dist[nx][ny] is None and S[nx][ny] == ".": -                Q += [(d + 1, nx, ny)] +        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