A maze is composed of a grid of H \times W squares - H vertical, W horizontal.
The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..
There is a magician in (C_h,C_w). He can do the following two kinds of moves:
 - Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.
 - Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.
In either case, he cannot go out of the maze.
At least how many times does he need to use the magic to reach (D_h, D_w)?

-----Constraints-----
 - 1 \leq H,W \leq 10^3
 - 1 \leq C_h,D_h \leq H
 - 1 \leq C_w,D_w \leq W
 - S_{ij} is # or ..
 - S_{C_h C_w} and S_{D_h D_w} are ..
 - (C_h,C_w) \neq (D_h,D_w)

-----Input-----
Input is given from Standard Input in the following format:
H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

-----Output-----
Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

-----Sample Input-----
4 4
1 1
4 4
..#.
..#.
.#..
.#..

-----Sample Output-----
1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.
Note that he cannot walk diagonally.

Difficulty: interview

Starter code:

Solutions:
# import numpy as np
from collections import deque

def solve(): #H, W, CH, CW, DH, DW, S):
	H,W=list(map(int,input().split()))
	CH,CW=list(map(int,input().split()))
	DH,DW=list(map(int,input().split()))
	S=[input() for _ in range(H)]

	# 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
	S = ['##{}##'.format(row) for row in S]
	S.insert(0, '##{}##'.format('#' * W))
	S.insert(0, '##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))


	MAX_COST = 10 ** 9
	Cost=[ [ MAX_COST for _ in range(W+4)] for _ in range(H+4)]
	# Cost = np.full((H,W),MAX_COST)

	# print(S)

	ans = -1

	cost0 = deque()

	cost0.append((CH+1,CW+1,0))
	Cost[CH+1][CW+1] = 0;

	# used = set()
	walk=[(1,0),(0,1),(-1,0),(0,-1)]
	warp = [ (i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0, 0)] + walk ]
	# print(warp)

	cost1 = deque()

	while cost0:
		h,w,c=cost0.popleft()
		cost1.append((h,w,c))

		for i,j in walk:
			dh = h+i
			dw = w+j
			if S[dh][dw] == '.' and c < Cost[dh][dw]:
				Cost[dh][dw] = c
				# print("updl ", i,j,dh,dw,c)
				cost0.appendleft((dh,dw,Cost[dh][dw]))

		if len(cost0) == 0:

			while cost1:
				h,w,c=cost1.popleft()
				# print(h,w,c)
				for i,j in warp:
					dh = h+i
					dw = w+j
					# print(i,j)
					if S[dh][dw] == '.' and c + 1 < Cost[dh][dw]:
						Cost[dh][dw] = c + 1
						cost0.append((dh,dw,Cost[dh][dw]))


	if Cost[DH+1][DW+1] == MAX_COST:
		print((-1))
	else:
		print((Cost[DH+1][DW+1]))


def __starting_point():

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

	solve() #H, W, CH, CW, DH, DW, S)

__starting_point()

from collections import deque


def main():

    def bfs(h1, w1, dp):
        """
        -1 - unsearched
        -2 - candidate for warp
        -3 - wall '#'
        """
        queue = deque()
        queue_warp = deque()
        queue.append([h1, w1])
        now = 0

        while queue:
            h1, w1 = queue.popleft()
            fl_up = False
            fl_left = False
            fl_down = False
            fl_right = False

            if h1 > 0:
                if dp[h1-1][w1] == -1 or dp[h1-1][w1] == -2:
                    if m[h1-1][w1] == '.':
                        dp[h1-1][w1] = now
                        queue.append([h1-1, w1])
                    else:
                        dp[h1-1][w1] = -3
                if dp[h1-1][w1] == -3:
                    fl_up = True
                    if h1 > 1:
                        if dp[h1-2][w1] == -1:
                            if m[h1-2][w1] == '.':
                                dp[h1-2][w1] = -2
                                queue_warp.append([h1-2, w1])
                            else:
                                dp[h1-2][w1] = -3
            
            if w1 > 0:
                if dp[h1][w1-1] == -1  or dp[h1][w1-1] == -2:
                    if m[h1][w1-1] == '.':
                        dp[h1][w1-1] = now
                        queue.append([h1, w1-1])
                    else:
                        dp[h1][w1-1] = -3
                if dp[h1][w1-1] == -3:
                    fl_left = True
                    if w1 > 1:
                        if dp[h1][w1-2] == -1:
                            if m[h1][w1-2] == '.':
                                dp[h1][w1-2] = -2
                                queue_warp.append([h1, w1-2])
                            else:
                                dp[h1][w1-2] = -3
            
            if h1 < h - 1:
                if dp[h1+1][w1] == -1 or dp[h1+1][w1] == -2:
                    if m[h1+1][w1] == '.':
                        dp[h1+1][w1] = now
                        queue.append([h1+1, w1])
                    else:
                        dp[h1+1][w1] = -3
                if dp[h1+1][w1] == -3:
                    fl_down = True
                    if h1 < h - 2:
                        if dp[h1+2][w1] == -1:
                            if m[h1+2][w1] == '.':
                                dp[h1+2][w1] = -2
                                queue_warp.append([h1+2, w1])
                            else:
                                dp[h1+2][w1] = -3
            
            if w1 < w - 1:
                if dp[h1][w1+1] == -1 or dp[h1][w1+1] == -2:
                    if m[h1][w1+1] == '.':
                        dp[h1][w1+1] = now
                        queue.append([h1, w1+1])
                    else:
                        dp[h1][w1+1] = -3
                if dp[h1][w1+1] == -3:
                    fl_right = True
                    if w1 < w - 2:
                        if dp[h1][w1+2] == -1:
                            if m[h1][w1+2] == '.':
                                dp[h1][w1+2] = -2
                                queue_warp.append([h1, w1+2])
                            else:
                                dp[h1][w1+2] = -3
            
            if fl_up and fl_left:
                for i in range(max(0, h1-2), h1):
                    for j in range(max(0, w1-2), w1):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if fl_left and fl_down:
                for i in range(h1+1, min(h, h1+3)):
                    for j in range(max(0, w1-2), w1):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3
            
            if fl_down and fl_right:
                for i in range(h1+1, min(h, h1+3)):
                    for j in range(w1+1, min(w, w1+3)):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if fl_right and fl_up:
                for i in range(max(0, h1-2), h1):
                    for j in range(w1+1, min(w, w1+3)):
                        if dp[i][j] == -1:
                            if m[i][j] == '.':
                                dp[i][j] = -2
                                queue_warp.append([i, j])
                            else:
                                dp[i][j] = -3

            if not queue:
                now += 1
                while queue_warp:
                    h1, w1 = queue_warp.popleft()
                    if dp[h1][w1] == -2:
                        dp[h1][w1] = now
                        queue.append([h1, w1])
            
            if dp[dh-1][dw-1] >= 0:
                return dp[dh-1][dw-1]
        
        return dp[dh-1][dw-1]

    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))

    m = [[] for i in range(h)]
    dp = [[-1]*w for i in range(h)]
    dp[ch-1][cw-1] = 0
    
    for i in range(h):
        m[i] = input()
    
    ans = bfs(ch-1, cw-1, dp)

    print(ans)

main()


import sys
input = sys.stdin.readline


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))
    
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    
    
    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for i in range(h):
        s.append("##" + input()[:-1] + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = [(ch, cw)]
    one_time = []
    
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print((ans[dh][dw]))
    
    
    
def __starting_point():
    main()

__starting_point()

from collections import deque
 
def main():
  h,w=map(int,input().split())
  c1,c2=map(lambda x:int(x)+1,input().split())
  d1,d2=map(lambda x:int(x)+1,input().split())
  work1=[(1,0),(-1,0),(0,1),(0,-1)]
  work2=[(i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0,0)]+work1]
  memo=["#"*(w+4)]*2+["##"+input()+"##" for _ in range(h)]+["#"*(w+4)]*2
  dist=[[-1]*(w+4) for _ in range(h+4)]
  for i in range(h+4):
    for j in range(w+4):
      if memo[i][j]=="#":
        dist[i][j]=float("inf")
  que1=deque([(c1,c2)])
  que2=deque([])
  dist[c1][c2]=0
  while que1:
    y,x=que1.popleft()
    score=dist[y][x]
    que2.append((y,x))
    for py,px in work1:
      ny,nx=y+py,x+px
      if dist[ny][nx]==-1:
        dist[ny][nx]=score
        que1.append((ny,nx))
    if len(que1)==0:
      while que2:
        y,x=que2.popleft()
        for py,px in work2:
          ny,nx=y+py,x+px
          if dist[ny][nx]==-1:
            dist[ny][nx]=score+1
            que1.append((ny,nx))
  print(dist[d1][d2])

def __starting_point():
    main()
__starting_point()

import sys
input = sys.stdin.readline

def solve():
    dxys = [(-1,0), (1,0), (0,-1), (0,1)]

    H, W = list(map(int, input().split()))
    Ch, Cw = list(map(int, input().split()))
    Dh, Dw = list(map(int, input().split()))
    Sss = ['#'*(W+4)] + ['#'*(W+4)] + ['##'+input().rstrip()+'##' for _ in range(H)] + ['#'*(W+4)] + ['#'*(W+4)]

    Ch, Cw, Dh, Dw = Ch+1, Cw+1, Dh+1, Dw+1

    usedss = [[0]*(W+4) for _ in range(H+4)]
    for x in range(H+4):
        for y in range(W+4):
            if Sss[x][y] == '#':
                usedss[x][y] = 1

    ans = 0
    vs = set([(Ch, Cw)])
    vNews = set([(Ch, Cw)])
    usedss[Ch][Cw] = 1
    while True:
        while vs:
            v2s = set()
            for x, y in vs:
                for dx, dy in dxys:
                    x2, y2 = x+dx, y+dy
                    if usedss[x2][y2]: continue
                    v2s.add((x2, y2))
                    vNews.add((x2, y2))
                    usedss[x2][y2] = 1
            vs = v2s

        if usedss[Dh][Dw]:
            print(ans)
            break

        if not vNews:
            print((-1))
            break

        vs = set()
        for x, y in vNews:
            for dx in range(-2, 3):
                for dy in range(-2, 3):
                    x2, y2 = x+dx, y+dy
                    if usedss[x2][y2]: continue
                    vs.add((x2, y2))
                    usedss[x2][y2] = 1

        ans += 1
        vNews = set(vs)


solve()


from collections import deque

def main():
    h, w = list(map(int, input().split()))
    start_h, start_w = [int(x)+1 for x in input().split()]
    goal_h, goal_w = [int(x)+1 for x in input().split()]

    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for _ in range(h):
        s.append("##" + input() + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))

    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[start_h][start_w] = 0

    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),(-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),(0, -2), (0, -1), (0, 0), (0, 1), (0, 2),(1, -2), (1, -1), (1, 0), (1, 1), (1, 2),(2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

    yet = deque([(start_h, start_w)])
    done = deque()

    while yet:
        x1, y1 = yet.popleft()
        done.append((x1, y1))
        for (p, q) in move1:
            v1, v2 = x1+p, y1+q
            if ans[v1][v2] == -1: #"."でまだ辿り着いていないマス
                yet.append((v1, v2))
                ans[v1][v2] = ans[x1][y1]

        if len(yet) == 0:
            while done:
                x2, y2 = done.popleft()
                for (p, q) in move2:
                    i, j = x2+p, y2+q
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        yet.append((i, j))

    print((ans[goal_h][goal_w]))

def __starting_point():
    main()

__starting_point()

from collections import deque
 
def main():
  h,w=map(int,input().split())
  c1,c2=map(lambda x:int(x)+1,input().split())
  d1,d2=map(lambda x:int(x)+1,input().split())
  work1=[(1,0),(-1,0),(0,1),(0,-1)]
  work2=[(i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0,0)]+work1]
  memo=["#"*(w+4)]*2+["##"+input()+"##" for _ in range(h)]+["#"*(w+4)]*2
  dist=[[-1]*(w+4) for _ in range(h+4)]
  for i in range(h+4):
    for j in range(w+4):
      if memo[i][j]=="#":
        dist[i][j]=float("inf")
  que1=deque([(c1,c2)])
  que2=deque([])
  dist[c1][c2]=0
  while que1:
    y,x=que1.popleft()
    score=dist[y][x]
    que2.append((y,x))
    for py,px in work1:
      ny,nx=y+py,x+px
      if dist[ny][nx]==-1:
        dist[ny][nx]=score
        que1.append((ny,nx))
    if not que1:
      while que2:
        y,x=que2.popleft()
        for py,px in work2:
          ny,nx=y+py,x+px
          if dist[ny][nx]==-1:
            dist[ny][nx]=score+1
            que1.append((ny,nx))
  print(dist[d1][d2])
 
def __starting_point():
    main()
__starting_point()

from collections import deque


def main():
    h, w = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    base = ["#" *(w+4)]
    base.append("#" * (w+4))
    for n in range(h):
        base.append("##" + input() + "##")
    base.append("#" * (w+4))
    base.append("#" * (w+4))
    ans = [[-1] * (w+4) for _ in range(h+4)]
    for j in range(w+4):
        for i in range(h+4):
            if base[i][j] == "#":
                ans[i][j] = -2
    ch+=1; cw+=1
    dh+=1; dw+=1
    ans[ch][cw] = 0
    move = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    move2 = [
        [-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], \
        [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], \
        [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], \
        [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], \
        [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]
        ]
    q = deque([[ch, cw]])
    one_time = deque([])
    while len(q) > 0:
        x, y = q.popleft()
        one_time.append([x, y])
        for i, j in move:
            v1, v2 = x+i, y+j
            if ans[v1][v2] == -1:
                ans[v1][v2] = ans[x][y]
                q.append([v1, v2])
        if len(q) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.popleft()
                for i, j in move2:
                    v1, v2 = x2+i, y2+j
                    if ans[v1][v2] == -1:
                        ans[v1][v2] = ans[x2][y2] + 1
                        q.append([v1, v2])
    print(ans[dh][dw])

def __starting_point():
    main()
__starting_point()

from collections import deque
import sys
def input():return sys.stdin.readline().strip()
def main():
    H, W = map(int, input().split())
    sh, sw = map(int, input().split())
    sh -= 1; sw -= 1
    start = (sh+2)*(W+4) + 2 + sw
    dh, dw = map(int, input().split())
    dh -= 1; dw -= 1
    goal = (dh+2)*(W+4) + 2 + dw

    field = "#"*(W+4)*2
    field += "##" + "####".join([ input() for _ in range(H)]) + "##"
    field += "#"*(W+4)*2

    move = [-1, 1, -(W+4), W+4]
    ex_left = [-2*(W+4)-2, -(W+4)-2, -2, (W+4)-2, 2*(W+4)-2, -(W+4)-1, (W+4)-1]
    ex_right = [-2*(W+4)+2, -(W+4)+2, +2, (W+4)+2, 2*(W+4)+2, -(W+4)+1, (W+4)+1]
    ex_up = [-2*(W+4)-2, -2*(W+4)-1, -2*(W+4), -2*(W+4)+1, -2*(W+4)+2, -(W+4)-1, -(W+4)+1]
    ex_down = [2*(W+4)-2, 2*(W+4)-1, 2*(W+4), 2*(W+4)+1, 2*(W+4)+2, (W+4)-1, (W+4)+1]

    INF = 10 ** 18
    num = [INF] * (H+4) * (W+4)
    warped = [False] * (H+4) * (W+4)

    def bfs(s):
        q = deque()
        num[s] = 0
        q.append((s, 0))
    
        while q:
            now, cost = q.popleft()

            # warpしたものの既に来ていた場合はスルー
            if cost > num[now]:
                continue
            else:
                num[now] = cost

            if now == goal:
                return

            for i, dx in enumerate(move):
                nx = now + dx
                if field[nx] == "." and num[nx] == INF:
                    q.appendleft((nx, cost))
                    num[nx] = cost

                elif field[nx] == "#":
                    if i == 0:
                        ex = ex_left
                    elif i == 1:
                        ex = ex_right
                    elif i == 2:
                        ex = ex_up
                    else:
                        ex = ex_down

                    for dx2 in ex:
                        nx2 = now + dx2
                        if field[nx2] == "." and num[nx2] == INF and not warped[nx2]:
                            q.append((nx2, cost+1))
                            warped[nx2] = True
    
    bfs(start)

    if num[goal] == INF:
        print(-1)
    else:
        print(num[goal])

    
    # for i in range(0, (H+4)*(W+4), W+4):
    #     print(field[i:i+W+4])
    
    # print(start)
    # print(start//(W+4), start%(W+4))
    # print(goal)
    # print(goal//(W+4), goal%(W+4))
    


def __starting_point():
    main()
__starting_point()

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = list(map(int, readline().split()))
    Ch, Cw = [int(x) - 1 for x in readline().split()]
    Dh, Dw = [int(x) - 1 for x in readline().split()]
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    a = 0
    warpq = []
    while q:
        while q:
            warpq.append(q[0])
            h, w = q.popleft()
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        if t[Dh][Dw] != INF:
            break

        a += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > a:
                        ti[j] = a
                        q.append((i, j))
        warpq.clear()

    if t[Dh][Dw] == INF:
        print((-1))
    else:
        print((t[Dh][Dw]))


main()


from collections import deque


def solve():
    H, W = list(map(int, input().split()))
    CH, CW = list(map(int, input().split()))
    DH, DW = list(map(int, input().split()))
    S = ["##"+input()+"##" for _ in range(H)]
    for i in range(2):
        S.insert(0, "#"*(W+4))
        S.append("#"*(W+4))

    inf = int(1e9)
    cost = [[inf for _ in range(W+4)] for _ in range(H+4)]
    cost0 = deque()
    ans = -1

    cost0.append((CH+1, CW+1, 0))
    cost[CH+1][CW+1] = 0

    move = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)]+move]

    cost1 = deque()

    while cost0:
        h, w, c = cost0.popleft()
        cost1.append((h, w, c))

        for i, j in move:
            dh = h+i
            dw = w+j
            if S[dh][dw] == "." and c < cost[dh][dw]:
                cost[dh][dw] = c
                cost0.appendleft((dh, dw, cost[dh][dw]))

        if len(cost0) == 0:
            while cost1:
                h, w, c = cost1.popleft()
                for i, j in warp:
                    dh = h+i
                    dw = w+j
                    if S[dh][dw] == "." and c+1 < cost[dh][dw]:
                        cost[dh][dw] = c+1
                        cost0.append((dh, dw, cost[dh][dw]))

        if cost[DH+1][DW+1] != inf:
            ans = cost[DH+1][DW+1]

    print(ans)


def __starting_point():
    solve()

__starting_point()

import sys
input = sys.stdin.readline


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))
    
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    
    
    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for i in range(h):
        s.append("##" + input()[:-1] + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = [(ch, cw)]
    one_time = []
    
    while len(not_yet) > 0:
        x, y = not_yet.pop()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.pop()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print((ans[dh][dw]))
    
    
    
def __starting_point():
    main()

__starting_point()


from collections import deque


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))

    """
    l = [list(input().strip()) for i in range(h)]
    ans = [[-1]*w for i in range(h)]
    for i in range(h):
        for j in range(w):
            if l[i][j] == "#":
                ans[i][j] = -2
    ch -= 1
    cw -= 1
    dh -= 1
    dw -= 1
    """

    base = ["#" * (w + 4)]
    base.append("#" * (w + 4))
    for n in range(h):
        base.append("##" + input() + "##")
    base.append("#" * (w + 4))
    base.append("#" * (w + 4))
    ans = [[-1] * (w + 4) for _ in range(h + 4)]
    for i in range(h + 4):
        for j in range(w + 4):
            if base[i][j] == "#":
                ans[i][j] = -2
    ch += 1
    cw += 1
    dh += 1
    dw += 1

    ans[ch][cw] = 0
    move1 = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    move2 = [
        [-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]
    ]
    q1 = deque([[ch, cw]])
    q2 = deque([])

    while q1:
        x1, y1 = q1.popleft()
        q2.append([x1, y1])
        for i, j in move1:
            nx, ny = x1 + i, y1 + j
            """
            if nx < 0 or h <= nx or ny < 0 or w <= ny:
                continue
            """
            if ans[nx][ny] == -1:
                ans[nx][ny] = ans[x1][y1]
                q1.append([nx, ny])
        if len(q1) == 0:
            while q2:
                x2, y2 = q2.popleft()
                for i, j in move2:
                    mx, my = x2 + i, y2 + j
                    """
                    if mx < 0 or h <= mx or my < 0 or w <= my:
                        continue
                    """
                    if ans[mx][my] == -1:
                        ans[mx][my] = ans[x2][y2] + 1
                        q1.append([mx, my])
    print((ans[dh][dw]))


def __starting_point():
    main()



__starting_point()

from collections import deque

def main():
    h, w = list(map(int, input().split()))
    start_h, start_w = [int(x)+1 for x in input().split()]
    goal_h, goal_w = [int(x)+1 for x in input().split()]

    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for _ in range(h):
        s.append("##" + input() + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))

    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[start_h][start_w] = 0

    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),(-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),(0, -2), (0, -1), (0, 0), (0, 1), (0, 2),(1, -2), (1, -1), (1, 0), (1, 1), (1, 2),(2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

    yet = deque([(start_h, start_w)])
    done = deque()

    while yet:
        x1, y1 = yet.popleft()
        done.append((x1, y1))
        for (p, q) in move1:
            v1, v2 = x1+p, y1+q
            if ans[v1][v2] == -1: #"."でまだ辿り着いていないマス
                yet.append((v1, v2))
                ans[v1][v2] = ans[x1][y1]

        if len(yet) == 0:
            while done:
                x2, y2 = done.popleft()
                for (p, q) in move2:
                    i, j = x2+p, y2+q
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        yet.append((i, j))

    print((ans[goal_h][goal_w]))

def __starting_point():
    main()

__starting_point()

#!/usr/bin/env python
# coding: utf-8

# In[1]:


from collections import deque


# In[30]:


def main():
    H,W = list(map(int, input().split()))
    ch,cw = list(map(int, input().split()))
    dh,dw = list(map(int, input().split()))
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [
        [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
        [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
        [0,-2],[0,-1],[0,0],[0,1],[0,2],\
        [1,-2],[1,-1],[1,0],[1,1],[1,2],\
        [2,-2],[2,-1],[2,0],[2,1],[2,2]
        ]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print((ans[dh][dw]))

def __starting_point():
    main()


# In[31]:


# H,W = map(int, input().split())
# ch,cw = map(int, input().split())
# dh,dw = map(int, input().split())
# s = ["#"*(W+4)]
# s.append("#"*(W+4))
# for i in range(H):
#     s.append("##"+input()+"##")
# s.append("#"*(W+4))
# s.append("#"*(W+4))


# In[32]:


# ans = [[-1]*(W+4) for _ in range(H+4)]
# for i in range(H+4):
#     for j in range(W+4):
#         if s[i][j] == "#":
#             ans[i][j] = -2
# ch += 1; cw += 1
# dh += 1; dw += 1
# ans[ch][cw] = 0


# In[33]:


# move = [[-1,0],[1,0],[0,-1],[0,1]]
# magic = [
#     [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
#     [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
#     [0,-2],[0,-1],[0,0],[0,1],[0,2],\
#     [1,-2],[1,-1],[1,0],[1,1],[1,2],\
#     [2,-2],[2,-1],[2,0],[2,1],[2,2]
#     ]


# In[34]:


# mylist = deque([[ch,cw]])
# mylist2 = deque([])
# while len(mylist) > 0:
#     x,y = mylist.popleft()
#     mylist2.append([x,y])
#     for p,q in move:
#         v1,v2 = x+p,y+q
#         if ans[v1][v2] == -1:
#             mylist.append([v1,v2])
#             ans[v1][v2] = ans[x][y]
#     if len(mylist) == 0:
#         while len(mylist2) > 0:
#             x2,y2 = mylist2.popleft()
#             for v1,v2 in magic:
#                 i,j = x2+v1,y2+v2
#                 if ans[i][j] == -1:
#                     ans[i][j] = ans[x2][y2]+1
#                     mylist.append([i,j])
# print(ans[dh][dw])


# In[ ]:





__starting_point()

from collections import deque

def solve(h, w, ch, cw, dh, dw, s):
    W, H = w+5, h+5
    def pos(r, c):
        return (r+2)*W + c+2
    G = [0] * (W*H)
    label = 0
    for r in range(h):
        for c in range(w):
            if (s[r][c] == '.') and (G[pos(r,c)] == 0):
                label += 1
                que = deque([(r, c)])
                G[pos(r,c)] = label
                while que:
                    cr, cc = que.popleft()
                    for dr, dc in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                        nr, nc = cr+dr, cc+dc
                        if (0 <= nr < h) and (0 <= nc < w) and (s[nr][nc] == ".") and (G[pos(nr,nc)] == 0):
                            G[pos(nr,nc)] = label
                            que.append((nr, nc))

    start = pos(ch-1, cw-1)
    goal = pos(dh-1, dw-1)

    neighbors = [(dr*W+dc) for dc in range(-2, 3) for dr in range(-2, 3) if abs(dr) + abs(dc) > 0]
    used = [True] * (W*H)
    for r in range(h):
        for c in range(w):
            used[pos(r,c)] = (s[r][c] == "#")
    que = deque([(start, 0)])
    label_dist_mapping = [-1] * (label + 1)
    used[start] = True
    label_dist_mapping[G[start]] = 0
    while que:
        cpos, n = que.popleft()
        if cpos == goal:
            return n
        for d in neighbors:
            npos = cpos + d
            if not used[npos]:
                used[npos] = True
                m = label_dist_mapping[G[npos]]
                if (G[npos] == G[cpos]) or (n == m): # 同一グループ
                    que.appendleft((npos, n))
                else:
                    label_dist_mapping[G[npos]] = n + 1
                    que.append((npos, n+1))
    return -1

h, w = list(map(int, input().split()))
ch, cw = list(map(int, input().split()))
dh, dw = list(map(int, input().split()))
s = [input() for i in range(h)]
print((solve(h, w, ch, cw, dh, dw, s)))


from collections import deque
 
def main():
  h,w=list(map(int,input().split()))
  c1,c2=[int(x)+1 for x in input().split()]
  d1,d2=[int(x)+1 for x in input().split()]
  work1=[(1,0),(-1,0),(0,1),(0,-1)]
  work2=[(i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0,0)]+work1]
  memo=["#"*(w+4)]*2+["##"+input()+"##" for _ in range(h)]+["#"*(w+4)]*2
  dist=[[-1]*(w+4) for _ in range(h+4)]
  for i in range(h+4):
    for j in range(w+4):
      if memo[i][j]=="#":
        dist[i][j]=float("inf")
  que1=deque([(c1,c2)])
  que2=deque([])
  dist[c1][c2]=0
  while que1:
    y,x=que1.popleft()
    score=dist[y][x]
    que2.append((y,x))
    for py,px in work1:
      ny,nx=y+py,x+px
      if dist[ny][nx]==-1:
        dist[ny][nx]=score
        que1.append((ny,nx))
    if not que1:
      while que2:
        y,x=que2.popleft()
        for py,px in work2:
          ny,nx=y+py,x+px
          if dist[ny][nx]==-1:
            dist[ny][nx]=score+1
            que1.append((ny,nx))
  print((dist[d1][d2]))

def __starting_point():
    main()

__starting_point()

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = list(map(int, readline().split()))
    Ch, Cw = [int(x) - 1 for x in readline().split()]
    Dh, Dw = [int(x) - 1 for x in readline().split()]
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    a = 0
    while q:
        warpq = []
        while q:
            h, w = q.popleft()
            warpq.append((h, w))
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        if t[Dh][Dw] != INF:
            break

        a += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > a:
                        ti[j] = a
                        q.append((i, j))

    if t[Dh][Dw] == INF:
        print((-1))
    else:
        print((t[Dh][Dw]))


main()


# import numpy as np
from collections import deque

def solve(H, W, CH, CW, DH, DW, S):
	# 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
	S = ['##{}##'.format(row) for row in S]
	S.insert(0, '##{}##'.format('#' * W))
	S.insert(0, '##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))


	MAX_COST = 10 ** 9
	Cost=[ [ MAX_COST for _ in range(W+4)] for _ in range(H+4)]
	# Cost = np.full((H,W),MAX_COST)

	# print(S)

	ans = -1

	cost0 = deque()

	cost0.append((CH+1,CW+1,0))
	Cost[CH+1][CW+1] = 0;

	# used = set()
	walk=[(1,0),(0,1),(-1,0),(0,-1)]
	warp = [ (i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0, 0)] + walk ]
	# print(warp)

	cost1 = deque()

	while cost0:
		h,w,c=cost0.popleft()
		cost1.append((h,w,c))

		for i,j in walk:
			dh = h+i
			dw = w+j
			if S[dh][dw] == '.' and c < Cost[dh][dw]:
				Cost[dh][dw] = c
				# print("updl ", i,j,dh,dw,c)
				cost0.appendleft((dh,dw,Cost[dh][dw]))

		if len(cost0) == 0:

			while cost1:
				h,w,c=cost1.popleft()
				# print(h,w,c)
				for i,j in warp:
					dh = h+i
					dw = w+j
					# print(i,j)
					if S[dh][dw] == '.' and c + 1 < Cost[dh][dw]:
						Cost[dh][dw] = c + 1
						cost0.append((dh,dw,Cost[dh][dw]))


	if Cost[DH+1][DW+1] == MAX_COST:
		print((-1))
	else:
		print((Cost[DH+1][DW+1]))


def __starting_point():
	H,W=list(map(int,input().split()))
	CH,CW=list(map(int,input().split()))
	DH,DW=list(map(int,input().split()))

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

	solve(H, W, CH, CW, DH, DW, S)

__starting_point()

from collections import deque

def bfs():
    H,W=map(int,input().split())
    sy,sx=map(int,input().split())
    gy,gx=map(int,input().split())
    S=['#'*(W+4),'#'*(W+4)]
    for _ in range(H):
        S.append('##'+input()+'##')
    S.append('#'*(W+4))
    S.append('#'*(W+4))
    dist=[[-2]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
      for j in range(W+4):
        if S[i][j]=='.':
          dist[i][j]=-1
    dist[sy+1][sx+1]=0
    A=[(1,0),(-1,0),(0,1),(0,-1)]
    B=[(i,j)for i in range(-2,3)for j in range(-2,3)if abs(i)+abs(j)>1]
    que=deque([[sx+1,sy+1]])
    que2=deque([])
    while que:
        x,y=que.popleft()
        que2.appendleft([x,y])
        for dx,dy in A:
            nx=x+dx
            ny=y+dy
            if dist[ny][nx]==-1:
                dist[ny][nx]=dist[y][x]
                que.appendleft([nx,ny])
        if que:
            continue
        while que2:
            x,y=que2.popleft()
            for dx,dy in B:
                nx=x+dx
                ny=y+dy
                if dist[ny][nx]==-1:
                    dist[ny][nx]=dist[y][x]+1
                    que.append([nx,ny])
    ans=dist[gy+1][gx+1]
    return ans

print(bfs())

from collections import deque
def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = list(map(int, readline().split()))
    C1, C2 = [int(x) - 1 for x in readline().split()]
    D1, D2 = [int(x) - 1 for x in readline().split()]
    S = [readline()[:-1] for _ in range(H)]



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


    for h in range (H):
        Sh = S[h]
        th = t[h]
        for w in range (W):
            if Sh[w] == '#':
                th[w] = -1


    q = deque([(C1,C2)])
    a = 0
    t[C1][C2] = a

    while q:
        warpq = []
        while q:
            h,w = q.popleft()
            warpq.append((h,w))
            if 0 < h and t[h-1][w] > a:
                t[h-1][w] = a
                q.append((h-1,w))
            if H-1 > h and t[h+1][w] > a:
                t[h+1][w] = a
                q.append((h+1,w))
            if 0 < w and t[h][w-1] > a:
                t[h][w-1] = a
                q.append((h,w-1))
            if w < W-1 and t[h][w+1] > a:
                t[h][w+1] = a
                q.append((h,w+1))

        if t[D1][D2]!= INF:
            break

        a+=1

        for h,w in warpq:
            for i in range (max(0,h-2),min(H,h+3)):
                ti = t[i]
                for j in range (max(0,w-2),min(W,w+3)):
                    if ti[j] >a:
                        ti[j] = a
                        q.append((i,j))


    if t[D1][D2] == INF:
        print((-1))
    else:
        print((t[D1][D2]))

main()


def main():
    import sys
    input = sys.stdin.readline
    H, W = [int(x) for x in input().strip().split()]
    Ch, Cw = [int(x)+1 for x in input().strip().split()]
    Dh, Dw = [int(x)+1 for x in input().strip().split()]
    M = [0 for _ in range(H+4)]
    M[0] = ['#'] * (W+4)
    M[1] = ['#'] * (W+4)
    M[-2] = ['#'] * (W+4)
    M[-1] = ['#'] * (W+4)
    for h in range(2, H+2):
        M[h] = ['#'] * 2 + list(input().strip()) + ['#'] * 2
        # print(M[h])
    ans = 0
    q = set([(Ch, Cw)])
    M[Ch][Cw] = '#'
    dhdw = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    while True:
        newq = set()
        while q:
            q_ = set()
            for h, w in q:
                # print('({}, {}) = {}'.format(h, w, ans))
                if h == Dh and w == Dw:
                    print(ans)
                    return True
                newq.add((h, w))
                for dh, dw in dhdw:
                    if M[h+dh][w+dw] == '#':
                        continue
                    q_.add((h+dh, w+dw))
                    M[h+dh][w+dw] = '#'
            q = q_

        if newq:
            for h, w in newq:
                for dh in range(-2, 3):
                    for dw in range(-2, 3):
                        if M[h+dh][w+dw] == '.':
                            q.add((h+dh, w+dw))
                            M[h+dh][w+dw] = '#'
        # for h in range(H):
        #     print(M[h])
        if len(q):
            ans += 1
        else:
            print(-1)
            return True

def __starting_point():
    main()
__starting_point()

from collections import deque
from itertools import product


def main():
    h, w = list(map(int, input().split()))
    c = tuple([int(x)+1 for x in input().split()])
    d = tuple([int(x)+1 for x in input().split()])
    s = ['#'*(w+4)]*2 + [f'##{input()}##' for _ in range(h)] + ['#'*(w+4)]*2

    move_a_area = [(0, -1), (-1, 0), (1, 0), (0, 1)]
    move_b_area = [(i, j) for i, j in product(list(range(-2, 3)), repeat=2) if (i, j) not in [(0, 0)] + move_a_area]

    cost = [[float('inf')] * (w+4) for _ in range(h+4)]
    cost[c[0]][c[1]] = 0
    yet = deque([(c[0], c[1], 0)])
    done = deque()

    while yet:
        y, x, m = yet.popleft()
        done.append((y, x, m))
        # Move A
        for dx, dy in move_a_area:
            hh, ww = y + dy, x + dx
            if s[hh][ww] == '#' or cost[hh][ww] <= m:
                continue
            cost[hh][ww] = m
            yet.append((hh, ww, m))
        # Move B
        if not yet:
            while done:
                y, x, m = done.popleft()
                for dx, dy in move_b_area:
                    hh, ww = y + dy, x + dx
                    if s[hh][ww] == '#' or cost[hh][ww] <= m + 1:
                        continue
                    cost[hh][ww] = m + 1
                    yet.append((hh, ww, m + 1))

    print((v if (v := cost[d[0]][d[1]]) < float('inf') else -1))


def __starting_point():
    main()

__starting_point()

from collections import deque
def solve():  
    h, w = list(map(int,input().split()))
    ch, cw = list(map(int,input().split()))
    Dh, Dw = list(map(int,input().split()))
    
    M = ['##' + input() + '##' for _ in range(h)]
    for i in range(2):
        M.insert(0,'#'*(w+4))
        M.append('#'*(w+4))
    
    INF = float('inf')
    cost = [[INF for _ in range(w+4)] for _ in range(h+4)]
    cost0 = deque()
    ans = -1
    
    cost0.append((ch+1, cw+1, 0))
    cost[ch+1][cw+1] = 0
    
    move = [(1,0),(0,1),(-1,0),(0,-1)]
    warp = [(i,j) for i in range(-2,3) for j in range(-2,3) if (i,j) not in [(0,0)] + move]
    
    cost1 = deque()
    
    while cost0:
        h, w, c = cost0.popleft()
        cost1.append((h, w, c))
        
        for i,j in move:
            dh = h + i
            dw = w + j
            if M[dh][dw] == '.' and c < cost[dh][dw]: #コストが高いところから低いところに戻らないようにするため
                cost[dh][dw] = c
                cost0.appendleft((dh, dw, cost[dh][dw]))
        
        if len(cost0) == 0:
            while cost1:
                h, w, c = cost1.popleft()
                for i,j in warp:
                    dh = h + i
                    dw = w + j
                    if M[dh][dw] == '.' and c+1 < cost[dh][dw]:  #無駄なワープをなくすため
                        cost[dh][dw] = c+1
                        cost0.append((dh, dw, cost[dh][dw]))
        if cost[Dh+1][Dw+1] != INF:
            ans = cost[Dh+1][Dw+1]
    
    print(ans)

def __starting_point():
    solve()

__starting_point()

from collections import deque

def main():
    h, w = list(map(int, input().split()))
    c = list([int(x)+1 for x in input().split()])
    d = list([int(x)+1 for x in input().split()])
    s = ['#'*(w+4)]*2 + ['#'*2 + input() + '#'*2 for _ in range(h)] + ['#'*(w+4)]*2

    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == '#':
                ans[i][j] = -2
    ans[c[0]][c[1]] = 0

    move1 = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    move2 = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if abs(i)+abs(j) > 1]

    yet = deque([(c[0], c[1])])
    done = deque()

    while yet:
        y, x = yet.popleft()
        done.append((y, x))
        for dy, dx in move1:
            ydy, xdx = y+dy, x+dx
            if ans[ydy][xdx] == -1:
                yet.append((ydy, xdx))
                ans[ydy][xdx] = ans[y][x]

        if len(yet) == 0:
            while done:
                y, x = done.popleft()
                for dy, dx in move2:
                    ydy, xdx = y+dy, x+dx
                    if ans[ydy][xdx] == -1:
                        ans[ydy][xdx] = ans[y][x] + 1
                        yet.append((ydy, xdx))

    print((ans[d[0]][d[1]]))

def __starting_point():
    main()

__starting_point()

from collections import deque

def main():
  h,w=list(map(int,input().split()))
  cy,cx=list(map(int,input().split()))
  dy,dx=list(map(int,input().split()))

  M=["##"+input()+"##" for x in range(h)]
  for i in range(2):
    M.insert(0,"#"*(w+4))
    M.append("#"*(w+4))

  C=[[-1 if M[j][i]=="." else -2 for i in range(w+4)] for j in range(h+4)]
  C[-~cy][-~cx]=0
  QA,QB=deque(),deque()
  QA.append((-~cy,-~cx,0))
  W=[[i,j] for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]

  while QA:
    h,w,c=QA.popleft()
    QB.append((h,w,c))

    for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
      dh,dw=h+i,w+j
      if C[dh][dw]==-1:
        C[dh][dw]=c
        QA.appendleft((dh,dw,c))

    if QA:
      continue

    while QB:
      h,w,c=QB.popleft()
      for i,j in W:
        dh,dw=h+i,w+j
        if C[dh][dw]==-1:
          C[dh][dw]=-~c
          QA.append((dh,dw,-~c))

    if C[-~dy][-~dx]!=-1:
      print((C[-~dy][-~dx]))
      return

  print((C[-~dy][-~dx]))

def __starting_point():
  main()

__starting_point()

H, W = map(int, input().split())
Ch, Cw = map(int, input().split())
Dh, Dw = map(int, input().split())

Ch += 1
Cw += 1
Dh += 1
Dw += 1


S = ['#'*(W+4)]
S.append('#'*(W+4))
for k in range(H):
  S.append('##' + input() + '##')
S.append('#'*(W+4))
S.append('#'*(W+4))

foo = [[-1 for _ in range(W+4)] for _ in range(H+4)]
for k in range(2, H+2):
  for j in range(2, W+2):
    if S[k][j] == '.':
      foo[k][j] = 10**6

foo[Ch][Cw] = 0

now = []
new = [(Ch, Cw)]
while len(new) > 0:
  item = new.pop()
  now.append(item)
  x = item[0]
  y = item[1]
  if foo[x+1][y] == 10**6:
    foo[x+1][y] = 0
    new.append((x+1, y))
  if foo[x][y+1] == 10**6:
    foo[x][y+1] = 0
    new.append((x, y+1))
  if foo[x-1][y] == 10**6:
    foo[x-1][y] = 0
    new.append((x-1, y))
  if foo[x][y-1] == 10**6:
    foo[x][y-1] = 0
    new.append((x, y-1))


ans = 0
while foo[Dh][Dw] == 10**6:
  if len(now) == 0:
    print(-1)
    return 
  ans += 1
  while len(now) > 0:
    item = now.pop()
    x = item[0]
    y = item[1]
    if foo[x+2][y+2] > ans:
      foo[x+2][y+2] = ans
      new.append((x+2, y+2))
    if foo[x+2][y+1] > ans:
      foo[x+2][y+1] = ans
      new.append((x+2, y+1))
    if foo[x+2][y] > ans:
      foo[x+2][y] = ans
      new.append((x+2, y))
    if foo[x+2][y-1] > ans:
      foo[x+2][y-1] = ans
      new.append((x+2, y-1))
    if foo[x+2][y-2] > ans:
      foo[x+2][y-2] = ans
      new.append((x+2, y-2))
    if foo[x+1][y+2] > ans:
      foo[x+1][y+2] = ans
      new.append((x+1, y+2))
    if foo[x+1][y-2] > ans:
      foo[x+1][y-2] = ans
      new.append((x+1, y-2))
    if foo[x][y+2] > ans:
      foo[x][y+2] = ans
      new.append((x, y+2))
    if foo[x][y-2] > ans:
      foo[x][y-2] = ans
      new.append((x, y-2))
    if foo[x-2][y+2] > ans:
      foo[x-2][y+2] = ans
      new.append((x-2, y+2))
    if foo[x-2][y+1] > ans:
      foo[x-2][y+1] = ans
      new.append((x-2, y+1))
    if foo[x-2][y] > ans:
      foo[x-2][y] = ans
      new.append((x-2, y))
    if foo[x-2][y-1] > ans:
      foo[x-2][y-1] = ans
      new.append((x-2, y-1))
    if foo[x-2][y-2] > ans:
      foo[x-2][y-2] = ans
      new.append((x-2, y-2))
    if foo[x-1][y+2] > ans:
      foo[x-1][y+2] = ans
      new.append((x-1, y+2))
    if foo[x-1][y-2] > ans:
      foo[x-1][y-2] = ans
      new.append((x-1, y-2))
    if foo[x-1][y-1] > ans:
      foo[x-1][y-1] = ans
      new.append((x-1, y-1))
    if foo[x-1][y+1] > ans:
      foo[x-1][y+1] = ans
      new.append((x-1, y+1))
    if foo[x+1][y+1] > ans:
      foo[x+1][y+1] = ans
      new.append((x+1, y+1))
    if foo[x+1][y-1] > ans:
      foo[x+1][y-1] = ans
      new.append((x+1, y-1))
      
  while len(new) > 0:
    item = new.pop()
    now.append(item)
    x = item[0]
    y = item[1]
    if foo[x+1][y] == 10**6:
      foo[x+1][y] = 0
      new.append((x+1, y))
    if foo[x][y+1] == 10**6:
      foo[x][y+1] = 0
      new.append((x, y+1))
    if foo[x-1][y] == 10**6:
      foo[x-1][y] = 0
      new.append((x-1, y))
    if foo[x][y-1] == 10**6:
      foo[x][y-1] = 0
      new.append((x, y-1))
print(ans)

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


import numpy as np
from numba import njit, b1, i4, i8, f8
from heapq import heappop as hpp, heappush as hp

@njit((b1[:, :],i8,i8,i8,i8))
def main(ss,ch,cw,dh,dw):
    inf = 1<<30
    h,w = ss.shape
    start = ch*w+cw
    goal = dh*w+dw
    n = h*w
    seen = np.full(n, inf, np.int64)
    seen[start] = 0
    q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置
    while q:
        pnum,pu = hpp(q)
        ux,uy = divmod(pu,w)
        if pu==goal:
            break
        for xx in range(-2, 3):
            for yy in range(-2, 3):
                if xx==yy==0:
                    continue
                if abs(xx)+abs(yy)<=1:
                    vv = 0
                else:
                    vv = 1
                x,y = ux+xx, uy+yy
                u = x*w + y
                num = pnum+vv
                if x<0 or y<0 or x>=h or y>=w or ss[x][y]:
                    continue
        #         print(x,y)
                if seen[u]>num:
                    seen[u] = num
                    hp(q,(num, u))
                
    val = seen[goal]
    if val==inf:
        return -1
    else:
        return val
h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh,dw = map(int, input().split())
ch -= 1
cw -= 1
dh -= 1
dw -= 1
rows,cols = h,w

OK = "."
NG = "#"

ss = np.array([[c=="#" for c in input()] for _ in range(h)])

print(main(ss,ch,cw,dh,dw))

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = list(map(int, readline().split()))
    Ch, Cw = [int(x) - 1 for x in readline().split()]
    Dh, Dw = [int(x) - 1 for x in readline().split()]
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    a = 0
    warpq = []
    while q:
        while q:
            h, w = q.popleft()
            warpq.append((h, w))
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        if t[Dh][Dw] != INF:
            break

        a += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > a:
                        ti[j] = a
                        q.append((i, j))
        warpq.clear()

    if t[Dh][Dw] == INF:
        print((-1))
    else:
        print((t[Dh][Dw]))


main()


from collections import deque

def solve():
    H,W = map(int,input().split())
    C = list(map(int,input().split()))
    D = list(map(int,input().split()))

    S = ["##" + input() + "##" for _ in range(H)]
    for i in range(2):
        S.insert(0,"#"*(W+4))
        S.append("#"*(W+4))

    INF = float("inf")
    cost = [[INF for _ in range(W+4)] for _ in range(H+4)]
    cost0 = deque()
    ans = -1

    cost0.append((C[0]+1, C[1]+1, 0))
    cost[C[0]+1][C[1]+1] = 0

    move = [(1,0), (0,1), (-1,0), (0,-1)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)]+move]

    cost1 = deque()

    while cost0:
        h,w,c = cost0.popleft()
        cost1.append((h,w,c))

        for i, j in move:
            dh = h+i
            dw = w+j
            if S[dh][dw] == "." and c < cost[dh][dw]:
                cost[dh][dw] = c
                cost0.appendleft((dh, dw, cost[dh][dw]))
        
        if len(cost0) == 0:
            while cost1:
                h, w, c = cost1.popleft()
                for i, j in warp:
                    dh = h+i
                    dw = w+j
                    if S[dh][dw] == "." and c+1 < cost[dh][dw]:
                        cost[dh][dw] = c+1
                        cost0.append((dh,dw, cost[dh][dw]))
        
        if cost[D[0]+1][D[1]+1] != INF:
            ans = cost[D[0]+1][D[1]+1]

    print(ans)

def __starting_point():
    solve()
__starting_point()

import sys
from collections import deque
input = sys.stdin.readline


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))
    
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    
    
    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for i in range(h):
        s.append("##" + input()[:-1] + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))
    
    
    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[ch][cw] = 0
    
    move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), \
             (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), \
             (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), \
             (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), \
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
        
    not_yet = deque([(ch, cw)])
    one_time = deque([])
    
    while len(not_yet) > 0:
        x, y = not_yet.popleft()
        one_time.append((x, y))
        for (p, q) in move:
            v1, v2 = x+p, y+q
            if ans[v1][v2] == -1:
                not_yet.append((v1, v2))
                ans[v1][v2] = ans[x][y]
                
        if len(not_yet) == 0:
            while len(one_time) > 0:
                x2, y2 = one_time.popleft()
                for (v1, v2) in move2:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        not_yet.append((i, j))
          
    print((ans[dh][dw]))
    
    
    
def __starting_point():
    main()

__starting_point()

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = list(map(int, readline().split()))
    Ch, Cw = [int(x) - 1 for x in readline().split()]
    Dh, Dw = [int(x) - 1 for x in readline().split()]
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    warp_count = 0
    warpq = []
    while q:
        while q:
            warpq.append(q[0])
            h, w = q.popleft()
            if h - 1 >= 0 and t[h - 1][w] > warp_count:
                q.append((h - 1, w))
                t[h - 1][w] = warp_count
            if h + 1 < H and t[h + 1][w] > warp_count:
                q.append((h + 1, w))
                t[h + 1][w] = warp_count
            if w - 1 >= 0 and t[h][w - 1] > warp_count:
                q.append((h, w - 1))
                t[h][w - 1] = warp_count
            if w + 1 < W and t[h][w + 1] > warp_count:
                q.append((h, w + 1))
                t[h][w + 1] = warp_count

        if t[Dh][Dw] != INF:
            break

        warp_count += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > warp_count:
                        ti[j] = warp_count
                        q.append((i, j))
        warpq.clear()

    if t[Dh][Dw] == INF:
        print((-1))
    else:
        print((t[Dh][Dw]))


main()


from collections import deque

def main():
  h,w=map(int,input().split())
  cy,cx=map(int,input().split())
  dy,dx=map(int,input().split())

  M=["##"+input()+"##" for x in range(h)]
  for i in range(2):
    M.insert(0,"#"*(w+4))
    M.append("#"*(w+4))

  C=[[-1 if M[j][i]=="." else -2 for i in range(w+4)] for j in range(h+4)]
  C[-~cy][-~cx]=0
  QA,QB=deque(),deque()
  QA.append((-~cy,-~cx,0))
  W=[[i,j] for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]

  while QA:
    h,w,c=QA.popleft()
    QB.append((h,w,c))

    for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
      dh,dw=h+i,w+j
      if C[dh][dw]==-1:
        C[dh][dw]=c
        QA.appendleft((dh,dw,c))

    if QA:
      continue

    while QB:
      h,w,c=QB.popleft()
      for i,j in W:
        dh,dw=h+i,w+j
        if C[dh][dw]==-1:
          C[dh][dw]=-~c
          QA.append((dh,dw,-~c))

  print(C[-~dy][-~dx])

def __starting_point():
  main()
__starting_point()

def main():
    from collections import deque
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))
    # 迷路の四方を##で囲む。
    # 移動で枠外にでてしまってindexがないことを阻止するための
    # if文を回避
    maze = ['#' * (w+4)]
    maze.append('#' * (w+4))
    for _ in range(h):
        maze.append('##' + input()[:-1] + '##')
    maze.append('#' * (w+4))
    maze.append('#' * (w+4))
    visited = [[-1] * (w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if maze[i][j] == '#':
                visited[i][j] = -2
    # この条件ではindexを+1しないとダメ
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    visited[ch][cw] = 0
    walk = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    telepo = [[i,j] for i in range(-2, 3) for j in range(-2, 3)]

    q_walk = deque([[ch, cw]])
    q_telepo = deque([])
    while q_walk:
        yw, xw = q_walk.popleft()
        # 行った場所を管理
        q_telepo.append([yw, xw])
        for i, j in walk:
            ny, nx = yw + i, xw + j
            # if ny < 0 or h <= ny or nx < 0 or w <= nx:
            #     continue
            if visited[ny][nx] == -1:
                # 行ったことあれば0にしていく
                visited[ny][nx] = visited[yw][xw]
                q_walk.append([ny, nx])
        # 歩ける場所なくなったらワープ
        if len(q_walk) == 0:
            while q_telepo:
                yt, xt = q_telepo.popleft()
                for i, j in telepo:
                    my, mx = yt + i, xt + j
                    # if my < 0 or h <= my or mx < 0 or w <= mx:
                    #     continue
                    # 次がまだ行ったことないところだったら、今までの値に+1
                    # つまり、テレポートしないといけない場所
                    if visited[my][mx] == -1:
                        visited[my][mx] = visited[yt][xt] + 1
                        q_walk.append([my, mx])
    print((visited[dh][dw]))

def __starting_point():
    main()

__starting_point()

# import numpy as np
from collections import deque

def solve(H, W, CH, CW, DH, DW):#, S):
	# 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
	S=[input() for _ in range(H)]
	S = ['##{}##'.format(row) for row in S]
	S.insert(0, '##{}##'.format('#' * W))
	S.insert(0, '##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))
	S.append('##{}##'.format('#' * W))


	MAX_COST = 10 ** 9
	Cost=[ [ MAX_COST for _ in range(W+4)] for _ in range(H+4)]
	# Cost = np.full((H,W),MAX_COST)

	# print(S)

	ans = -1

	cost0 = deque()

	cost0.append((CH+1,CW+1,0))
	Cost[CH+1][CW+1] = 0;

	# used = set()
	walk=[(1,0),(0,1),(-1,0),(0,-1)]
	warp = [ (i,j) for i in range(-2,3) for j in range(-2,3) if (i, j) not in [(0, 0)] + walk ]
	# print(warp)

	cost1 = deque()

	while cost0:
		h,w,c=cost0.popleft()
		cost1.append((h,w,c))

		for i,j in walk:
			dh = h+i
			dw = w+j
			if S[dh][dw] == '.' and c < Cost[dh][dw]:
				Cost[dh][dw] = c
				# print("updl ", i,j,dh,dw,c)
				cost0.appendleft((dh,dw,Cost[dh][dw]))

		if len(cost0) == 0:

			while cost1:
				h,w,c=cost1.popleft()
				# print(h,w,c)
				for i,j in warp:
					dh = h+i
					dw = w+j
					# print(i,j)
					if S[dh][dw] == '.' and c + 1 < Cost[dh][dw]:
						Cost[dh][dw] = c + 1
						cost0.append((dh,dw,Cost[dh][dw]))


	if Cost[DH+1][DW+1] == MAX_COST:
		print((-1))
	else:
		print((Cost[DH+1][DW+1]))


def __starting_point():
	H,W=list(map(int,input().split()))
	CH,CW=list(map(int,input().split()))
	DH,DW=list(map(int,input().split()))

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

	solve(H, W, CH, CW, DH, DW)#, S)

__starting_point()

from collections import deque


def solve(H, W, Ch, Cw, Dh, Dw, maze):
    # 後の条件分岐を簡略化するためワープしても迷路外に出ないように壁で囲む
    walled_maze = ['##{}##'.format(row) for row in maze]
    walled_maze.insert(0, '##{}##'.format('#' * W))
    walled_maze.insert(0, '##{}##'.format('#' * W))
    walled_maze.append('##{}##'.format('#' * W))
    walled_maze.append('##{}##'.format('#' * W))

    INF = 10 ** 12
    path = [[INF] * (W + 4) for _ in range(H + 4)]
    walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

    yet = deque()
    yet.append((Ch + 2, Cw + 2, 0))    # 囲った壁の分だけプラス
    path[Ch + 2][Cw + 2] = 0
    done = deque()

    while yet:
        y, x, s = yet.popleft()
        done.append((y, x, s))
        for dy, dx in walk:
            ny = y + dy
            nx = x + dx
            if walled_maze[ny][nx] == '.' and path[ny][nx] > s:
                path[ny][nx] = s
                yet.append((ny, nx, s))
        if len(yet) == 0:
            while done:
                y, x, s = done.popleft()
                for dy, dx in warp:
                    ny = y + dy
                    nx = x + dx
                    if walled_maze[ny][nx] == '.' and path[ny][nx] > s + 1:
                        path[ny][nx] = s + 1
                        yet.append((ny, nx, s + 1))
    ans = path[Dh + 2][Dw + 2] if path[Dh + 2][Dw + 2] < INF else -1
    print(ans)


def __starting_point():
    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 = [input() for _ in range(H)]
    solve(H, W, Ch, Cw, Dh, Dw, maze)

__starting_point()


from collections import deque


def main():
    h, w = list(map(int, input().split()))
    ch, cw = list(map(int, input().split()))
    dh, dw = list(map(int, input().split()))

    """
    l = [list(input().strip()) for i in range(h)]
    ans = [[-1]*w for i in range(h)]
    for i in range(h):
        for j in range(w):
            if l[i][j] == "#":
                ans[i][j] = -2
    ch -= 1
    cw -= 1
    dh -= 1
    dw -= 1
    """

    base = ["#" * (w + 4)]
    base.append("#" * (w + 4))
    for n in range(h):
        base.append("##" + input() + "##")
    base.append("#" * (w + 4))
    base.append("#" * (w + 4))
    ans = [[-1] * (w + 4) for _ in range(h + 4)]
    for i in range(h + 4):
        for j in range(w + 4):
            if base[i][j] == "#":
                ans[i][j] = -2
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    
    ans[ch][cw] = 0
    move1 = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    move2 = [
        [-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]
    ]
    q1 = deque([[ch, cw]])
    q2 = deque([])

    while q1:
        x1, y1 = q1.popleft()
        q2.append([x1, y1])
        for i, j in move1:
            nx, ny = x1 + i, y1 + j
            """
            if nx < 0 or h <= nx or ny < 0 or w <= ny:
                continue
            """
            if ans[nx][ny] == -1:
                ans[nx][ny] = ans[x1][y1]
                q1.append([nx, ny])
        if len(q1) == 0:
            while q2:
                x2, y2 = q2.popleft()
                for i, j in move2:
                    mx, my = x2 + i, y2 + j
                    """
                    if mx < 0 or h <= mx or my < 0 or w <= my:
                        continue
                    """
                    if ans[mx][my] == -1:
                        ans[mx][my] = ans[x2][y2] + 1
                        q1.append([mx, my])
    print((ans[dh][dw]))


def __starting_point():
    main()



__starting_point()

from collections import deque

def main():
  h,w=list(map(int,input().split()))
  cy,cx=list(map(int,input().split()))
  dy,dx=list(map(int,input().split()))

  M=["##"+input()+"##" for x in range(h)]
  for i in range(2):
    M.insert(0,"#"*(w+4))
    M.append("#"*(w+4))

  C=[[-1 if M[j][i]=="." else -2 for i in range(w+4)] for j in range(h+4)]
  C[-~cy][-~cx]=0
  QA,QB=deque(),deque()
  QA.append((-~cy,-~cx,0))
  W=[[i,j] for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]

  while QA:
    h,w,c=QA.popleft()
    QB.append((h,w,c))

    for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
      dh,dw=h+i,w+j
      if C[dh][dw]==-1:
        C[dh][dw]=c
        QA.appendleft((dh,dw,c))

    if QA:
      continue

    while QB:
      h,w,c=QB.popleft()
      for i,j in W:
        dh,dw=h+i,w+j
        if C[dh][dw]==-1:
          C[dh][dw]=-~c
          QA.append((dh,dw,-~c))

  print((C[-~dy][-~dx]))

def __starting_point():
  main()

__starting_point()

#写経コードです。
from collections import deque

def main():
  h,w=map(int,input().split())
  cy,cx=map(int,input().split())
  dy,dx=map(int,input().split())

  M=["##"+input()+"##" for x in range(h)]
  for i in range(2):
    M.insert(0,"#"*(w+4))
    M.append("#"*(w+4))

  C=[[-1 if M[j][i]=="." else -2 for i in range(w+4)] for j in range(h+4)]
  C[-~cy][-~cx]=0
  QA,QB=deque(),deque()
  QA.append((-~cy,-~cx,0))
  W=[[i,j] for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]

  while QA:
    h,w,c=QA.popleft()
    QB.append((h,w,c))

    for i,j in [[1,0],[0,1],[-1,0],[0,-1]]:
      dh,dw=h+i,w+j
      if C[dh][dw]==-1:
        C[dh][dw]=c
        QA.appendleft((dh,dw,c))

    if QA:
      continue

    while QB:
      h,w,c=QB.popleft()
      for i,j in W:
        dh,dw=h+i,w+j
        if C[dh][dw]==-1:
          C[dh][dw]=-~c
          QA.append((dh,dw,-~c))

    if C[-~dy][-~dx]!=-1:
      print(C[-~dy][-~dx])
      return

  print(C[-~dy][-~dx])

def __starting_point():
  main()
__starting_point()

from collections import deque


def main():
    from sys import stdin
    readline = stdin.readline

    from builtins import max, min, range

    INF = 10 ** 6

    H, W = map(int, readline().split())
    Ch, Cw = map(lambda x: int(x) - 1, readline().split())
    Dh, Dw = map(lambda x: int(x) - 1, readline().split())
    S = [readline()[:-1] for _ in range(H)]

    t = [[INF] * W for _ in range(H)]
    for h in range(H):
        th = t[h]
        Sh = S[h]
        for w in range(W):
            if Sh[w] == '#':
                th[w] = -1

    t[Ch][Cw] = 0
    q = deque([(Ch, Cw)])
    a = 0
    while q:
        warpq = []
        while q:
            h, w = q.popleft()
            warpq.append((h, w))
            if h - 1 >= 0 and t[h - 1][w] > a:
                q.append((h - 1, w))
                t[h - 1][w] = a
            if h + 1 < H and t[h + 1][w] > a:
                q.append((h + 1, w))
                t[h + 1][w] = a
            if w - 1 >= 0 and t[h][w - 1] > a:
                q.append((h, w - 1))
                t[h][w - 1] = a
            if w + 1 < W and t[h][w + 1] > a:
                q.append((h, w + 1))
                t[h][w + 1] = a

        if t[Dh][Dw] != INF:
            break

        a += 1
        for h, w in warpq:
            for i in range(max(0, h - 2), min(H, h + 3)):
                ti = t[i]
                for j in range(max(0, w - 2), min(W, w + 3)):
                    if ti[j] > a:
                        ti[j] = a
                        q.append((i, j))

    if t[Dh][Dw] == INF:
        print(-1)
    else:
        print(t[Dh][Dw])


main()

import heapq
import numpy as np
from numba import njit,i8,b1

h,w = map(int,input().split())
sh,sw = map(int,input().split())
gh,gw = map(int,input().split())
s = np.array([list(input()) for _ in range(h)]) == "#"

@njit(i8(i8,i8,i8,i8,i8,i8,b1[:,:]))
def solve(h,w,sh,sw,gh,gw,s):

    sh-=1; sw-=1; gh-=1; gw-=1;

    INF = 10**7
    dist = [[INF]*w for _ in range(h)]
    dist[sh][sw] = 0
    q = [(0,sh,sw)]
    while q:

        d,vsh,vsw = heapq.heappop(q)

        for dh in range(-2,3):
            for dw in range(-2,3):
                newh = vsh+dh
                neww = vsw+dw
                if newh<0 or h<=newh or neww<0 or w<=neww: continue
                if dh == 0 and dw==0: continue
                if s[newh][neww] : continue


                if abs(dh)+abs(dw)==1:
                    if dist[newh][neww] > d:
                        dist[newh][neww] = d
                        if newh == gh and neww == gw:
                            return d
                        heapq.heappush(q,(d,newh,neww))
                else:
                    if dist[newh][neww] > d+1:
                        dist[newh][neww] = d+1
                        heapq.heappush(q,(d+1,newh,neww))
    if dist[gh][gw] == INF:
        return -1
    else: return dist[gh][gw]
        
print(solve(h,w,sh,sw,gh,gw,s))

from collections import *

def main():
    H,W = map(int, input().split())
    ch,cw = map(int, input().split())
    dh,dw = map(int, input().split())
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [
        [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
        [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
        [0,-2],[0,-1],[0,0],[0,1],[0,2],\
        [1,-2],[1,-1],[1,0],[1,1],[1,2],\
        [2,-2],[2,-1],[2,0],[2,1],[2,2]
        ]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print(ans[dh][dw])
 
def __starting_point():
    main()
__starting_point()

from collections import deque
H, W = map(int, input().split())
c = tuple(int(x)+1 for x in input().split())
d = [int(x)+1 for x in input().split()]
maze = [[-1] * (W+4), [-1] * (W+4)]

for i in range(H):
    temp = []
    s0 = input()
    s0 = '11' + s0.replace('.', '0').replace('#', '1') + '11'
    for j, s in enumerate(s0):
        temp.append(-int(s))
    maze.append(temp)
maze.append([-1] * (W+4))
maze.append([-1] * (W+4))

def check55(i, j):
    cset = {(ii, jj) for ii in range(i-2, i+3) for jj in range(j-2, j+3) if maze[ii][jj]==0}
    cset = cset - {(i-1, j), (i, j), (i+1, j), (i, j-1), (i, j+1)}
    return cset

def check(i, j, delta):
    if delta[0] != 0:
        ii = i + 3 * delta[0]
        for jj in range(j-2, j+3):
            if maze[ii][jj]==0:
                dset.add((ii, jj))

    else:
        jj = j + 3 * delta[1]
        for ii in range(i-2, i+3):
            if maze[ii][jj]==0:
                dset.add((ii, jj))

    return

#bfs

k = 1
tempbfs = deque([[c[0], c[1], k]])
flag = 0
while tempbfs:
    if flag == 1:break
    p = tempbfs.popleft()

    #dfs
    if maze[p[0]][p[1]] != 0:continue
    maze[p[0]][p[1]] = p[2]
    if p[0] == d[0] and p[1] == d[1]:
        flag = 1
        k = p[2]
        break
    temp = [(p[0], p[1])]
    dset = set()
    while temp:
        p0 = temp.pop()
        for delta in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            y = p0[0] + delta[0]
            x = p0[1] + delta[1]
            if maze[y][x] != 0: continue
            maze[y][x] = p[2]
            if y == d[0] and x == d[1]:
                flag = 1
                k = p[2]
                break
            temp.append((y, x))
            check(p0[0], p0[1], delta)

    if flag == 1: break
    for l in dset:
        if maze[l[0]][l[1]] == 0:
            tempbfs.append([l[0], l[1], p[2]+1])
    dset = check55(p[0], p[1])
    for l in dset:
        tempbfs.append([l[0], l[1], p[2]+1])

print(k - 1 if flag else -1)

from collections import *

def main():
    H,W = map(int, input().split())
    ch,cw = map(int, input().split())
    dh,dw = map(int, input().split())
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [[-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]]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print(ans[dh][dw])
 
def __starting_point():
    main()
__starting_point()

from collections import deque

def main():
    h, w = list(map(int, input().split()))
    start_h, start_w = [int(x)+1 for x in input().split()]
    goal_h, goal_w = [int(x)+1 for x in input().split()]

    s = ["#"*(w+4)]
    s.append("#"*(w+4))
    for _ in range(h):
        s.append("##" + input() + "##")
    s.append("#"*(w+4))
    s.append("#"*(w+4))

    ans = [[-1]*(w+4) for _ in range(h+4)]
    for i in range(h+4):
        for j in range(w+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[start_h][start_w] = 0

    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),(-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),(0, -2), (0, -1), (0, 0), (0, 1), (0, 2),(1, -2), (1, -1), (1, 0), (1, 1), (1, 2),(2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

    yet = deque([(start_h, start_w)])
    done = deque()

    while yet:
        x1, y1 = yet.popleft()
        done.append((x1, y1))
        for (p, q) in move1:
            v1, v2 = x1+p, y1+q
            if ans[v1][v2] == -1: #"."でまだ辿り着いていないマス
                yet.append((v1, v2))
                ans[v1][v2] = ans[x1][y1]

        if len(yet) == 0:
            while done:
                x2, y2 = done.popleft()
                for (p, q) in move2:
                    i, j = x2+p, y2+q
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        yet.append((i, j))

    print((ans[goal_h][goal_w]))

def __starting_point():
    main()

__starting_point()

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

    y, x = list(map(int, input().split()))
    start = (x+1, y+1)

    y, x = list(map(int, input().split()))
    goal = (x+1, y+1)

    dist_table = [[-2] * (W+4)]
    dist_table.append([-2] * (W+4))
    for _ in range(H):
        row = '##' + input() + '##'
        row = [-1 if c == '.' else -2 for c in row]
        dist_table.append(row)
    dist_table.append([-2] * (W+4))
    dist_table.append([-2] * (W+4))



    sx, sy = start
    dist_table[sy][sx] = 0

    move1 = ((-1, 0), (1, 0), (0, 1), (0, -1))
    move2 = ((-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))

    queue1 = [start]
    queue2 = []

    while queue1:
        now_x, now_y = queue1.pop()
        if (now_x, now_y) == goal:
            break
        queue2.append((now_x, now_y))
        dist = dist_table[now_y][now_x]
        for dx, dy in move1:
            tx, ty = now_x + dx, now_y + dy
            if dist_table[ty][tx] == -1:
                dist_table[ty][tx] = dist
                queue1.append((tx, ty))
        if len(queue1) == 0:
            while queue2:
                now_x, now_y = queue2.pop()
                dist = dist_table[now_y][now_x]
                for dx, dy in move2:
                    tx, ty = now_x + dx, now_y + dy
                    if dist_table[ty][tx] == -1:
                        dist_table[ty][tx] = dist + 1
                        queue1.append((tx, ty))
    gx, gy = goal
    print((dist_table[gy][gx]))

def __starting_point():
    main()

__starting_point()

H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))

Ch += 1
Cw += 1
Dh += 1
Dw += 1

start = (Ch, Cw)

S = ['#'*(W+4)]
S.append('#'*(W+4))
for k in range(H):
  S.append('##' + input() + '##')
S.append('#'*(W+4))
S.append('#'*(W+4))

counter = [[-1 for _ in range(W+4)] for _ in range(H+4)]
for k in range(2, H+2):
  for j in range(2, W+2):
    if S[k][j] == '.':
      counter[k][j] = 10**7

counter[Ch][Cw] = 0

#print(counter)
#print(S)

now = [start]
now_2 = [start]
while len(now_2) > 0:
  new = []
  for item in now_2:
    x = item[0]
    y = item[1]
    if counter[x+1][y] == 10**7:
      counter[x+1][y] = 0
      new.append((x+1, y))
    if counter[x][y+1] == 10**7:
      counter[x][y+1] = 0
      new.append((x, y+1))
    if counter[x-1][y] == 10**7:
      counter[x-1][y] = 0
      new.append((x-1, y))
    if counter[x][y-1] == 10**7:
      counter[x][y-1] = 0
      new.append((x, y-1))
  now += new
  now_2 = new
#print(now)
#print(counter)
if counter[Dh][Dw] == 0:
  print((0))
  return
ans = 0
while counter[Dh][Dw] == 10**7:
  if len(now) == 0:
    print((-1))
    return 
  nextlist = []
  ans += 1
  for item in now:
    x = item[0]
    y = item[1]
    if counter[x+2][y+2] > ans:
      counter[x+2][y+2] = ans
      nextlist.append((x+2, y+2))
    if counter[x+2][y+1] > ans:
      counter[x+2][y+1] = ans
      nextlist.append((x+2, y+1))
    if counter[x+2][y] > ans:
      counter[x+2][y] = ans
      nextlist.append((x+2, y))
    if counter[x+2][y-1] > ans:
      counter[x+2][y-1] = ans
      nextlist.append((x+2, y-1))
    if counter[x+2][y-2] > ans:
      counter[x+2][y-2] = ans
      nextlist.append((x+2, y-2))
    if counter[x+1][y+2] > ans:
      counter[x+1][y+2] = ans
      nextlist.append((x+1, y+2))
    if counter[x+1][y-2] > ans:
      counter[x+1][y-2] = ans
      nextlist.append((x+1, y-2))
    if counter[x][y+2] > ans:
      counter[x][y+2] = ans
      nextlist.append((x, y+2))
    if counter[x][y-2] > ans:
      counter[x][y-2] = ans
      nextlist.append((x, y-2))
    if counter[x-2][y+2] > ans:
      counter[x-2][y+2] = ans
      nextlist.append((x-2, y+2))
    if counter[x-2][y+1] > ans:
      counter[x-2][y+1] = ans
      nextlist.append((x-2, y+1))
    if counter[x-2][y] > ans:
      counter[x-2][y] = ans
      nextlist.append((x-2, y))
    if counter[x-2][y-1] > ans:
      counter[x-2][y-1] = ans
      nextlist.append((x-2, y-1))
    if counter[x-2][y-2] > ans:
      counter[x-2][y-2] = ans
      nextlist.append((x-2, y-2))
    if counter[x-1][y+2] > ans:
      counter[x-1][y+2] = ans
      nextlist.append((x-1, y+2))
    if counter[x-1][y-2] > ans:
      counter[x-1][y-2] = ans
      nextlist.append((x-1, y-2))
    if counter[x-1][y-1] > ans:
      counter[x-1][y-1] = ans
      nextlist.append((x-1, y-1))
    if counter[x-1][y+1] > ans:
      counter[x-1][y+1] = ans
      nextlist.append((x-1, y+1))
    if counter[x+1][y+1] > ans:
      counter[x+1][y+1] = ans
      nextlist.append((x+1, y+1))
    if counter[x+1][y-1] > ans:
      counter[x+1][y-1] = ans
      nextlist.append((x+1, y-1))
      
  
  now_2 = nextlist
  while len(now_2) > 0:
    new = []
    for item in now_2:
      x = item[0]
      y = item[1]
      if counter[x+1][y] == 10**7:
        counter[x+1][y] = 0
        new.append((x+1, y))
      if counter[x][y+1] == 10**7:
        counter[x][y+1] = 0
        new.append((x, y+1))
      if counter[x-1][y] == 10**7:
        counter[x-1][y] = 0
        new.append((x-1, y))
      if counter[x][y-1] == 10**7:
        counter[x][y-1] = 0
        new.append((x, y-1))
    nextlist += new
    now_2 = new
  now = nextlist
  #print(now)
  if ans > 10**7:
    print((-1))
    return
print(ans)      
    


from collections import deque
 
 
# In[30]:
 
 
def main():
    H,W = map(int, input().split())
    ch,cw = map(int, input().split())
    dh,dw = map(int, input().split())
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [
        [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
        [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
        [0,-2],[0,-1],[0,0],[0,1],[0,2],\
        [1,-2],[1,-1],[1,0],[1,1],[1,2],\
        [2,-2],[2,-1],[2,0],[2,1],[2,2]
        ]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print(ans[dh][dw])
 
def __starting_point():
    main()
__starting_point()

from collections import deque
def main():
  h,w = map(int,input().split())
  ch,cw = map(lambda x:int(x)+1,input().split())
  dh,dw = map(lambda x:int(x)+1,input().split())
  s = [["#"]*(w+4) for _ in range(2)]+[["#"]*2+list(input())+["#"]*2 for _ in range(h)]+[["#"]*(w+4) for _ in range(2)]
  m1 = ((-1,0),(0,-1),(0,1),(1,0))
  m2 = [(i,j) for i in range(-2,3) for j in range(-2,3) if abs(i)+abs(j)>1]
  a = deque([(ch,cw)])
  b = deque()
  i = 0
  while a:
    while a:
      ah,aw = a.popleft()
      if s[ah][aw]!=".":
        continue
      b.append((ah,aw))
      s[ah][aw] = i
      for bh,bw in m1:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    while b:
      ah,aw = b.popleft()
      for bh,bw in m2:
        bh,bw = ah+bh,aw+bw
        if s[bh][bw]==".":
          a.append((bh,bw))
    i += 1
  ans = s[dh][dw]
  print(ans if ans != "." else -1)

def __starting_point():
    main()
__starting_point()

from collections import deque
#ワープなしで進んだ後にワープを行う
def main():
    H, W = map(int, input().split())
    ch, cw = map(int, input().split())
    dh, dw = map(int, input().split())
    # ######
    # ######
    # ##in##
    # ######
    # ######
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))

    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1
    cw += 1
    dh += 1
    dw += 1
    ans[ch][cw] = 0
    # 移動変位
    move = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    magic = [[-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]]
    # 位置
    mylist = deque([[ch, cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x, y = mylist.popleft()
        mylist2.append([x, y])
        for p, q in move:
            v1, v2 = x+p, y+q
            # 移動可能
            if ans[v1][v2] == -1:
                mylist.append([v1, v2])
                ans[v1][v2] = ans[x][y]
        # 動けなくなった場合
        if len(mylist) == 0:
            # 
            while len(mylist2) > 0:
                x2, y2 = mylist2.popleft()
                for v1, v2 in magic:
                    i, j = x2+v1, y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i, j])
    print(ans[dh][dw])
def __starting_point():
    main()
__starting_point()