### BUGGY CODE ###

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

h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: list(x.decode()), readlines()))
ch-=1; cw-=1; dh-=1; dw-=1

def valid(i, j):
    if 0<=i<=h-1 and 0<=j<=w-1:
        return True
    else:
        return False

def bfs():
    que=[]
    m[ch][cw]=str(0)
    que.append((ch,cw))
    
    while que!=[]:
        i, j=que.pop(0)
        for si in range(-2, 3):
            for sj in range(-2, 3):
                f=False
                if valid(i+si, j+sj) and m[i+si][j+sj]!='#':
                    if (si, sj)==(0,0):
                        pass
                    elif (si, sj)==(-1, 0) or (si, sj)==(0, -1) or (si, sj)==(1, 0) or (si, sj)==(0, 1):
                        if m[i+si][j+sj]=='.':
                            m[i+si][j+sj]=m[i][j]
                            f=True
                        else:
                            if int(m[i+si][j+sj])>int(m[i][j]):
                                m[i+si][j+sj]=m[i][j]
                                f=True
                    else:
                        if m[i+si][j+sj]=='.':
                            m[i+si][j+sj]=str(int(m[i][j])+1)
                            f=True
                        else:
                            if int(m[i+si][j+sj])>int(m[i][j])+1:
                                m[i+si][j+sj]=str(int(m[i][j])+1)
                                f=True
                if f:
                    que.append((i+si, j+sj))

bfs()

if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])

### FIXED CODE ###

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

# ---------標準入力時--------------
h,w=map(int,readline().split())
ch, cw=map(int,readline().split())
dh, dw=map(int,readline().split())
m=list(map(lambda x: list(x.decode()), readlines()))
# ---------標準入力時--------------

ch-=1; cw-=1; dh-=1; dw-=1

ad = [(-1,0), (0,-1), (1,0), (0,1)]
warp = [(-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)]

for i in range(h):
    m[i] = ['#']*2 + m[i] + ['#']*2
    
m = [['#']*(w+4)]*2 + m + [['#']*(w+4)]*2
ch+=2; cw+=2; dh+=2; dw+=2

def bfs4():
    '''
    bfs3とほぼ同じ. 迷路の上下左右に2列ずつ壁追加
    '''
    que1=[]
    que2=[] # queueというよりはメモリ
    m[ch][cw]=str(0)
    que1.append((ch,cw))

    while que1!=[]:
        l1=len(que1)
        r1=0 # que1における使用済みindex

        # que1の要素(座標)から歩いていけるマス
        while r1<l1:
            i, j=que1[r1]
            r1+=1
            # 隣接したマスへの移動
            for si, sj in ad:
                if m[i+si][j+sj]=='.': 
                    m[i+si][j+sj]=m[i][j]
                    que1.append((i+si, j+sj))
                    l1+=1

        # 歩いていけないマス
        for i,j in que1:
            for si, sj in warp:
                if m[i+si][j+sj]=='.':
                    m[i+si][j+sj]=str(int(m[i][j])+1)
                    que2.append((i+si, j+sj))

        que1=que2
        que2=[]
###---------------------------------------

bfs4()


if m[dh][dw] == '.':
    print(-1)
else:
    print(m[dh][dw])