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



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

rows,cols = h,w



OK = "."

NG = "#"



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

@njit((b1[:, :],i8,i8,i8,i8), cache=True)

def main(ss,ch,cw,dh,dw):

    inf = 1<<30

    start = ch*w+cw

    goal = dh*w+dw

    n = h*w

    seen = np.full(n, inf, np.int64)

    seen[start] = 0

    q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置

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

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

#     dx = []

#     dy = []

#     vvs = []

#     for i in range(-2,3):

#         for j in range(-2,3):

#             if not (i==j==0):

#                 dx.append(i)

#                 dy.append(j)

#                 if abs(i)+abs(j)>1:

#                     vvs.append(1)

#                 else:

#                     vvs.append(0)

    while q:

#         print(seen)

        pnum,pu = hpp(q)

        ux,uy = divmod(pu,w)

        # print(pnum,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

#         for xx,yy,vv in zip(dx,dy,vvs):

    #         x = ux+xx

    #         y = uy+yy

                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

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

import numpy as np

import numba

from numba import njit, b1, i4, i8, f8

from heapq import *



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



@njit((b1[:, :], i8, i8, i8, i8), cache=True)

def main(wall, Sx, Sy, Tx, Ty):

    INF = 1 << 30

    H, W = wall.shape

    S = Sx * W + Sy

    T = Tx * W + Ty

    dist = np.full(H * W, INF, np.int64)

    q = [(0,S)]

    while q:

        dv, v = heappop(q)

        if dv > dist[v]:

            continue

        vx, vy = divmod(v, W)

        # そのままあるく

        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):

            wx, wy = vx + dx, vy + dy

            if not (0 <= wx < H):

                continue

            if not (0 <= wy < W):

                continue

            if wall[wx, wy]:

                continue

            w = wx * W + wy

            if dist[w] <= dv:

                continue

            dist[w] = dv

            heappush(q, (dv, w))

        # ワープする

        for dx in range(-2, 3):

            for dy in range(-2, 3):

                wx, wy = vx + dx, vy + dy

                if not (0 <= wx < H):

                    continue

                if not (0 <= wy < W):

                    continue

                if wall[wx, wy]:

                    continue

                w = wx * W + wy

                if dist[w] <= dv + 1:

                    continue

                dist[w] = dv + 1

                heappush(q, (dv+1, w))

    ans = dist[T]

    if ans == INF:

        ans = -1

    return ans



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

Sx, Sy = [int(x) - 1 for x in readline().split()]

Tx, Ty = [int(x) - 1 for x in readline().split()]

wall = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W] == b'#'



print((main(wall, Sx, Sy, Tx, Ty)))

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

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
rows, cols = h, w
OK = "."
NG = "#"
ss = np.array([[c == "#" for c in eval(input())] for _ in range(h)])


@njit((b1[:, :], i8, i8, i8, i8), cache=True)
def main(ss, ch, cw, dh, dw):
    inf = 1 << 30
    start = ch * w + cw
    goal = dh * w + dw
    n = h * w
    seen = np.full(n, inf, np.int64)
    seen[start] = 0
    q = [(0, start)]  # ワープ回数, 現在位置, 最後の道の位置
    # dx = [0,1,0,-1]
    # dy = [1,0,-1,0]
    #     dx = []
    #     dy = []
    #     vvs = []
    #     for i in range(-2,3):
    #         for j in range(-2,3):
    #             if not (i==j==0):
    #                 dx.append(i)
    #                 dy.append(j)
    #                 if abs(i)+abs(j)>1:
    #                     vvs.append(1)
    #                 else:
    #                     vvs.append(0)
    while q:
        #         print(seen)
        pnum, pu = hpp(q)
        ux, uy = divmod(pu, w)
        # print(pnum,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
                #         for xx,yy,vv in zip(dx,dy,vvs):
                #         x = ux+xx
                #         y = uy+yy
                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


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

import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
from heapq import *

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


@njit((b1[:, :], i8, i8, i8, i8), cache=True)
def main(wall, Sx, Sy, Tx, Ty):
    INF = 1 << 30
    H, W = wall.shape
    S = Sx * W + Sy
    T = Tx * W + Ty
    dist = np.full(H * W, INF, np.int64)
    q = [(0, S)]
    while q:
        dv, v = heappop(q)
        if dv > dist[v]:
            continue
        vx, vy = divmod(v, W)
        # そのままあるく
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            wx, wy = vx + dx, vy + dy
            if not (0 <= wx < H):
                continue
            if not (0 <= wy < W):
                continue
            if wall[wx, wy]:
                continue
            w = wx * W + wy
            if dist[w] <= dv:
                continue
            dist[w] = dv
            heappush(q, (dv, w))
        # ワープする
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                wx, wy = vx + dx, vy + dy
                if not (0 <= wx < H):
                    continue
                if not (0 <= wy < W):
                    continue
                if wall[wx, wy]:
                    continue
                w = wx * W + wy
                if dist[w] <= dv + 1:
                    continue
                dist[w] = dv + 1
                heappush(q, (dv + 1, w))
    ans = dist[T]
    if ans == INF:
        ans = -1
    return ans


H, W = list(map(int, readline().split()))
Sx, Sy = [int(x) - 1 for x in readline().split()]
Tx, Ty = [int(x) - 1 for x in readline().split()]
wall = np.frombuffer(read(), "S1").reshape(H, -1)[:, :W] == b"#"
print((main(wall, Sx, Sy, Tx, Ty)))

+import numpy as np +import numba +from numba import njit, b1, i4, i8, f8 +from heapq import * -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 - -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 -rows, cols = h, w -OK = "." -NG = "#" -ss = np.array([[c == "#" for c in eval(input())] for _ in range(h)]) +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines -def main(ss, ch, cw, dh, dw): -    inf = 1 << 30 -    start = ch * w + cw -    goal = dh * w + dw -    n = h * w -    seen = np.full(n, inf, np.int64) -    seen[start] = 0 -    q = [(0, start)]  # ワープ回数, 現在位置, 最後の道の位置 -    # dx = [0,1,0,-1] -    # dy = [1,0,-1,0] -    #     dx = [] -    #     dy = [] -    #     vvs = [] -    #     for i in range(-2,3): -    #         for j in range(-2,3): -    #             if not (i==j==0): -    #                 dx.append(i) -    #                 dy.append(j) -    #                 if abs(i)+abs(j)>1: -    #                     vvs.append(1) -    #                 else: -    #                     vvs.append(0) +def main(wall, Sx, Sy, Tx, Ty): +    INF = 1 << 30 +    H, W = wall.shape +    S = Sx * W + Sy +    T = Tx * W + Ty +    dist = np.full(H * W, INF, np.int64) +    q = [(0, S)] -        #         print(seen) -        pnum, pu = hpp(q) -        ux, uy = divmod(pu, w) -        # print(pnum,divmod(pu,w)) -        if pu == goal: -            break -        for xx in range(-2, 3): -            for yy in range(-2, 3): -                if xx == yy == 0: +        dv, v = heappop(q) +        if dv > dist[v]: +            continue +        vx, vy = divmod(v, W) +        # そのままあるく +        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)): +            wx, wy = vx + dx, vy + dy +            if not (0 <= wx < H): +                continue +            if not (0 <= wy < W): +                continue +            if wall[wx, wy]: +                continue +            w = wx * W + wy +            if dist[w] <= dv: +                continue +            dist[w] = dv +            heappush(q, (dv, w)) +        # ワープする +        for dx in range(-2, 3): +            for dy in range(-2, 3): +                wx, wy = vx + dx, vy + dy +                if not (0 <= wx < H): -                if abs(xx) + abs(yy) <= 1: -                    vv = 0 -                else: -                    vv = 1 -                #         for xx,yy,vv in zip(dx,dy,vvs): -                #         x = ux+xx -                #         y = uy+yy -                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]: +                if not (0 <= wy < W): -                #         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 +                if wall[wx, wy]: +                    continue +                w = wx * W + wy +                if dist[w] <= dv + 1: +                    continue +                dist[w] = dv + 1 +                heappush(q, (dv + 1, w)) +    ans = dist[T] +    if ans == INF: +        ans = -1 +    return ans -print((main(ss, ch, cw, dh, dw))) +H, W = list(map(int, readline().split())) +Sx, Sy = [int(x) - 1 for x in readline().split()] +Tx, Ty = [int(x) - 1 for x in readline().split()] +wall = np.frombuffer(read(), "S1").reshape(H, -1)[:, :W] == b"#" +print((main(wall, Sx, Sy, Tx, Ty)))