#pragma GCC optimize("Ofast") #pragma GCC target("avx2") char*mmap(); char*rp; #define RD(v) v=0;{int _c;while(_c=*rp++-48,_c>=0)v=v*10+_c;} int wy,wx,ay,ax,by,bx,z,len; static inline int s(int y,int x){ return rp[y*(wx+1)+x]=='.'; } #define INF (1<<24) typedef int dp_t[1333][1333]; dp_t dp1,dp2; struct{int y,x,d;} q[1<<16]; int qr,qw; static inline void enq(int y,int x,int d){ q[qw].y=y; q[qw].x=x; q[qw].d=d; qw=(qw+1)&(1<<16)-1; } void bfs(int y,int x,int ty,int tx,dp_t dp){ for(int y=0;yd?d:z; } main(){ rp=mmap(0l,1l<<28,1,2,0,0ll); RD(wy); RD(wx); RD(ay);--ay; RD(ax);--ax; RD(by);--by; RD(bx);--bx; bfs(ay,ax,0,0,dp1); bfs(by,bx,0,0,dp2); len=dp1[by][bx]; z=INF; int n=0; for(int y=0;y2){ d+=2; z=z<=d?z:d; } } } } z*=2; bfs(ay,ax,by,bx,dp1); f(by-1,bx); f(by+1,bx); f(by,bx-1); f(by,bx+1); printf("%d\n",z>=INF?-1:z); }