import sys sys.setrecursionlimit(10 ** 6) from bisect import * from collections import * from heapq import * def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline()[:-1] def LLI(rows_number): return [LI() for _ in range(rows_number)] int1 = lambda x: int(x) - 1 def MI1(): return map(int1, sys.stdin.readline().split()) def LI1(): return list(map(int1, sys.stdin.readline().split())) p2D = lambda x: print(*x, sep="\n") dij = [(1, 0), (0, 1), (-1, 0), (0, -1)] def main(): h,w=MI() si,sj=MI() gi,gj=MI() tt=[] for _ in range(h): row=[False]+[c=="#" for c in SI()]+[False] tt.append(row) q=deque() q.append((si,sj,0)) dist=[[-1]*(w+2) for _ in range(h+2)] while q: i,j,d=q.popleft() for di,dj in dij: ni,nj=i+di,j+dj if ni==gi and nj==gj: print(d+1) exit() if ni<0 or ni>h+1 or nj<0 or nj>w+1:continue if tt[ni][nj]:continue if dist[ni][nj]!=-1:continue dist[ni][nj]=d+1 q.append((ni,nj,d+1)) main()