#include using namespace std; using ll = long long; using ld = long double; // -------------------------------------------------------- #define FOR(i,l,r) for (ll i = (l); i < (r); ++i) #define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define COUNT(c,v) count(ALL(c),(v)) #define SZ(c) ((ll)(c).size()) #define BIT(b,i) (((b)>>(i)) & 1) #define PCNT(b) __builtin_popcountll(b) #define P0(i) (((i) & 1) == 0) #define P1(i) (((i) & 1) == 1) #define LB(c,v) distance((c).begin(), lower_bound(ALL(c), (v))) #define UB(c,v) distance((c).begin(), upper_bound(ALL(c), (v))) #define UQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end()) #define END(...) do { print(__VA_ARGS__); exit(0); } while (0) #define elif else if template using PQ_max = priority_queue; template using PQ_min = priority_queue, greater>; constexpr int inf = (1 << 30) - 1; // 1073741824 - 1 constexpr ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 #ifdef _LOCAL #define debug_bar cerr << "----------------------------------------\n"; #define debug_header cerr << "[" << __FUNCTION__ << ":" << __LINE__ << "] " #define debug(...) do { debug_header; cerr << #__VA_ARGS__ << " = "; view(__VA_ARGS__); cerr << '\n'; } while (0) #define debug2(vv) do { debug_header; cerr << #vv << " = [\n"; view2d(vv); cerr << " ]\n"; } while (0) #define debug3(vvv) do { debug_header; cerr << #vvv << " = [\n"; view3d(vvv); cerr << " ]\n"; } while (0) void view() {} void view(const int& a) { if (abs(a) == inf) { cerr << "+-"[signbit(a)] << "inf"; } else { cerr << a; } } void view(const ll& a) { if (abs(a) == INF) { cerr << "+-"[signbit(a)] << "INF"; } else { cerr << a; } } template void view(const T& a) { cerr << a; } template void view(const pair& a) { cerr << "("; view(a.first); cerr << ", "; view(a.second); cerr << ")"; } template void view(const tuple& a) { cerr << "("; view(get<0>(a)); cerr << ", "; view(get<1>(a)); cerr << ", "; view(get<2>(a)); cerr << ")"; } template void view(const tuple& a) { cerr << "("; view(get<0>(a)); cerr << ", "; view(get<1>(a)); cerr << ", "; view(get<2>(a)); cerr << ", "; view(get<3>(a)); cerr << ")"; } template void view(const vector& v){ cerr << "["; for (int i = 0; i < (int)v.size(); i++) { if (i) { cerr << ", "; } view(v[i]); } cerr << "]"; } template void view(const vector>& vv){ cerr << "["; for (int i = 0; i < (int)vv.size(); i++) { if (i) { cerr << ", "; } view(vv[i]); } cerr << "]"; } template void view(const map& mp){ cerr << "["; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) { cerr << ", "; } cerr << "("; view(it->first); cerr << ", "; view(it->second); cerr << ")"; } cerr << "]"; } template void view(const multimap& mmp){ cerr << "["; for (auto it = mmp.begin(); it != mmp.end(); it++) { if (it != mmp.begin()) { cerr << ", "; } cerr << "("; view(it->first); cerr << ", "; view(it->second); cerr << ")"; } cerr << "]"; } template void view(const set& s){ cerr << "["; for (auto it = s.begin(); it != s.end(); it++) { if (it != s.begin()) { cerr << ", "; } view(*it); } cerr << "]"; } template void view(const multiset& ms){ cerr << "["; for (auto it = ms.begin(); it != ms.end(); it++) { if (it != ms.begin()) { cerr << ", "; } view(*it); } cerr << "]"; } template void view(const deque& d){ cerr << "(front)<-["; for (auto it = d.begin(); it != d.end(); it++) { if (it != d.begin()) { cerr << ", "; } view(*it); } cerr << "]"; } template void view(stack s){ vector v; while (not s.empty()) { v.push_back(s.top()); s.pop(); } reverse(v.begin(), v.end()); view(v); cerr << "->(top)"; } template void view(queue q){ vector v; while (not q.empty()) { v.push_back(q.front()); q.pop(); } cerr << "(front)<-"; view(v); } template void view(PQ_max pq){ vector v; while (not pq.empty()) { v.push_back(pq.top()); pq.pop(); } cerr << "(top)<-"; view(v); } template void view(PQ_min pq){ vector v; while (not pq.empty()) { v.push_back(pq.top()); pq.pop(); } cerr << "(top)<-"; view(v); } template void view2d(const vector>& vv){ for (int i = 0; i < (int)vv.size(); i++) { cerr << " "; view(vv[i]); cerr << ",\n"; } } template void view3d(const vector>>& vvv) { for (int i = 0; i < (int)vvv.size(); i++) { for (int j = 0; j < (int)vvv[i].size(); j++) { cerr << " " << " ["[j == 0]; view(vvv[i][j]); if (j == (int)vvv[i].size() - 1) { cerr << "]"; } cerr << ",\n"; } if (i < (int)vvv.size() - 1) { cerr << "\n"; } } } template void view(const T& a, const Ts&... b) { view(a); cerr << ", "; view(b...); } #else #define cerr if (false) cerr #define debug_bar #define debug(...) #define debug2(vv) #define debug3(vvv) #endif template void input(T&... a) { (cin >> ... >> a); } void print() { cout << '\n'; } template void print(const T& a) { cout << a << '\n'; } template void print(const T& a, const Ts&... b) { cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; } template void cout_line(const vector& ans, int l, int r) { for (int i = l; i < r; i++) { if (i != l) { cout << ' '; } cout << ans[i]; } cout << '\n'; } template bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; } template bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; } template T SUM(const vector& A) { return accumulate(ALL(A), T(0)); } template vector cumsum(const vector& A, bool offset = false) { int N = A.size(); vector S(N+1, 0); for (int i = 0; i < N; i++) { S[i+1] = S[i] + A[i]; } if (not offset) { S.erase(S.begin()); } return S; } template string to_binary(T x, int B) { string s; while (x) { s += ('0' + (x & 1)); x >>= 1; } while ((int)s.size() < B) { s += '0'; } reverse(s.begin(), s.end()); return s; } ll mod(ll x, ll m) { assert(m != 0); return (x % m + m) % m; } ll ceil(ll a, ll b) { if (b < 0) { return ceil(-a, -b); } assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); } ll floor(ll a, ll b) { if (b < 0) { return floor(-a, -b); } assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); } ll powint(ll x, ll n) { assert(n >= 0); if (n == 0) { return 1; }; ll res = powint(x, n>>1); res *= res; if (n & 1) { res *= x; } return res; } pair divmod(ll a, ll b) { assert(b != 0); ll q = floor(a, b); return make_pair(q, a - q * b); } ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); } ll digit_len(ll n) { assert(n >= 0); if (n == 0) { return 1; } ll sum = 0; while (n > 0) { sum++; n /= 10; } return sum; } ll digit_sum(ll n) { assert(n >= 0); ll sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return sum; } ll digit_prod(ll n) { assert(n >= 0); if (n == 0) { return 0; } ll prod = 1; while (n > 0) { prod *= n % 10; n /= 10; } return prod; } ll xor_sum(ll x) { assert(0 <= x); switch (x % 4) { case 0: return x; case 1: return 1; case 2: return x ^ 1; case 3: return 0; } assert(false); } string toupper(const string& S) { string T(S); for (int i = 0; i < (int)T.size(); i++) { T[i] = toupper(T[i]); } return T; } string tolower(const string& S) { string T(S); for (int i = 0; i < (int)T.size(); i++) { T[i] = tolower(T[i]); } return T; } int a2i(const char& c) { assert(islower(c)); return (c - 'a'); } int A2i(const char& c) { assert(isupper(c)); return (c - 'A'); } int d2i(const char& d) { assert(isdigit(d)); return (d - '0'); } char i2a(const int& i) { assert(0 <= i && i < 26); return ('a' + i); } char i2A(const int& i) { assert(0 <= i && i < 26); return ('A' + i); } char i2d(const int& i) { assert(0 <= i && i <= 9); return ('0' + i); } using P = pair; using VP = vector

; using VVP = vector; using VS = vector; using VVS = vector; using VI = vector; using VVI = vector; using VVVI = vector; using VLL = vector; using VVLL = vector; using VVVLL = vector; using VB = vector; using VVB = vector; using VVVB = vector; using VD = vector; using VVD = vector; using VVVD = vector; using VLD = vector; using VVLD = vector; using VVVLD = vector; const ld EPS = 1e-10; const ld PI = acosl(-1.0); // -------------------------------------------------------- // #include // using namespace atcoder; ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); } ll min(int a, ll b) { return min((ll)a, b); } ll min(ll a, int b) { return min(a, (ll)b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int H, W; input(H, W); int sh, sw, gh, gw; input(sh, sw, gh, gw); sh--; sw--; gh--; gw--; VS S(H); REP(i,H) { input(S[i]); } VVI maxh(H, VI(W)); REP(w,W) { int mx = -1; REP(h,H) { if (S[h][w] == '.') mx = h; maxh[h][w] = mx; } } VVI dist(H, VI(W, inf)); dist[sh][sw] = 0; deque> d; d.emplace_back(sh, sw); while (not d.empty()) { auto [h, w] = d.front(); d.pop_front(); for (auto dw : {-1, +1}) { // 通常 if (0 <= w + dw && w + dw < W) { int hh = min(H-1, h+1); hh = maxh[hh][w+dw]; if (chmin(dist[hh][w+dw], dist[h][w] + 0)) d.emplace_front(hh, w+dw); } // 通常 + 通常 if (0 <= w + 2*dw && w + 2*dw < W) { int E = h - maxh[h][w+dw]; int hh = maxh[min(H-1, h-E+E/2+1)][w+2*dw]; hh = maxh[hh][w+2*dw]; if (chmin(dist[hh][w+2*dw], dist[h][w] + 0)) d.emplace_front(hh, w+2*dw); } // 通常 + ブースト if (0 <= w + 2*dw && w + 2*dw < W) { int hh = min(H-1, h+1); hh = maxh[hh][w+2*dw]; if (chmin(dist[hh][w+2*dw], dist[h][w] + 1)) d.emplace_back(hh, w+2*dw); } } // 通常 + ブースト (左右いずれかに通常移動してから元の列にブースト移動) int hh = min(H-1, h+1); hh = maxh[hh][w]; if (chmin(dist[hh][w], dist[h][w] + 1)) d.emplace_back(hh, w); } int ans = dist[gh][gw]; if (ans == inf) ans = -1; print(ans); return 0; }