### BUGGY CODE ###

import heapq
from numba import jit
h,w = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
road_map = []
for i in range(h):
    road = input()
    road_map.append(road)

@jit
def solve(h,w,ch,cw,dh,dw,road_map):

    queue = []
    heapq.heappush(queue,[0,ch-1,cw-1])

    used_map = [[-1]*w for _ in range(h)]
    layer_set = set()
    layer_set.add((ch-1,cw-1))
    loop = 0
    while used_map[dh-1][dw-1] == -1 and queue != []:
        #move walk
        while queue != []:
            dot = heapq.heappop(queue)
            if dot[1] < 0 or dot[2] < 0 or dot[1] >= h or dot[2] >= w:
                continue
            if used_map[dot[1]][dot[2]] > -1 and used_map[dot[1]][dot[2]] <= dot[0]:
                continue
            if road_map[dot[1]][dot[2]] == "#":
                continue
            used_map[dot[1]][dot[2]] = dot[0]
            layer_set.add((dot[1],dot[2]))

            if not (dot[2] - 1 < 0):
                heapq.heappush(queue,[dot[0],dot[1],dot[2]-1])
            if not (dot[2] + 1 >= w):
                heapq.heappush(queue,[dot[0],dot[1],dot[2]+1])
            if not (dot[1]+1 >= h):
                heapq.heappush(queue,[dot[0],dot[1]+1,dot[2]])
            if not (dot[1]-1 < 0):
                heapq.heappush(queue,[dot[0],dot[1]-1,dot[2]])

        temp = []
        for s in layer_set:
            for x in range(-2,3,1):
                for y in range(-2,3,1):
                    if not(s[0]+ x < 0 or s[1]+y < 0 or s[0]+x >= h or s[1]+y >= w):
                        if used_map[s[0]+x][s[1]+y] == -1 and road_map[s[0]+x][s[1]+y] == ".":
                            temp = [s[0]+x,s[1]+y]
                            heapq.heappush(queue,[loop+1,temp[0],temp[1]])
            
        loop+=1

    print(used_map[dh-1][dw-1])

solve(h,w,ch,cw,dh,dw,road_map)

### FIXED CODE ###

#shotoyooさんのコードをお借りしてテスト

import numpy as np
from numba import njit, b1, i4, i8, f8
from heapq import heappop as hpp, heappush as hp

@njit((b1[:, :],i8,i8,i8,i8), cache=True)
def main(ss,ch,cw,dh,dw):
    inf = 1<<30
    h,w = ss.shape
    start = ch*w+cw
    goal = dh*w+dw
    n = h*w
    seen = np.full(n, inf, np.int64)
    seen[start] = 0
    q = [(0, start)] # ワープ回数, 現在位置, 最後の道の位置
    while q:
        pnum,pu = hpp(q)
        ux,uy = divmod(pu,w)
        if pu==goal:
            break
        for xx in range(-2, 3):
            for yy in range(-2, 3):
                if xx==yy==0:
                    continue
                if abs(xx)+abs(yy)<=1:
                    vv = 0
                else:
                    vv = 1
                x,y = ux+xx, uy+yy
                u = x*w + y
                num = pnum+vv
                if x<0 or y<0 or x>=h or y>=w or ss[x][y]:
                    continue
        #         print(x,y)
                if seen[u]>num:
                    seen[u] = num
                    hp(q,(num, u))
                
    val = seen[goal]
    if val==inf:
        return -1
    else:
        return val
      

h,w = map(int, input().split())
ch,cw = map(int, input().split())
dh,dw = map(int, input().split())
ch -= 1
cw -= 1
dh -= 1
dw -= 1
ss = np.array([[c=="#" for c in input()] for _ in range(h)])

print(main(ss,ch,cw,dh,dw))