#include "bits/stdc++.h" using namespace std; #define ALL(x) begin(x), end(x) #define rep(i, n) for (int i = 0; i < (n); i++) #define mod 1000000007 using ll = long long; const int INF = 1000000000; const ll LINF = 1001002003004005006ll; int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } struct IOSetup { IOSetup() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(12); } } iosetup; template ostream &operator<<(ostream &os, const vector &v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 == (int)v.size() ? "" : " "); return os; } template istream &operator>>(istream &is, vector &v) { for (T &x : v) is >> x; return is; } template struct Dice{ //left, right, front, back, down, up T l,r,f,b,d,u; void RollF(){ // y-- (next, d <- f) int tmp=d; d=f; f=u; u=b; b=tmp; } void RollB(){ // y++ (next, d <- b) int tmp=d; d=b; b=u; u=f; f=tmp; } void RollL(){ // x-- (next, d <- l) int tmp=d; d=l; l=u; u=r; r=tmp; } void RollR(){ // x++ (next, d <- r) int tmp=d; d=r; r=u; u=l; l=tmp; } }; template bool same_dice(Dice a,Dice b){ return (a.b==b.b and a.f==b.f and a.l==b.l and a.r==b.r and a.u==b.u and a.d==b.d); } signed main() { int H, W; cin >> H >> W; int si, sj, gi, gj; cin >> si >> sj >> gi >> gj; si--, sj--, gi--, gj--; vector G(H); cin >> G; vector>> dp(H, vector>(W, vector(6, INF))); // 0u, 1d, 2l, 3r, 4f, 5b using Tp = tuple; queue que; dp[si][sj][0] = 0; que.emplace(si, sj, 0); Dice Ud = {0, 0, 0, 0, 0, 1}, Dd = {0, 0, 0, 0, 1, 0}, Bd = {0, 0, 0, 1, 0, 0}, Fd = {0, 0, 1, 0, 0, 0}, Rd = {0, 1, 0, 0, 0, 0}, Ld = {1, 0, 0, 0, 0, 0}; auto diceIdx = [&](Dice x){ if(same_dice(x, Ud)) return 0; if(same_dice(x, Dd)) return 1; if(same_dice(x, Bd)) return 2; if(same_dice(x, Fd)) return 3; if(same_dice(x, Rd)) return 4; if(same_dice(x, Ld)) return 5; return -1; }; auto makeDice = [&](int state){ if(state == 0) return Ud; if(state == 1) return Dd; if(state == 2) return Bd; if(state == 3) return Fd; if(state == 4) return Rd; if(state == 5) return Ld; return Ud; }; while(not que.empty()){ auto [ci, cj, dir] = que.front(); que.pop(); {// R int ni = ci + 0, nj = cj + 1; Dice d = makeDice(dir); d.RollR(); int ndir = diceIdx(d); if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.') if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir); } {// L int ni = ci + 0, nj = cj - 1; Dice d = makeDice(dir); d.RollL(); int ndir = diceIdx(d); if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.') if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir); } {// F int ni = ci + 1, nj = cj; Dice d = makeDice(dir); d.RollF(); int ndir = diceIdx(d); if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.') if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir); } {// B int ni = ci - 1, nj = cj; Dice d = makeDice(dir); d.RollB(); int ndir = diceIdx(d); if(0<= ni and ni < H and 0 <= nj and nj < W and G[ni][nj] == '.') if(chmin(dp[ni][nj][ndir], dp[ci][cj][dir] + 1)) que.emplace(ni, nj, ndir); } } if(dp[gi][gj][0] > -INF) cout << dp[gi][gj][0] << endl; else cout << -1 << endl; return 0; }