### BUGGY CODE ###

class P:
    def __init__(self, c):
        self.c = c
        self.reached = False
        self.searched = False
        self.checked = False

from collections import deque
H, W = [int(i) for i in input().split()]
Ch, Cw = [int(i)-1 for i in input().split()]
Dh, Dw = [int(i)-1 for i in input().split()]
S = [[None for _ in range(H)] for _ in range(W)]
for y in range(H):
    tmp = input()
    for x in range(W):
        S[x][y] = P(tmp[x])

def neighbors(x, y, d):
    for i in range(max(0, x-1), min(W, x+2)):
        if S[i][y].c == '.' and S[i][y].reached == False:
            d.append((i, y))
    for i in range(max(0, y-1), min(H, y+2)):
        if S[x][i].c == '.' and S[x][i].reached == False:
            d.append((x, i))

def jump(x, y, d):
    S[x][y].checked = True
    for i in range(max(0, x-2), min(W, x+3)):
        for j in range(max(0, y-2), min(H, y+3)):
            if  S[i][j].c == '.' and S[i][j].searched == False and S[i][j].reached == False:
                d.append((i, j))
            S[i][j].searched = True


def BFS(x, y):
    d = deque([(x, y)])
    route = []
    while True:
        x, y = d.popleft()
        route.append((x, y))
        S[x][y].reached = True
        neighbors(x, y, d)
        if len(d) == 0:
            break
    return route


counter = 0
if Cw == Dw and Ch == Dh:
    print(0)
    exit()
q = deque([(Cw, Ch)])
flag = False
while True:
    array = []
    while len(q) > 0:
        x, y = q.popleft()
        if S[x][y].reached:
            pass
        else:
            array.append(BFS(x, y))
    if S[Dw][Dh].reached == True:
        break
    counter += 1
    for each in array:
        for a in each:
            if S[a[0]][a[1]].reached and S[a[0]][a[1]].checked == False:
                jump(a[0], a[1], q)
    if len(q) == 0:
        flag = True
        break
if flag:
    print(-1)
else:
    print(counter)
    

### FIXED CODE ###

#!/usr/bin/env python3
import sys
import collections
import itertools
 
def main():
    input = sys.stdin.readline
    h, w = map(int, input().split())
    ch, cw = map(lambda x:int(x) - 1, input().split())
    dh, dw = map(lambda x:int(x) - 1, input().split())
    s = [list(input().rstrip()) for _ in range(h)]
    
    que = collections.deque()
    que.append((ch, cw))
    cost = [[float("INF")] * w for _ in range(h)]
    cost[ch][cw] = 0
    
    while que:
        py, px = que.popleft()
        for dy, dx in itertools.product(range(-2, 3), repeat = 2):
            ny, nx = py + dy, px + dx
            if 0 <= ny < h and 0 <= nx < w and s[ny][nx] == ".":
                if dy == dx == 0:
                    continue
                if (dy, dx) in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
                    if cost[ny][nx] > cost[py][px]:
                        cost[ny][nx] = cost[py][px]
                        que.appendleft((ny, nx))
                else:
                    if cost[ny][nx] > cost[py][px] + 1:
                        cost[ny][nx] = cost[py][px] + 1
                        que.append((ny, nx))
                        
    if cost[dh][dw] == float("INF"):
        print(-1)
    else:
        print(cost[dh][dw])
    
if __name__ == '__main__':
    main()