from collections import deque n,m = map(int,input().split()) xs,ys,xt,yt = map(int,input().split()) grid = [list(input().strip()) for _ in range(n)] # print(grid) def bfs(): seen = set() run = [(0,-1),(0,1),(1,0),(-1,0)] queue = deque([(xs-1,ys-1,0)]) while queue: x,y,d = queue.popleft() if x == xt-1 and y == yt-1: return d for dx,dy in run: nx,ny = x+dx,y+dy if 0<=nx<n and 0<=ny<m and (nx,ny) not in seen and grid[nx][ny]=='.': queue.append((nx,ny,d+1)) seen.add((nx,ny)) return -1 print(bfs())