#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") /** author: shobonvip created: 2025.04.20 01:49:37 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; sx--; sy--; gx--; gy--; vector> c(h, vector(w)); rep(i,0,h) rep(j,0,w) { cin >> c[i][j]; } if (sx > gx) { vector> nc(h, vector(w)); rep(i,0,h) { rep(j,0,w) { nc[i][j] = c[h-1-i][j]; } } swap(c, nc); sx = h-1-sx; gx = h-1-gx; } if (sy > gy) { vector> nc(h, vector(w)); rep(i,0,h) { rep(j,0,w) { nc[i][j] = c[i][w-1-j]; } } swap(c, nc); sy = w-1-sy; gy = w-1-gy; } int geta_h = gx - sx; int geta_w = gy - sy; static int d[1600][1620][4]; static bool seen[1600][1620][4]; rep(i,0,1600)rep(k,0,1620)rep(s,0,4){ d[i][k][s]=1e9; } d[sx*w+sy][0][0] = 0; deque> mada; mada.push_back(make_tuple(sx*w+sy, 0, 0)); while(!mada.empty()){ auto [val, a, state] = mada.front(); mada.pop_front(); if(seen[val][a][state]) continue; seen[val][a][state] = 1; int i = val/w; int j = val%w; vector cango; for (auto [x, y]:{ pair(i-1, j), pair(i+1, j), pair(i, j-1), pair(i, j+1) }) { if (!(0<=x&&x 0) { if (chmin(d[x*w+y][a][state], d[val][a][state] + 1)) { mada.push_back(make_tuple(x*w+y,a,state)); } }else if(y-j > 0) { if (chmin(d[x*w+y][a][state], d[val][a][state])) { mada.push_front(make_tuple(x*w+y,a,state)); } }else if (i-x > 0) { if (a+1 >= h*w+10) continue; if (chmin(d[x*w+y][a+1][state], d[val][a][state])) { mada.push_front(make_tuple(x*w+y,a+1,state)); } }else if (x-i > 0) { if (chmin(d[x*w+y][a][state], d[val][a][state])) { mada.push_front(make_tuple(x*w+y,a,state)); } } } for (int news: cango) { if (news == 1) { if (a+1 >= h*w+10) continue; if (chmin(d[val][a+1][state|news], d[val][a][state])) { mada.push_front(make_tuple(val,a+1,state|news)); } }else { if (chmin(d[val][a][state|news], d[val][a][state] + 1)) { mada.push_back(make_tuple(val,a,state|news)); } } } } const int mx = 2e6; vector p(mx,true); p[0] = false; p[1] = false; for (int i=2; i hs((int)1e6); vector ws((int)1e6); for (int i=0; i<(int)1e6; i++) { if (p[i] && p[i+geta_h]) hs[i] = 1; if (p[i] && p[i+geta_w]) ws[i] = 1; } vector ht((int)1e6, 1e9); vector wt((int)1e6, 1e9); for (int i=(int)1e6-1; i>=0; i--){ if (hs[i]) ht[i]=0; if (ws[i]) wt[i]=0; } for (int i=(int)1e6-2; i>=0; i--) { chmin(ht[i], ht[i+1] + 1); chmin(wt[i], wt[i+1] + 1); } ll ans = 1e9; for (int a=0; a (int)1e8) continue; //cout << a << ' ' << b << ' ' << state << endl; if ((state & 1) == 0) if (ht[a] != 0) continue; if ((state & 2) == 0) if (wt[b] != 0) continue; chmin(ans, ll(ht[a] + wt[b] + a + b) * 2 + geta_h + geta_w); } } if (ans > 1e8) { cout << -1 << '\n'; }else { cout << ans << '\n'; } }