import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7



from collections import deque

import itertools



h,w = list(map(int,readline().split()))

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

gx,gy = list(map(int,readline().split()))

cx -= 1

cy -= 1

grid = [readline().rstrip() for i in range(h)]



dist = [[INF]*w for i in range(h)]

dist[cx][cy] = 0



l2 = list(itertools.product([-2,-1,0,1,2],repeat = 2))

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

q1 = deque([(cx,cy)])



while q1:

    if q1:

        x,y = q1.popleft()

        for dy,dx in l1:

            ny,nx = y+dy, x+dx

            if not (0 <= ny < w and 0 <= nx < h):

                continue

            if grid[nx][ny] == '#':

                continue

            if dist[nx][ny] > dist[x][y]:

                dist[nx][ny] = dist[x][y]

                q1.appendleft((nx,ny))

        for dy,dx in l2:

            ny,nx = y+dy, x+dx

            if not (0 <= ny < w and 0 <= nx < h):

                continue

            if grid[nx][ny] == '#':

                continue

            if dist[nx][ny] > dist[x][y]+1:

                dist[nx][ny] = dist[x][y]+1

                q1.append((nx,ny))



print((dist[gx-1][gy-1] if dist[gx-1][gy-1] != INF else -1))





import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7



from collections import deque

import itertools



h,w = list(map(int,readline().split()))

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

gx,gy = list(map(int,readline().split()))

cx -= 1

cy -= 1

grid = [readline().rstrip() for i in range(h)]



dist = [[INF]*w for i in range(h)]

dist[cx][cy] = 0



l2 = list(itertools.product([-2,-1,0,1,2],repeat = 2))

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

q1 = deque([(cx,cy)])

q2 = deque([(cx,cy)])



while q1 or q2:

    if q1:

        x,y = q1.popleft()

        for dy,dx in l1:

            ny,nx = y+dy, x+dx

            if not (0 <= ny < w and 0 <= nx < h):

                continue

            if grid[nx][ny] == '#':

                continue

            if dist[nx][ny] > dist[x][y]:

                dist[nx][ny] = dist[x][y]

                q1.append((nx,ny))

                q2.append((nx,ny))

    else:

        x,y = q2.popleft()

        for dy,dx in l2:

            ny,nx = y+dy, x+dx

            if not (0 <= ny < w and 0 <= nx < h):

                continue

            if grid[nx][ny] == '#':

                continue

            if dist[nx][ny] > dist[x][y]+1:

                dist[nx][ny] = dist[x][y]+1

                q1.append((nx,ny))

                q2.append((nx,ny))



print((dist[gx-1][gy-1] if dist[gx-1][gy-1] != INF else -1))
import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
from collections import deque
import itertools

h, w = list(map(int, readline().split()))
cx, cy = list(map(int, readline().split()))
gx, gy = list(map(int, readline().split()))
cx -= 1
cy -= 1
grid = [readline().rstrip() for i in range(h)]
dist = [[INF] * w for i in range(h)]
dist[cx][cy] = 0
l2 = list(itertools.product([-2, -1, 0, 1, 2], repeat=2))
l1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
q1 = deque([(cx, cy)])
while q1:
    if q1:
        x, y = q1.popleft()
        for dy, dx in l1:
            ny, nx = y + dy, x + dx
            if not (0 <= ny < w and 0 <= nx < h):
                continue
            if grid[nx][ny] == "#":
                continue
            if dist[nx][ny] > dist[x][y]:
                dist[nx][ny] = dist[x][y]
                q1.appendleft((nx, ny))
        for dy, dx in l2:
            ny, nx = y + dy, x + dx
            if not (0 <= ny < w and 0 <= nx < h):
                continue
            if grid[nx][ny] == "#":
                continue
            if dist[nx][ny] > dist[x][y] + 1:
                dist[nx][ny] = dist[x][y] + 1
                q1.append((nx, ny))
print((dist[gx - 1][gy - 1] if dist[gx - 1][gy - 1] != INF else -1))

import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
from collections import deque
import itertools

h, w = list(map(int, readline().split()))
cx, cy = list(map(int, readline().split()))
gx, gy = list(map(int, readline().split()))
cx -= 1
cy -= 1
grid = [readline().rstrip() for i in range(h)]
dist = [[INF] * w for i in range(h)]
dist[cx][cy] = 0
l2 = list(itertools.product([-2, -1, 0, 1, 2], repeat=2))
l1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
q1 = deque([(cx, cy)])
q2 = deque([(cx, cy)])
while q1 or q2:
    if q1:
        x, y = q1.popleft()
        for dy, dx in l1:
            ny, nx = y + dy, x + dx
            if not (0 <= ny < w and 0 <= nx < h):
                continue
            if grid[nx][ny] == "#":
                continue
            if dist[nx][ny] > dist[x][y]:
                dist[nx][ny] = dist[x][y]
                q1.append((nx, ny))
                q2.append((nx, ny))
    else:
        x, y = q2.popleft()
        for dy, dx in l2:
            ny, nx = y + dy, x + dx
            if not (0 <= ny < w and 0 <= nx < h):
                continue
            if grid[nx][ny] == "#":
                continue
            if dist[nx][ny] > dist[x][y] + 1:
                dist[nx][ny] = dist[x][y] + 1
                q1.append((nx, ny))
                q2.append((nx, ny))
print((dist[gx - 1][gy - 1] if dist[gx - 1][gy - 1] != INF else -1))

-while q1: +q2 = deque([(cx, cy)]) +while q1 or q2: -                q1.appendleft((nx, ny)) +                q1.append((nx, ny)) +                q2.append((nx, ny)) +    else: +        x, y = q2.popleft() +                q2.append((nx, ny))