dhw = [(1,0),(0,-1),(0,1),(-1,0),

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



H, W = list(map(int, input().split()))

sh, sw = [int(x)-1 for x in input().split()]

eh, ew = [int(x)-1 for x in input().split()]

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



dst = [[-1]*W for _ in range(H)]



from collections import deque

dq = deque([(sh, sw, 0)])

while dq:

    h, w, d = dq.popleft()

    if h == eh and w == ew:

        dst[h][w] = d

        break

    if 0 <= h < H and 0 <= w < W and dst[h][w] < 0 and S[h][w] != '#':

        dst[h][w] = d

        for i in range(4): dq.appendleft((h + dhw[i][0], w + dhw[i][1], d))

        for i in range(4, 24): dq.append((h + dhw[i][0], w + dhw[i][1], d+1))



print((dst[eh][ew]))

dhw = [(1,0),(0,-1),(0,1),(-1,0),

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



H, W = list(map(int, input().split()))

sh, sw = [int(x)-1 for x in input().split()]

eh, ew = [int(x)-1 for x in input().split()]

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



dst = [[-1]*W for _ in range(H)]



from collections import deque

dq = deque([(sh, sw, 0)])

while dq:

    h, w, d = dq.popleft()

    if h == eh and w == ew:

        dst[h][w] = d

        break



    if dst[h][w] >= 0 or S[h][w] == '#': continue

    if dst[h][w] == -1 or d < dst[h][w]: dst[h][w] = d



    for i in range(4):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] >= 0: continue

        dq.appendleft((nh, nw, d))



    for i in range(4, 24):

        nh = h + dhw[i][0]; nw = w + dhw[i][1]

        if nh < 0 or H <= nh : continue

        if nw < 0 or W <= nw : continue

        if S[nh][nw] == '#': continue

        if dst[nh][nw] >= 0: continue

        dq.append((nh, nw, d+1))



print((dst[eh][ew]))

dhw = [
    (1, 0),
    (0, -1),
    (0, 1),
    (-1, 0),
    (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),
]
H, W = list(map(int, input().split()))
sh, sw = [int(x) - 1 for x in input().split()]
eh, ew = [int(x) - 1 for x in input().split()]
S = [eval(input()) for _ in range(H)]
dst = [[-1] * W for _ in range(H)]
from collections import deque

dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if h == eh and w == ew:
        dst[h][w] = d
        break
    if 0 <= h < H and 0 <= w < W and dst[h][w] < 0 and S[h][w] != "#":
        dst[h][w] = d
        for i in range(4):
            dq.appendleft((h + dhw[i][0], w + dhw[i][1], d))
        for i in range(4, 24):
            dq.append((h + dhw[i][0], w + dhw[i][1], d + 1))
print((dst[eh][ew]))

dhw = [
    (1, 0),
    (0, -1),
    (0, 1),
    (-1, 0),
    (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),
]
H, W = list(map(int, input().split()))
sh, sw = [int(x) - 1 for x in input().split()]
eh, ew = [int(x) - 1 for x in input().split()]
S = [eval(input()) for _ in range(H)]
dst = [[-1] * W for _ in range(H)]
from collections import deque

dq = deque([(sh, sw, 0)])
while dq:
    h, w, d = dq.popleft()
    if h == eh and w == ew:
        dst[h][w] = d
        break
    if dst[h][w] >= 0 or S[h][w] == "#":
        continue
    if dst[h][w] == -1 or d < dst[h][w]:
        dst[h][w] = d
    for i in range(4):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] >= 0:
            continue
        dq.appendleft((nh, nw, d))
    for i in range(4, 24):
        nh = h + dhw[i][0]
        nw = w + dhw[i][1]
        if nh < 0 or H <= nh:
            continue
        if nw < 0 or W <= nw:
            continue
        if S[nh][nw] == "#":
            continue
        if dst[nh][nw] >= 0:
            continue
        dq.append((nh, nw, d + 1))
print((dst[eh][ew]))

-    if 0 <= h < H and 0 <= w < W and dst[h][w] < 0 and S[h][w] != "#": +    if dst[h][w] >= 0 or S[h][w] == "#": +        continue +    if dst[h][w] == -1 or d < dst[h][w]: -        for i in range(4): -            dq.appendleft((h + dhw[i][0], w + dhw[i][1], d)) -        for i in range(4, 24): -            dq.append((h + dhw[i][0], w + dhw[i][1], d + 1)) +    for i in range(4): +        nh = h + dhw[i][0] +        nw = w + dhw[i][1] +        if nh < 0 or H <= nh: +            continue +        if nw < 0 or W <= nw: +            continue +        if S[nh][nw] == "#": +            continue +        if dst[nh][nw] >= 0: +            continue +        dq.appendleft((nh, nw, d)) +    for i in range(4, 24): +        nh = h + dhw[i][0] +        nw = w + dhw[i][1] +        if nh < 0 or H <= nh: +            continue +        if nw < 0 or W <= nw: +            continue +        if S[nh][nw] == "#": +            continue +        if dst[nh][nw] >= 0: +            continue +        dq.append((nh, nw, d + 1))