import sys

from collections import deque





input = lambda: sys.stdin.readline().rstrip()





if __name__ == "__main__":

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

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

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

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

    INF = 10**8

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

    mc[ch][cw] = 0

    walk_que = deque()

    walk_que.append((ch, cw, 0))

    while walk_que:

        y, x, w = walk_que.popleft()

        for ny, nx in [(y+1, x), (y-1, x), (y, x+1), (y, x-1)]:

            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w and maze[ny][nx] == ".":

                mc[ny][nx] = w

                walk_que.appendleft((ny, nx, w))

        for dy in range(-2, 3):

            ny = y + dy

            for dx in range(-2, 3):

                nx = x + dx

                if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w+1 and maze[ny][nx] == ".":

                    mc[ny][nx] = w + 1

                    walk_que.append((ny, nx, w+1))

    if mc[dh][dw] == INF:

        print((-1))

    else:

        print((mc[dh][dw]))

import sys

from collections import deque





input = lambda: sys.stdin.readline().rstrip()





if __name__ == "__main__":

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

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

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

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

    INF = 10**8

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

    mc[ch][cw] = 0

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

    warp = [(y, x) for x in range(-2, 3) for y in range(-2, 3) if abs(y) + abs(x) > 1]

    que = deque()

    que.append((ch, cw, 0))

    while que:

        y, x, w = que.popleft()

        for dy, dx in walk:

            ny = y + dy

            nx = x + dx

            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w and maze[ny][nx] == ".":

                mc[ny][nx] = w

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

        for dy, dx in warp:

            ny = y + dy

            nx = x + dx

            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w+1 and maze[ny][nx] == ".":

                mc[ny][nx] = w + 1

                que.append((ny, nx, w+1))

    if mc[dh][dw] == INF:

        print((-1))

    else:

        print((mc[dh][dw]))

import sys
from collections import deque

input = lambda: sys.stdin.readline().rstrip()
if __name__ == "__main__":
    H, W = list(map(int, input().split()))
    ch, cw = [int(x) - 1 for x in input().split()]
    dh, dw = [int(x) - 1 for x in input().split()]
    maze = [list(eval(input())) for _ in range(H)]
    INF = 10**8
    mc = [[INF] * W for _ in range(H)]
    mc[ch][cw] = 0
    walk_que = deque()
    walk_que.append((ch, cw, 0))
    while walk_que:
        y, x, w = walk_que.popleft()
        for ny, nx in [(y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)]:
            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w and maze[ny][nx] == ".":
                mc[ny][nx] = w
                walk_que.appendleft((ny, nx, w))
        for dy in range(-2, 3):
            ny = y + dy
            for dx in range(-2, 3):
                nx = x + dx
                if (
                    H > ny >= 0
                    and W > nx >= 0
                    and mc[ny][nx] > w + 1
                    and maze[ny][nx] == "."
                ):
                    mc[ny][nx] = w + 1
                    walk_que.append((ny, nx, w + 1))
    if mc[dh][dw] == INF:
        print((-1))
    else:
        print((mc[dh][dw]))

import sys
from collections import deque

input = lambda: sys.stdin.readline().rstrip()
if __name__ == "__main__":
    H, W = list(map(int, input().split()))
    ch, cw = [int(x) - 1 for x in input().split()]
    dh, dw = [int(x) - 1 for x in input().split()]
    maze = [list(eval(input())) for _ in range(H)]
    INF = 10**8
    mc = [[INF] * W for _ in range(H)]
    mc[ch][cw] = 0
    walk = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    warp = [(y, x) for x in range(-2, 3) for y in range(-2, 3) if abs(y) + abs(x) > 1]
    que = deque()
    que.append((ch, cw, 0))
    while que:
        y, x, w = que.popleft()
        for dy, dx in walk:
            ny = y + dy
            nx = x + dx
            if H > ny >= 0 and W > nx >= 0 and mc[ny][nx] > w and maze[ny][nx] == ".":
                mc[ny][nx] = w
                que.appendleft((ny, nx, w))
        for dy, dx in warp:
            ny = y + dy
            nx = x + dx
            if (
                H > ny >= 0
                and W > nx >= 0
                and mc[ny][nx] > w + 1
                and maze[ny][nx] == "."
            ):
                mc[ny][nx] = w + 1
                que.append((ny, nx, w + 1))
    if mc[dh][dw] == INF:
        print((-1))
    else:
        print((mc[dh][dw]))

-    walk_que = deque() -    walk_que.append((ch, cw, 0)) -    while walk_que: -        y, x, w = walk_que.popleft() -        for ny, nx in [(y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)]: +    walk = [(1, 0), (0, 1), (-1, 0), (0, -1)] +    warp = [(y, x) for x in range(-2, 3) for y in range(-2, 3) if abs(y) + abs(x) > 1] +    que = deque() +    que.append((ch, cw, 0)) +    while que: +        y, x, w = que.popleft() +        for dy, dx in walk: +            ny = y + dy +            nx = x + dx -                walk_que.appendleft((ny, nx, w)) -        for dy in range(-2, 3): +                que.appendleft((ny, nx, w)) +        for dy, dx in warp: -            for dx in range(-2, 3): -                nx = x + dx -                if ( -                    H > ny >= 0 -                    and W > nx >= 0 -                    and mc[ny][nx] > w + 1 -                    and maze[ny][nx] == "." -                ): -                    mc[ny][nx] = w + 1 -                    walk_que.append((ny, nx, w + 1)) +            nx = x + dx +            if ( +                H > ny >= 0 +                and W > nx >= 0 +                and mc[ny][nx] > w + 1 +                and maze[ny][nx] == "." +            ): +                mc[ny][nx] = w + 1 +                que.append((ny, nx, w + 1))