### BUGGY CODE ###

h,w = map(int, input().split())
sy,sx = map(int, input().split())
gy,gx = map(int, input().split())
l = [None]*h
for i in range(h):
    l[i] = list(input())

from heapq import heappush, heappop,heappushpop,heapify
from collections import defaultdict
link = defaultdict(list)
chg=10**4

for y in range(h):
    for x in range(w):
        if l[y][x]=="#":
            continue
        frm = y*chg+x
        dx = [1, 0, -1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]
            if 0 <= nx < w and 0 <= ny < h and l[ny][nx]==".":
                to=ny*chg+nx
                link[frm].append([to,0])

        for dx in [-2,-1,0,1,2]:
            for dy in [-2,-1,0,1,2]:
                if abs(dx)+abs(dy)<=1:
                    continue
                nx=x+dx
                ny=y+dy
                if 0 <= nx < w and 0 <= ny < h and l[ny][nx]==".":
                    to=ny*chg+nx
                    link[frm].append([to,1])

def dks(g,start):
    visited = set()
    hq = []
    heappush(hq, (0,start))

    cost = defaultdict(lambda: float("inf"))
    while hq:
        shortest, i = heappop(hq)
        if i in visited:
            continue
        visited.add(i)
        cost[i] = shortest
        for j, t in g[i]:
            if j in visited:
                continue
            heappush(hq, (shortest + t, j))
    return cost

start = (sy-1)*chg+sx-1
goal = (gy-1)*chg+gx-1
ret=dks(link, start)
if goal in ret:
    print(ret[goal])
else:
    print(-1)

### FIXED CODE ###

h,w = map(int, input().split())
ch,hw = map(int, input().split())
dh,dw = map(int, input().split())

sy,sx=ch-1,hw-1
gx,gy=dw-1,dh-1

l = [None]*h
for i in range(h):
    l[i] = list(input())

dist = [[-1]*w for i in range(h)]
dist[sy][sx] = 0

from collections import deque
def walk(warped,cnt):
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    Q = deque(warped)
    walked=warped
    changed=False
    while Q:
        p = Q.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]

            if 0 <= nx < w and 0 <= ny < h:
                if l[ny][nx]=="." and dist[ny][nx]==-1:
                    changed=True
                    Q.append([nx, ny])
                    walked.append([nx,ny])
                    dist[ny][nx] = cnt
                    if nx==gx and ny==gy:
                        print(cnt)
                        exit()
    return walked,changed
def warp(walked,cnt):
    warped=[]
    changed=False

    for x,y in walked:
        for dx in [-2,-1,0,1,2]:
            for dy in [-2,-1,0,1,2]:
                if abs(dx)+abs(dy)<=1:
                    continue

                nx=x+dx
                ny=y+dy
                if 0 <= nx < w and 0 <= ny < h:
                    if l[ny][nx]=="." and dist[ny][nx]==-1:
                        changed=True
                        warped.append([nx,ny])
                        dist[ny][nx] = cnt
                        if nx==gx and ny==gy:
                            print(cnt)
                            exit()
    return warped,changed

cnt=0

warped=[[sx,sy]]

while True:
    walked,walkchange= walk(warped,cnt)
    cnt+=1
    warped,warpchange=warp(walked,cnt)
    if not walkchange and not warpchange:
        print(-1)
        exit()