### 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()))
# ---------標準入力時--------------

# ---------ファイル入力時--------------
# with open('maze.txt', 'r') as f:
#     h,w=map(int,f.readline().split())
#     ch, cw=map(int,f.readline().split())
#     dh, dw=map(int,f.readline().split())
#     m=list(map(list, f.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

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

def bfs2():
    '''
    bfs1の移動の順番を変えただけ
    '''
    que=[]
    m[ch][cw]=str(0)
    que.append((ch,cw))
    
    while que!=[]:
        i, j=que.pop(0)

        # 隣接したマスへの移動
        for si, sj in ad:
            f=False
            if valid(i+si, j+sj) and m[i+si][j+sj]!='#':
                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
            if f:
                que.append((i+si, j+sj))

        for si, sj in warp:
            f=False
            if valid(i+si, j+sj) and m[i+si][j+sj]!='#':
                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))
###---------------------------------------

bfs2()

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