### BUGGY CODE ###

from queue import deque

h,w = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
inf = 10**10

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

li = [["" for i in range(w)]for j in range(h)]
for i in range(h):
    s = input()
    for j in range(w):
        li[i][j] = s[j]

dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque()
bomb_count = 0

mx = []
my = []

for i in range(-2,3):
    for j in range(-2,3):
        if i == 0 and j == 0:
            continue
        mx.append(i)
        my.append(j)

lin = [[inf for i in range(w)]for j in range(h)]
lin[ch][cw] = 0#ここをスタートの座標に変えること
que.append((ch,cw))#ここもスタートの座標に変えること

while que:
    k = que.popleft()
    x = k[0]
    y = k[1]

    for i,j in zip(mx,my):
        nx = x + i
        ny = y + j
        if nx < 0 or nx >= h or ny < 0 or ny >= w:
            continue
        elif li[nx][ny] == "#":
            continue
        else:
            if [i,j] in [[-1,0],[1,0],[0,-1],[0,1]]:
                if lin[nx][ny] > lin[x][y]:
                    que.append((nx,ny))
                    lin[nx][ny] = lin[x][y]# 直接行ける
            else:
                if lin[nx][ny] > lin[x][y]+1:
                    que.append((nx,ny))
                    lin[nx][ny] = lin[x][y]+1

if lin[dh][dw] == inf:
    print(-1)
else:
    print(lin[dh][dw])

### FIXED CODE ###

from queue import deque
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

h,w = map(int,stdin.readline().rstrip().split())
ch,cw = map(int,stdin.readline().rstrip().split())
dh,dw = map(int,stdin.readline().rstrip().split())
inf = 10**10

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

def main():
    li = [["" for i in range(w)]for j in range(h)]

    for i in range(h):
        s = stdin.readline().rstrip()
        for j in range(w):
            li[i][j] = s[j]

    que = deque()

    mx = []
    my = []

    for i in range(-2,3):
        for j in range(-2,3):
            if i == 0 and j == 0:
                continue
            mx.append(i)
            my.append(j)

    lin = [[inf for i in range(w)]for j in range(h)]
    lin[ch][cw] = 0#ここをスタートの座標に変えること
    que.append((ch,cw))#ここもスタートの座標に変えること

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

    while que:
        k = que.popleft()
        x = k[0]
        y = k[1]
        no_magic = set()#魔法を使わずに行ける

        for i,j in zip(dx,dy):
            nx = x + i
            ny = y + j

            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            elif li[nx][ny] == "#":
                continue
            else:
                if lin[nx][ny] > lin[x][y]:
                    que.appendleft((nx,ny))
                    no_magic.add((nx,ny))
                    lin[nx][ny] = lin[x][y]# 直接行ける
        
        for i,j in zip(mx,my):
            nx = x + i
            ny = y + j

            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            elif li[nx][ny] == "#":
                continue
            else:
                if lin[nx][ny] > lin[x][y]+1:
                    if (nx,ny) not in no_magic:
                        que.append((nx,ny))
                    lin[nx][ny] = lin[x][y]+1# 魔法を使っていける

    if lin[dh][dw] == inf:
        print(-1)
    else:
        print(lin[dh][dw])

if __name__ == "__main__":
    main()