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 = "#"



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

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

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



print((main(ss,ch,cw,dh,dw)))
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), cache=True)

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

      

import sys

input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(max(1000, 10**9))

write = lambda x: sys.stdout.write(x+"\n")



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)])



print((main(ss,ch,cw,dh,dw)))
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 = "#"


@njit((b1[:, :], i8, i8, i8, i8), cache=True)
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


ss = np.array([[c == "#" for c in eval(input())] for _ in range(h)])
print((main(ss, ch, cw, dh, dw)))

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), cache=True)
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


import sys

input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x + "\n")
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)])
print((main(ss, ch, cw, dh, dw)))

-import sys - -input = lambda: sys.stdin.readline().rstrip() -sys.setrecursionlimit(max(1000, 10**9)) -write = lambda x: sys.stdout.write(x + "\n") - -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 = "#" +import sys + +input = lambda: sys.stdin.readline().rstrip() +sys.setrecursionlimit(max(1000, 10**9)) +write = lambda x: sys.stdout.write(x + "\n") +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 = "#"