import sys import heapq n, m = map(int, input().split()) xs, ys, xt, yt = map(int, input().split()) g = [['*'] for _ in range(n + 1)] # 索引0不使用 for i in range(1, n + 1): g[i] += input() g[i] += '*' g[0] = ['*'] * (m + 2) g.append(['*'] * (m + 2)) if g[xs][ys] == '*' or g[xt][yt] == '*': print(-1) sys.exit() current = [(0,(xs,ys))] already = {(xs,ys)} while current: dist,node = heapq.heappop(current) if node == (xt,yt): print(dist) sys.exit() x,y = node around = {(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)} # 上下左右 for x0,y0 in around: if g[x0][y0] == '.' and (x0,y0) not in already: already.add((x0,y0)) heapq.heappush(current,(dist+1,(x0, y0))) print(-1)