import sys

from collections import deque



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

si,sj=[int(x)-1 for x in input().split()]

ti,tj=[int(x)-1 for x in input().split()]

A=list(sys.stdin)



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

dist=[[10**6]*W for _ in range(H)]

dist[si][sj]=0

dq=deque()

dq.append((0,si,sj))

ok=False

while dq:

    k,i,j=dq.popleft()

    if k>dist[i][j]:

        continue

    if i==ti and j==tj:

        ok=True

        break

    for di in dirs:

        for dj in dirs:

            if di==dj==0:

                continue

            ni,nj=i+di,j+dj

            if not (0<=ni<H and 0<=nj<W):

                continue

            if A[ni][nj]=='#':

                continue

            if abs(di)+abs(dj)==1:

                append=dq.appendleft

                nk=k

            else:

                append=dq.append

                nk=k+1

            if k>=dist[ni][nj]:

                continue

            dist[ni][nj]=nk

            append((nk,ni,nj))

      

if ok:

    ans=dist[ti][tj]

else:

    ans=-1



print(ans)

import sys

from collections import deque



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

si,sj=[int(x)-1 for x in input().split()]

ti,tj=[int(x)-1 for x in input().split()]

A=[[*next(sys.stdin)] for _ in range(H)]



N=-1

for i in range(H):

    for j in range(W):

        if A[i][j]!='.':

            continue

        N+=1

        stk=[(i,j)]

        while stk:

            ii,jj=stk.pop()

            A[ii][jj]=N

            for ni,nj in (ii-1,jj),(ii,jj+1),(ii+1,jj),(ii,jj-1):

                if not (0<=ni<H and 0<=nj<W):

                    continue

                if A[ni][nj]!='.':

                    continue

                stk.append((ni,nj))



s=t=-1

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

adj=[{*()} for _ in range(N+1)]

for i in range(H):

    for j in range(W):

        u=A[i][j]

        if u=='#':

            continue

        if i==si and j==sj:

            s=u

        elif i==ti and j==tj:

            t=u

        for di in dirs:

            for dj in dirs:

                if di==dj==0:

                    continue

                ni,nj=i+di,j+dj

                if not (0<=ni<H and 0<=nj<W):

                    continue

                v=A[ni][nj]

                if v=='#':

                    continue

                if u!=v:

                    adj[u].add(v)

                    adj[v].add(u)



que=deque()                

que.append(s)

vis={s}

m=-1

ok=False

while que:

    m+=1

    for _ in range(len(que)):

        u=que.popleft()

        if u==t:

            ok=True

            break

        for v in adj[u]:

            if v in vis:

                continue

            vis.add(v)

            que.append(v)

    if ok:

        break



if ok:

    print(m)

else:

    print((-1))

import sys
from collections import deque

H, W = list(map(int, input().split()))
si, sj = [int(x) - 1 for x in input().split()]
ti, tj = [int(x) - 1 for x in input().split()]
A = list(sys.stdin)
dirs = [-2, -1, 0, 1, 2]
dist = [[10**6] * W for _ in range(H)]
dist[si][sj] = 0
dq = deque()
dq.append((0, si, sj))
ok = False
while dq:
    k, i, j = dq.popleft()
    if k > dist[i][j]:
        continue
    if i == ti and j == tj:
        ok = True
        break
    for di in dirs:
        for dj in dirs:
            if di == dj == 0:
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < H and 0 <= nj < W):
                continue
            if A[ni][nj] == "#":
                continue
            if abs(di) + abs(dj) == 1:
                append = dq.appendleft
                nk = k
            else:
                append = dq.append
                nk = k + 1
            if k >= dist[ni][nj]:
                continue
            dist[ni][nj] = nk
            append((nk, ni, nj))
if ok:
    ans = dist[ti][tj]
else:
    ans = -1
print(ans)

import sys
from collections import deque

H, W = list(map(int, input().split()))
si, sj = [int(x) - 1 for x in input().split()]
ti, tj = [int(x) - 1 for x in input().split()]
A = [[*next(sys.stdin)] for _ in range(H)]
N = -1
for i in range(H):
    for j in range(W):
        if A[i][j] != ".":
            continue
        N += 1
        stk = [(i, j)]
        while stk:
            ii, jj = stk.pop()
            A[ii][jj] = N
            for ni, nj in (ii - 1, jj), (ii, jj + 1), (ii + 1, jj), (ii, jj - 1):
                if not (0 <= ni < H and 0 <= nj < W):
                    continue
                if A[ni][nj] != ".":
                    continue
                stk.append((ni, nj))
s = t = -1
dirs = [-2, -1, 0, 1, 2]
adj = [{*()} for _ in range(N + 1)]
for i in range(H):
    for j in range(W):
        u = A[i][j]
        if u == "#":
            continue
        if i == si and j == sj:
            s = u
        elif i == ti and j == tj:
            t = u
        for di in dirs:
            for dj in dirs:
                if di == dj == 0:
                    continue
                ni, nj = i + di, j + dj
                if not (0 <= ni < H and 0 <= nj < W):
                    continue
                v = A[ni][nj]
                if v == "#":
                    continue
                if u != v:
                    adj[u].add(v)
                    adj[v].add(u)
que = deque()
que.append(s)
vis = {s}
m = -1
ok = False
while que:
    m += 1
    for _ in range(len(que)):
        u = que.popleft()
        if u == t:
            ok = True
            break
        for v in adj[u]:
            if v in vis:
                continue
            vis.add(v)
            que.append(v)
    if ok:
        break
if ok:
    print(m)
else:
    print((-1))

-A = list(sys.stdin) +A = [[*next(sys.stdin)] for _ in range(H)] +N = -1 +for i in range(H): +    for j in range(W): +        if A[i][j] != ".": +            continue +        N += 1 +        stk = [(i, j)] +        while stk: +            ii, jj = stk.pop() +            A[ii][jj] = N +            for ni, nj in (ii - 1, jj), (ii, jj + 1), (ii + 1, jj), (ii, jj - 1): +                if not (0 <= ni < H and 0 <= nj < W): +                    continue +                if A[ni][nj] != ".": +                    continue +                stk.append((ni, nj)) +s = t = -1 -dist = [[10**6] * W for _ in range(H)] -dist[si][sj] = 0 -dq = deque() -dq.append((0, si, sj)) +adj = [{*()} for _ in range(N + 1)] +for i in range(H): +    for j in range(W): +        u = A[i][j] +        if u == "#": +            continue +        if i == si and j == sj: +            s = u +        elif i == ti and j == tj: +            t = u +        for di in dirs: +            for dj in dirs: +                if di == dj == 0: +                    continue +                ni, nj = i + di, j + dj +                if not (0 <= ni < H and 0 <= nj < W): +                    continue +                v = A[ni][nj] +                if v == "#": +                    continue +                if u != v: +                    adj[u].add(v) +                    adj[v].add(u) +que = deque() +que.append(s) +vis = {s} +m = -1 -while dq: -    k, i, j = dq.popleft() -    if k > dist[i][j]: -        continue -    if i == ti and j == tj: -        ok = True +while que: +    m += 1 +    for _ in range(len(que)): +        u = que.popleft() +        if u == t: +            ok = True +            break +        for v in adj[u]: +            if v in vis: +                continue +            vis.add(v) +            que.append(v) +    if ok: -    for di in dirs: -        for dj in dirs: -            if di == dj == 0: -                continue -            ni, nj = i + di, j + dj -            if not (0 <= ni < H and 0 <= nj < W): -                continue -            if A[ni][nj] == "#": -                continue -            if abs(di) + abs(dj) == 1: -                append = dq.appendleft -                nk = k -            else: -                append = dq.append -                nk = k + 1 -            if k >= dist[ni][nj]: -                continue -            dist[ni][nj] = nk -            append((nk, ni, nj)) -    ans = dist[ti][tj] +    print(m) -    ans = -1 -print(ans) +    print((-1))