//#define _GLIBCXX_DEBUG #include using namespace std; #define endl '\n' #define lfs cout<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair; templatebool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} templatebool chmax(T1 &a,T2 b){if(avoid ans(bool x,T1 y,T2 z){if(x)cout<void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;ivoid debug(vector&v,ll n){if(n!=0)cout<vector>vec(ll x, ll y, T w){vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,-1,0,0,1,1,-1,-1};vectordy={0,0,1,-1,1,-1,1,-1}; templatevector make_v(size_t a,T b){return vector(a,b);} templateauto make_v(size_t a,Ts... ts){return vector(a,make_v(ts...));} templateostream &operator<<(ostream &os, const pair&p){return os << p.first << " " << p.second;} templateostream &operator<<(ostream &os, const vector &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;} templatevoid rearrange(vector&ord, vector&v){ auto tmp = v; for(int i=0;ivoid rearrange(vector&ord,Head&& head, Tail&&... tail){ rearrange(ord, head); rearrange(ord, tail...); } //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); int popcount(ll x){return __builtin_popcountll(x);}; int poplow(ll x){return __builtin_ctzll(x);}; int pophigh(ll x){return 63 - __builtin_clzll(x);}; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; ll h,w,ra,ca,rb,cb;cin>>h>>w>>ra>>ca>>rb>>cb;ra--;ca--;rb--;cb--; vectors(h); rep(i,0,h){ cin>>s[i]; } vector>dist; vector>dp; bool ok=true; auto solve=[&](){ queue

que; dist=vec(h,w,-1); dp=vec(h,w,0); que.emplace(ra,ca); dist[ra][ca]=0; dp[ra][ca]=1; while(!que.empty()){ auto p=que.front(); que.pop(); ll d=dist[p.fi][p.se]; rep(i,0,4){ ll x=p.fi+dx[i],y=p.se+dy[i]; if(x<0||y<0||x>=h||y>=w||s[x][y]=='#')continue; if(!ok&&p.fi==ra&&p.se==ca&&x==rb&&y==cb)continue; if(dist[x][y]==-1){ dist[x][y]=d+1; dp[x][y]=dp[p.fi][p.se]; que.emplace(x,y); } else if(dist[x][y]==d+1)dp[x][y]=min(2,dp[x][y]+dp[p.fi][p.se]); } } }; ll ret=INF; solve(); auto dpre=dist; //debug(dist,h,w);cout<path; { ll nx=rb,ny=cb; while(nx!=ra||ny!=ca){ rep(i,0,4){ ll x=nx+dx[i],y=ny+dy[i]; if(x<0||y<0||x>=h||y>=w||dist[x][y]+1!=dist[nx][ny])continue; path.EB(x,y); nx=x;ny=y; break; } } path.pop_back(); } for(auto z:path){ ll cnt=0; rep(i,0,4){ ll x=z.fi+dx[i],y=z.se+dy[i]; if(x<0||y<0||x>=h||y>=w||s[x][y]=='#')continue; cnt++; } if(cnt>=3)chmin(ret,dist[rb][cb]*2+2); } if(dp[rb][cb]>=2)chmin(ret,dist[rb][cb]*2); //cout<=h||y>=w||s[x][y]=='#')continue; cnt++; } //cout<=3)chmin(ret,(dpre[i][j]+dist[i][j])*2+4); } } ans(ret==INF,-1,ret); return 0; }