from collections import deque INF = float("inf") h, w = map(int, input().split()) si, sj = map(lambda x:int(x)-1, input().split()) ti, tj = map(lambda x:int(x)-1, input().split()) G = [input() for _ in range(h)] D = [(-1, 0), (1, 0), (0, -1), (0, 1)] Q = deque([(si, sj, 0)]) while 1: i, j, d = Q.popleft() for di, dj in D: ni, nj = i + di, j + dj if not (0 <= ni < h and 0 <= nj < w): continue if G[ni][nj] == '#': continue if (ni, nj) == (ti, tj): print(d) exit(0) Q.append((di, dj, d + 1))