import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 md = 10**9+7 # md = 998244353 from collections import deque h, w = LI() si, sj, gi, gj = LI1() ss = [SI() for _ in range(h)] mxs = [1]*w d0 = [[inf]*h for _ in range(w)] dd = [[inf]*2 for _ in range(h*w)] q = deque() for j in range(w): q.append((0, j, 0)) d0[j][0] = 0 while q: i, j, f = q.popleft() if i == 0: d = d0[j][f] ni = min(1+f//2, h-1) for nj in [j-1, j+1]: if nj >= w or nj < 0: continue if d < dd[ni*w+nj][1]: dd[ni*w+nj][1] = d q.appendleft((ni, nj, 1)) ni = min(1+f, h-1) for nj in [j-1, j+1]: if nj >= w or nj < 0: continue if d+1 < dd[ni*w+nj][1]: dd[ni*w+nj][1] = d+1 q.append((ni, nj, 1)) else: d = dd[i*w+j][f] if f: if ss[i][j] == "." and d < dd[i*w+j][0]: dd[i*w+j][0] = d q.appendleft((i, j, 0)) if i > 1 and d < dd[(i-1)*w+j][1]: dd[(i-1)*w+j][1] = d q.appendleft((i-1, j, 1)) else: ni = i+1 if ni < h: for nj in [j-1, j+1]: if nj < 0 or nj >= w: continue if ss[ni][nj] == "." and d < dd[ni*w+nj][0]: dd[ni*w+nj][0] = d q.appendleft((ni, nj, 0)) for nj in [j-1, j+1]: if nj < 0 or nj >= w: continue if d < d0[nj][i]: d0[nj][i] = d q.appendleft((0, nj, i)) # p2D(d0) # p2D(dd) ans = dd[gi*w+gj][0] if ans == inf: ans = -1 print(ans)