#include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int H,W; int Ay,Ax,By,Bx; bool block[3<<9][3<<9]; int D[3<<9][3<<9][2]; int P[3<<9][3<<9][2]; int dx[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct QueueNode{ int x,y,t; }; int main(){ cin>>H>>W>>Ay>>Ax>>By>>Bx; Ay--; Ax--; By--; Bx--; rep(y,H) rep(x,W){ char c; cin>>c; block[x][y]=(c=='#'); } int ans=1000000000; rep(y,H) rep(x,W) rep(t,2){ D[x][y][t]=-1; P[x][y][t]=-1; } queue Q; D[Ax][Ay][0]=0; Q.push({Ax,Ay,0}); while(Q.size()){ auto q = Q.front(); Q.pop(); rep(i,4){ auto nx=q; nx.x+=dx[i][0]; nx.y+=dx[i][1]; if(block[nx.x][nx.y]) continue; while(nx.t<2) if(D[nx.x][nx.y][nx.t]!=-1) nx.t++; else break; if(nx.t==2) continue; if(P[q.x][q.y][q.t]==(i^2)) continue; D[nx.x][nx.y][nx.t]=D[q.x][q.y][q.t]+1; P[nx.x][nx.y][nx.t]=i; Q.push(nx); } } if(D[Bx][By][1]==-1){ cout<<-1<