from sys import *

from collections import deque

H, W = list(map(int, stdin.readline().split()))

personY, personX = list(map(int, stdin.readline().split()))

gH, gW = list(map(int, stdin.readline().split()))



grid = [list(stdin.readline().strip()) for x in range(H)]

visited = [ [10**10] * W for i in range(H)]

options = deque([[personY-1, personX-1]])



visited[personY-1][personX-1] = 0

while(options):

    y,x = options.popleft()

    if y+1 < H and  grid[y+1][x] == ".":

        if visited[y+1][x] > visited[y][x]:

            options.appendleft((y+1, x))

            visited[y+1][x] = visited[y][x]



    if  y-1 >= 0 and grid[y-1][x] == ".":

        if visited[y-1][x] > visited[y][x]:

            options.appendleft((y-1, x))

            visited[y-1][x] = visited[y][x]



    if x+1 < W and grid[y][x+1] == ".":

        if visited[y][x+1] > visited[y][x]:

            options.appendleft((y, x+1))

            visited[y][x+1] = visited[y][x]



    if x-1 >= 0 and grid[y][x-1] == ".":

        if visited[y][x-1] > visited[y][x]:

            options.appendleft((y, x-1))

            visited[y][x-1] = visited[y][x]

   #-------------------------------------------

    for Y,X in [(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)]:

       if x+X >= 0 and x+X < W and y+Y >= 0 and y+Y < H and grid[y+Y][x+X] == ".":

           if visited[y+Y][x+X] > visited[y][x] +1:

               options.append((y+Y, x+X))

               visited[y+Y][x+X] = visited[y][x]+1



print((-1 if visited[gH-1][gW-1] == 10**10 else visited[gH-1][gW-1] ))

from sys import *

from collections import deque

H, W = list(map(int, stdin.readline().split()))

personY, personX = list(map(int, stdin.readline().split()))

gH, gW = list(map(int, stdin.readline().split()))



grid = [eval(input()) for x in range(H)]

visited = [[10**10] * W for i in range(H)]

options = deque([[personY-1, personX-1]])



visited[personY-1][personX-1] = 0

while(options):

    y,x = options.popleft()

    if y+1 < H and  grid[y+1][x] == ".":

        if visited[y+1][x] > visited[y][x]:

            options.appendleft((y+1, x))

            visited[y+1][x] = visited[y][x]



    if  y-1 >= 0 and grid[y-1][x] == ".":

        if visited[y-1][x] > visited[y][x]:

            options.appendleft((y-1, x))

            visited[y-1][x] = visited[y][x]



    if x+1 < W and grid[y][x+1] == ".":

        if visited[y][x+1] > visited[y][x]:

            options.appendleft((y, x+1))

            visited[y][x+1] = visited[y][x]



    if x-1 >= 0 and grid[y][x-1] == ".":

        if visited[y][x-1] > visited[y][x]:

            options.appendleft((y, x-1))

            visited[y][x-1] = visited[y][x]

   #-------------------------------------------

    for Y,X in [(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)]:

       if x+X >= 0 and x+X < W and y+Y >= 0 and y+Y < H and grid[y+Y][x+X] == ".":

           if visited[y+Y][x+X] > visited[y][x] +1:

               options.append((y+Y, x+X))

               visited[y+Y][x+X] = visited[y][x]+1



print((-1 if visited[gH-1][gW-1] == 10**10 else visited[gH-1][gW-1] ))

from sys import *
from collections import deque

H, W = list(map(int, stdin.readline().split()))
personY, personX = list(map(int, stdin.readline().split()))
gH, gW = list(map(int, stdin.readline().split()))
grid = [list(stdin.readline().strip()) for x in range(H)]
visited = [[10**10] * W for i in range(H)]
options = deque([[personY - 1, personX - 1]])
visited[personY - 1][personX - 1] = 0
while options:
    y, x = options.popleft()
    if y + 1 < H and grid[y + 1][x] == ".":
        if visited[y + 1][x] > visited[y][x]:
            options.appendleft((y + 1, x))
            visited[y + 1][x] = visited[y][x]
    if y - 1 >= 0 and grid[y - 1][x] == ".":
        if visited[y - 1][x] > visited[y][x]:
            options.appendleft((y - 1, x))
            visited[y - 1][x] = visited[y][x]
    if x + 1 < W and grid[y][x + 1] == ".":
        if visited[y][x + 1] > visited[y][x]:
            options.appendleft((y, x + 1))
            visited[y][x + 1] = visited[y][x]
    if x - 1 >= 0 and grid[y][x - 1] == ".":
        if visited[y][x - 1] > visited[y][x]:
            options.appendleft((y, x - 1))
            visited[y][x - 1] = visited[y][x]
    # -------------------------------------------
    for Y, X in [
        (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),
    ]:
        if (
            x + X >= 0
            and x + X < W
            and y + Y >= 0
            and y + Y < H
            and grid[y + Y][x + X] == "."
        ):
            if visited[y + Y][x + X] > visited[y][x] + 1:
                options.append((y + Y, x + X))
                visited[y + Y][x + X] = visited[y][x] + 1
print((-1 if visited[gH - 1][gW - 1] == 10**10 else visited[gH - 1][gW - 1]))

from sys import *
from collections import deque

H, W = list(map(int, stdin.readline().split()))
personY, personX = list(map(int, stdin.readline().split()))
gH, gW = list(map(int, stdin.readline().split()))
grid = [eval(input()) for x in range(H)]
visited = [[10**10] * W for i in range(H)]
options = deque([[personY - 1, personX - 1]])
visited[personY - 1][personX - 1] = 0
while options:
    y, x = options.popleft()
    if y + 1 < H and grid[y + 1][x] == ".":
        if visited[y + 1][x] > visited[y][x]:
            options.appendleft((y + 1, x))
            visited[y + 1][x] = visited[y][x]
    if y - 1 >= 0 and grid[y - 1][x] == ".":
        if visited[y - 1][x] > visited[y][x]:
            options.appendleft((y - 1, x))
            visited[y - 1][x] = visited[y][x]
    if x + 1 < W and grid[y][x + 1] == ".":
        if visited[y][x + 1] > visited[y][x]:
            options.appendleft((y, x + 1))
            visited[y][x + 1] = visited[y][x]
    if x - 1 >= 0 and grid[y][x - 1] == ".":
        if visited[y][x - 1] > visited[y][x]:
            options.appendleft((y, x - 1))
            visited[y][x - 1] = visited[y][x]
    # -------------------------------------------
    for Y, X in [
        (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),
    ]:
        if (
            x + X >= 0
            and x + X < W
            and y + Y >= 0
            and y + Y < H
            and grid[y + Y][x + X] == "."
        ):
            if visited[y + Y][x + X] > visited[y][x] + 1:
                options.append((y + Y, x + X))
                visited[y + Y][x + X] = visited[y][x] + 1
print((-1 if visited[gH - 1][gW - 1] == 10**10 else visited[gH - 1][gW - 1]))

-grid = [list(stdin.readline().strip()) for x in range(H)] +grid = [eval(input()) for x in range(H)]