from collections import deque import sys R, C = map(int, input().split()) sx, sy = map(int, input().split()) gx, gy = map(int, input().split()) sy = sy-1 sx = sx-1 gy = gy-1 gx = gx-1 c = [] for i in range(R): r = list(input()) c.append(r) depth = 0 q = deque() q.append([sx, sy, depth]) twd = [[1, 0], [0, 1], [-1, 0], [0, -1]] visited = [([0] * C) for i in range(R)] while q: x, y, d = q.popleft() for i in range(4): dx, dy = x + twd[i][0], y + twd[i][1] if (dx < 0) or (dx >= C): continue if (dy < 0) or (dy >= R): continue # if c[dx][dy] == "#": # continue if visited[dx][dy] == 1: continue q.append([dx, dy, d+1]) visited[x][y] = 1 if visited[gx][gy] == 1: print(d) sys.exit()