### BUGGY CODE ###

import numpy as np
from numba import njit, i8, u1, void, b1
import heapq
import math

@njit(void(i8,i8,i8,i8,b1[:,:]),cache=True)
def solve(ch,cw,dh,dw,road_map):

    h,w = road_map.shape

    queue = [[0,ch,cw]]
    used_map = np.full((h,w),-1,dtype=np.int64)

    layer_set = set()
    layer_set.add((ch,cw))
    loop = 0
    while used_map[dh][dw] == -1 and len(queue) != 0:
        #move walk
        while len(queue) != 0:
            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]] == True:
                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]])

        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] == False:
                            heapq.heappush(queue,[loop+1,s[0]+x,s[1]+y])
            
        loop+=1

    print(used_map[dh,dw])

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

solve(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))