mod = 998244353 def main(): import sys from bisect import bisect_left from collections import deque input = sys.stdin.readline H, W = map(int, input().split()) sh, sw, gh, gw = map(int, input().split()) gh -= 1 gw -= 1 G = [] for _ in range(H): G.append(input().rstrip('\n')) R = [] for w in range(W): col = [] for h in range(H): if G[h][w] == ".": col.append(h) R.append(col) que = deque() seen = [[-1] * W for _ in range(H)] for w in range(W): que.append((0, 0, w)) seen[0][w] = 0 while que: x, h, w = que.popleft() if seen[h][w] < x: continue #print(x, h, w) # normal jump if w - 1 >= 0: j = bisect_left(R[w - 1], h+2) - 1 h_back = R[w - 1][j] if seen[h_back][w - 1] == -1 or seen[h_back][w - 1] > x: seen[h_back][w - 1] = x que.appendleft((x, h_back, w - 1)) for j_ in range(j - 1, -1, -1): h_ = R[w - 1][j_] if seen[h_][w - 1] == -1 or seen[h_][w - 1] > x: seen[h_][w - 1] = x else: break if w - 2 >= 0: h_new = h_back + max(0, h - h_back) // 2 + 1 jj = bisect_left(R[w - 2], h_new + 1) - 1 hh = R[w - 2][jj] if seen[hh][w - 2] == -1 or seen[hh][w - 2] > x: seen[hh][w - 2] = x que.appendleft((x, hh, w - 2)) for j_ in range(jj - 1, -1, -1): h_ = R[w - 2][j_] if seen[h_][w - 2] == -1 or seen[h_][w - 2] > x: seen[h_][w - 2] = x else: break if w + 1 < W: j = bisect_left(R[w + 1], h+2) - 1 h_back = R[w + 1][j] if seen[h_back][w + 1] == -1 or seen[h_back][w + 1] > x: seen[h_back][w + 1] = x que.appendleft((x, h_back, w + 1)) for j_ in range(j - 1, -1, -1): h_ = R[w + 1][j_] if seen[h_][w + 1] == -1 or seen[h_][w + 1] > x: seen[h_][w + 1] = x else: break if w + 2 < W: h_new = h_back + max(0, h - h_back) // 2 + 1 jj = bisect_left(R[w + 2], h_new + 1) - 1 hh = R[w + 2][jj] if seen[hh][w + 2] == -1 or seen[hh][w + 2] > x: seen[hh][w + 2] = x que.appendleft((x, hh, w + 2)) for j_ in range(jj - 1, -1, -1): h_ = R[w + 2][j_] if seen[h_][w + 2] == -1 or seen[h_][w + 2] > x: seen[h_][w + 2] = x else: break # super jump if w - 2 >= 0: h_new = h + 1 jj = bisect_left(R[w - 2], h_new + 1) - 1 hh = R[w - 2][jj] if seen[hh][w - 2] == -1: seen[hh][w - 2] = x + 1 que.append((x + 1, hh, w - 2)) for j_ in range(jj - 1, -1, -1): h_ = R[w - 2][j_] if seen[h_][w - 2] == -1 or seen[h_][w - 2] > x + 1: seen[h_][w - 2] = x + 1 else: break if w + 2 < W: h_new = h + 1 jj = bisect_left(R[w + 2], h_new + 1) - 1 hh = R[w + 2][jj] if seen[hh][w + 2] == -1: seen[hh][w + 2] = x + 1 que.append((x + 1, hh, w + 2)) for j_ in range(jj - 1, -1, -1): h_ = R[w + 2][j_] if seen[h_][w + 2] == -1 or seen[h_][w + 2] > x + 1: seen[h_][w + 2] = x + 1 else: break h_new = h + 1 if h_new < H: if G[h_new][w] == ".": if seen[h_new][w] == -1: seen[h_new][w] = x + 1 que.append((x + 1, h_new, w)) print(seen[gh][gw]) #for h in range(H): # print(*seen[h]) if __name__ == '__main__': main()