n, m = map(int, input().split())
xs, ys, xt, yt = map(int, input().split())

graph = [[1]*m for i in range(n)]
for i in range(n):
    string = input()
    for j in range(len(string)):
        if string[j] == ".":
            graph[i][j] = 0

visits = {(xs-1, ys-1): 0}
queue = [(xs-1, ys-1, 0)]

def bfs():
    count = 0
    while queue:
        for i in range(len(queue)):
            x_pre, y_pre, d_pre = queue.pop(0)

            for i, j in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
                x_new, y_new = x_pre + i, y_pre + j
                if 0 <= x_new <= n-1 and 0 <= y_new <= m-1 and graph[x_new][y_new] == 0:
                    d_new = d_pre + 1
                    if x_new == xt-1 and y_new == yt-1:
                        return count+1

                    if (x_new, y_new) not in visits or d_new < visits[(x_new, y_new)]:
                        queue.append((x_new, y_new, d_new))
                        visits[(x_new, y_new)] = d_new
        count += 1
    return -1

print(bfs())