from collections import deque



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

cy, cx = list(map(int, input().split()))

sy, sx = list(map(int, input().split()))

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



# initialization

INF = 10**4

arrived = [[INF for _ in range(W)] for __ in range(H)]

paths = deque()

warps = deque()

dx = [1, 0, -1, 0]

dy = [0, -1, 0, 1]

moves = [-2, -1, 0, 1, 2]

arrived[cy-1][cx-1] = 0

paths.append([cx-1, cy-1])

count = 0



# BFS

while(len(paths) > 0):

  while(len(paths) > 0):

    nx, ny = paths.popleft()

    for i in range(4):

      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':

        paths.append([nx+dx[i], ny+dy[i]])

        arrived[ny+dy[i]][nx+dx[i]] = count

    for x in moves:

      for y in moves:

        if 0 <= nx+x < W and 0 <= ny+y < H and arrived[ny+y][nx+x] == INF and tiles[ny+y][nx+x] == '.':

          warps.append([nx+x, ny+y])

  count += 1

  while(len(warps) > 0):

    nx, ny = warps.popleft()

    if arrived[ny][nx] == INF:

      arrived[ny][nx] = count

      paths.append([nx, ny])



if arrived[sy-1][sx-1] != INF:

  print((arrived[sy-1][sx-1]))

else:

  print((-1))
from collections import deque



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

cy, cx = list(map(int, input().split()))

sy, sx = list(map(int, input().split()))

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



# initialization

INF = 10**4

arrived = [[INF for _ in range(W)] for __ in range(H)]

paths = deque()

warps = deque()

dx = [1, 0, -1, 0]

dy = [0, -1, 0, 1]

moves = [-2, -1, 0, 1, 2]

arrived[cy-1][cx-1] = 0

paths.append([cx-1, cy-1])

count = 0



# BFS

while(len(paths) > 0):

  while(len(paths) > 0):

    nx, ny = paths.popleft()

    if ny == sy-1 and nx == sx-1:

      print(count)

      exit()

    for i in range(4):

      if 0 <= nx+dx[i] < W and 0 <= ny+dy[i] < H and arrived[ny+dy[i]][nx+dx[i]] > count and tiles[ny+dy[i]][nx+dx[i]] == '.':

        paths.append([nx+dx[i], ny+dy[i]])

        arrived[ny+dy[i]][nx+dx[i]] = count

    for x in moves:

      for y in moves:

        if 0 <= nx+x < W and 0 <= ny+y < H and arrived[ny+y][nx+x] == INF and tiles[ny+y][nx+x] == '.':

          warps.append([nx+x, ny+y])

  count += 1

  while(len(warps) > 0):

    nx, ny = warps.popleft()

    if arrived[ny][nx] == INF:

      arrived[ny][nx] = count

      paths.append([nx, ny])

print((-1))
from collections import deque

H, W = list(map(int, input().split()))
cy, cx = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
tiles = [list(eval(input())) for _ in range(H)]
# initialization
INF = 10**4
arrived = [[INF for _ in range(W)] for __ in range(H)]
paths = deque()
warps = deque()
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
moves = [-2, -1, 0, 1, 2]
arrived[cy - 1][cx - 1] = 0
paths.append([cx - 1, cy - 1])
count = 0
# BFS
while len(paths) > 0:
    while len(paths) > 0:
        nx, ny = paths.popleft()
        for i in range(4):
            if (
                0 <= nx + dx[i] < W
                and 0 <= ny + dy[i] < H
                and arrived[ny + dy[i]][nx + dx[i]] > count
                and tiles[ny + dy[i]][nx + dx[i]] == "."
            ):
                paths.append([nx + dx[i], ny + dy[i]])
                arrived[ny + dy[i]][nx + dx[i]] = count
        for x in moves:
            for y in moves:
                if (
                    0 <= nx + x < W
                    and 0 <= ny + y < H
                    and arrived[ny + y][nx + x] == INF
                    and tiles[ny + y][nx + x] == "."
                ):
                    warps.append([nx + x, ny + y])
    count += 1
    while len(warps) > 0:
        nx, ny = warps.popleft()
        if arrived[ny][nx] == INF:
            arrived[ny][nx] = count
            paths.append([nx, ny])
if arrived[sy - 1][sx - 1] != INF:
    print((arrived[sy - 1][sx - 1]))
else:
    print((-1))

from collections import deque

H, W = list(map(int, input().split()))
cy, cx = list(map(int, input().split()))
sy, sx = list(map(int, input().split()))
tiles = [list(eval(input())) for _ in range(H)]
# initialization
INF = 10**4
arrived = [[INF for _ in range(W)] for __ in range(H)]
paths = deque()
warps = deque()
dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]
moves = [-2, -1, 0, 1, 2]
arrived[cy - 1][cx - 1] = 0
paths.append([cx - 1, cy - 1])
count = 0
# BFS
while len(paths) > 0:
    while len(paths) > 0:
        nx, ny = paths.popleft()
        if ny == sy - 1 and nx == sx - 1:
            print(count)
            exit()
        for i in range(4):
            if (
                0 <= nx + dx[i] < W
                and 0 <= ny + dy[i] < H
                and arrived[ny + dy[i]][nx + dx[i]] > count
                and tiles[ny + dy[i]][nx + dx[i]] == "."
            ):
                paths.append([nx + dx[i], ny + dy[i]])
                arrived[ny + dy[i]][nx + dx[i]] = count
        for x in moves:
            for y in moves:
                if (
                    0 <= nx + x < W
                    and 0 <= ny + y < H
                    and arrived[ny + y][nx + x] == INF
                    and tiles[ny + y][nx + x] == "."
                ):
                    warps.append([nx + x, ny + y])
    count += 1
    while len(warps) > 0:
        nx, ny = warps.popleft()
        if arrived[ny][nx] == INF:
            arrived[ny][nx] = count
            paths.append([nx, ny])
print((-1))

+        if ny == sy - 1 and nx == sx - 1: +            print(count) +            exit() -if arrived[sy - 1][sx - 1] != INF: -    print((arrived[sy - 1][sx - 1])) -else: -    print((-1)) +print((-1))