#include using namespace std; using LL = long long int; #define incII(i, l, r) for(LL i = (l) ; i <= (r); i++) #define incIX(i, l, r) for(LL i = (l) ; i < (r); i++) #define incXI(i, l, r) for(LL i = (l) + 1; i <= (r); i++) #define incXX(i, l, r) for(LL i = (l) + 1; i < (r); i++) #define decII(i, l, r) for(LL i = (r) ; i >= (l); i--) #define decIX(i, l, r) for(LL i = (r) - 1; i >= (l); i--) #define decXI(i, l, r) for(LL i = (r) ; i > (l); i--) #define decXX(i, l, r) for(LL i = (r) - 1; i > (l); i--) #define inc(i, n) incIX(i, 0, n) #define dec(i, n) decIX(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec1(i, n) decII(i, 1, n) auto inII = [](auto x, auto l, auto r) { return (l <= x && x <= r); }; auto inIX = [](auto x, auto l, auto r) { return (l <= x && x < r); }; auto inXI = [](auto x, auto l, auto r) { return (l < x && x <= r); }; auto inXX = [](auto x, auto l, auto r) { return (l < x && x < r); }; auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); }; auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); }; auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); }; auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); }; #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define FI first #define SE second #define FR front() #define BA back() #define ALL(c) c.begin(), c.end() #define RALL(c) c.rbegin(), c.rend() #define RV(c) reverse(ALL(c)) #define SC static_cast #define SI(c) SC(c.size()) #define SL(c) SC(c.size()) #define RF(e, c) for(auto & e: c) #define SF(c, ...) for(auto & [__VA_ARGS__]: c) #define until(e) while(! (e)) #define if_not(e) if(! (e)) #define ef else if #define UR assert(false) auto * IS = & cin; auto * OS = & cout; array SEQ = { "", " ", "" }; // input template T in() { T a; (* IS) >> a; return a; } // input: tuple template void tin_(istream & is, U & t) { if constexpr(I < tuple_size::value) { is >> get(t); tin_(is, t); } } template istream & operator>>(istream & is, tuple & t) { tin_<0>(is, t); return is; } template auto tin() { return in>(); } // input: array template istream & operator>>(istream & is, array & a) { RF(e, a) { is >> e; } return is; } template auto ain() { return in>(); } // input: multi-dimensional vector template T vin() { T v; (* IS) >> v; return v; } template auto vin(N n, M ... m) { vector(m ...))> v(n); inc(i, n) { v[i] = vin(m ...); } return v; } // input: multi-column (tuple) template void colin_([[maybe_unused]] U & t) { } template void colin_(U & t) { get(t).PB(in()); colin_(t); } template auto colin(int n) { tuple ...> t; inc(i, n) { colin_ ...>, 0, T ...>(t); } return t; } // output void out_([[maybe_unused]] string s) { } template void out_([[maybe_unused]] string s, A && a) { (* OS) << a; } template void out_(string s, A && a, B && ... b) { (* OS) << a << s; out_(s, b ...); } auto outF = [](auto x, auto y, auto z, auto ... a) { (* OS) << x; out_(y, a ...); (* OS) << z << flush; }; auto out = [](auto ... a) { outF("", " " , "\n", a ...); }; auto outS = [](auto ... a) { outF("", " " , " " , a ...); }; auto outL = [](auto ... a) { outF("", "\n", "\n", a ...); }; auto outN = [](auto ... a) { outF("", "" , "" , a ...); }; // output: multi-dimensional vector template ostream & operator<<(ostream & os, vector const & v) { os << SEQ[0]; inc(i, SI(v)) { os << (i == 0 ? "" : SEQ[1]) << v[i]; } return (os << SEQ[2]); } template void vout_(T && v) { (* OS) << v; } template void vout_(T && v, A a, B ... b) { inc(i, SI(v)) { (* OS) << (i == 0 ? "" : a); vout_(v[i], b ...); } } template void vout (T && v, A a, B ... b) { vout_(v, a, b ...); (* OS) << a << flush; } template void voutN(T && v, A a, B ... b) { vout_(v, a, b ...); (* OS) << flush; } // ---- ---- auto dd = [](int h, int w, int i, int j, int k) -> tuple { vector di = { +1, 0, -1, 0, +1, +1, -1, -1, 0 }; vector dj = { 0, +1, 0, -1, +1, -1, +1, -1, 0 }; int ii = i + di.at(k); int jj = j + dj.at(k); return { inIX(ii, 0, h) && inIX(jj, 0, w), ii, jj }; }; // make vector template T MV(T v) { return v; } template auto MV(T v, int a, U ... b) { return vector(a, MV(v, b ...)); } int main() { auto [h, w, ai, aj, bi, bj] = ain(); ai--; aj--; bi--; bj--; auto s = vin(h); auto dist = [&](int si, int sj, auto & D) { queue> q; auto add = [&](int i, int j, int d) { if(setmin(D[i][j], d)) { q.push({ i, j, d }); } }; add(si, sj, 0); until(q.empty()) { auto [i, j, d] = q.front(); q.pop(); inc(k, 4) { auto [f, ii, jj] = dd(h, w, i, j, k); if(f && s[ii][jj] == '.') { add(ii, jj, d + 1); } } } }; const int INF = 1e9; auto da = MV(INF, h, w); auto db = MV(INF, h, w); auto dc = MV(INF, h, w); dist(ai, aj, da); dist(bi, bj, db); s[ai][aj] = '#'; dist(bi, bj, dc); s[ai][aj] = '.'; if(da[bi][bj] == INF) { out(-1); exit(0); } int ans = INF; inc(i, h) { inc(j, w) { if(da[i][j] == INF) { continue; } vector d; inc(k, 4) { auto [f, ii, jj] = dd(h, w, i, j, k); if(f && s[ii][jj] == '.') { d.PB(da[ii][jj] + db[ii][jj]); } } if(SI(d) >= 3) { sort(ALL(d)); setmin(ans, d[1] + d[2]); } } } { vector d; inc(k, 4) { auto [f, ii, jj] = dd(h, w, ai, aj, k); if(f && s[ii][jj] == '.') { d.PB(dc[ii][jj]); } } if(SI(d) >= 2) { sort(ALL(d)); setmin(ans, d[0] + d[1] + 2); } } if(ans == INF) { ans = -1; } out(ans); }