from collections import deque


R, C = map(int, input().split())
sy, sx = (int(t) - 1 for t in input().split())
gy, gx = (int(t) - 1 for t in input().split())
c: list = [input() for _ in range(R)]

d = [[-1 for _ in range(C)] for _ in range(R)]
q: deque = deque()
q.append((sy, sx))
d[sy][sx] = 0

while q:
    y, x = q.popleft()
    n_yx = [(y - 1, x), (y, x + 1), (y + 1, x), (y, x - 1)]
    for ny, nx in n_yx:
        if c[ny][nx] == "." and d[ny][nx] < 0:
            d[ny][nx] = d[y][x] + 1
            q.append((ny, nx))

print(d[gy][gx])
