/* author: Kite_kuma created: 2020.12.18 12:43:15 */ // #ifdef LOCAL // #define _GLIBCXX_DEBUG // #endif #include using namespace std; #pragma region aliases #define rep(i, n) for(long long i = 0; i < (n); i++) #define rrep(i, n) for(long long i = (n)-1; i > -1; i--) #define Rep(i, m, n) for(long long i = (m); i < (n); i++) #define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--) #define REP(i, m, n, p) for(long long i = m; i < n; i += p) #define foa(s, v) for(auto &s : v) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define bcnt(n) __builtin_popcountll(n) using ll = long long; using ld = long double; using ull = unsigned long long; using vb = vector; using vi = vector; using vvi = vector>; using vvvi = vector>>; using vll = vector; using vvll = vector; using vvvll = vector; template using V = vector; template using VV = V>; template using VVV = V>>; template using pqup = priority_queue, greater>; template using pqdn = priority_queue; #pragma endregion #pragma region constants long long const dekai = 1e18; const long double pi = acos(-1); const char dl = '\n'; const char sp = ' '; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; const int mod = 1000000007; // const int mod = 998244353; #pragma endregion #pragma region basic_procedure template inline bool isin(T x, T lef, T rig) { return ((lef <= x) && (x < rig)); } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << '\n'; } void No() { cout << "No\n"; } void YES(bool f = 1) { cout << (f ? "YES" : "NO") << '\n'; } void NO() { cout << "NO\n"; } template void drop(T answer) { cout << answer << '\n'; exit(0); } void err(bool flag) { if(!flag) return; cout << -1 << '\n'; exit(0); } template void vout(vector const &v, bool tate = 0) { if(tate) { for(auto const &a : v) { cout << a << '\n'; } return; } for(auto it = v.begin(); it != v.end(); it++) { cout << (*it); if(it != v.end() - 1) { cout << ' '; } } cout << '\n'; return; } inline void print() { cout << '\n'; } template inline void print(T x) { cout << x << '\n'; return; } template void print(Head H, Tail... T) { cout << H << " "; print(T...); } template void add(vector &v, T val) { for(auto &a : v) a += val; return; } template T dup(T a, T b) { assert(b != 0); T x = abs(a); T y = abs(b); T z = (x + y - 1) / y; if((a < 0 && b > 0) || (a > 0 && b < 0)) return -z; else if(a == 0) return 0; else return z; } template T greatest_lower_multiple(T x, T d) { if(d == 0) return 0; if(d < 0) d *= -1; T y = x % d; if(y < 0) y += d; return x - y; } template T least_upper_multiple(T x, T d) { return -greatest_lower_multiple(-x, d); } long long POW(long long a, long long n) { long long res = 1; while(n > 0) { if(n & 1) res = res * a; a = a * a; n >>= 1; } return res; } long long modpow(long long a, long long n, long long mod) { // a^n mod assert(n >= 0); if(mod == 1) return 0LL; long long res = 1; while(n > 0) { if(n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a * x % mod == __gcd(a,mod)なるxを返す // a が modの倍数でないことが条件 long long modinv(long long a, long long mod) { long long b = mod, u = 1, v = 0; while(b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if(u < 0) u += mod; return u; } vvll comb(100, vll(100, -1)); long long com(long long n, long long k) { //普通の二項計数 assert(n < 100 && k < 100); if(n < k || k < 0 || n < 0) return 0; if(comb[n][k] != -1) return comb[n][k]; ll res; if(n - k < k) res = com(n, n - k); else if(k == 0) res = 1; else res = com(n - 1, k - 1) + com(n - 1, k); comb[n][k] = res; return res; } const int MAX = 5100000; // about 300ms, when MAX = 3*10^7 : 1900ms long long fac[MAX], finv[MAX], inv[MAX]; void cominit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long commod(long long n, long long k) { if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } long long pmod(long long n, long long k) { if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n] * finv[n - k] % mod; } // n個の区別しないボールを区別するk個の箱に入れる方法の総数 long long hmod(long long n, long long k) { // 重複組み合わせ return commod(n + k - 1, n); } #pragma endregion #pragma region input #define VEC(type, name, size) \ vector name(size); \ INPUT(name) #define VVEC(type, name, h, w) \ vector> name(h, vector(w)); \ INPUT(name) #define INT(...) \ int __VA_ARGS__; \ INPUT(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ INPUT(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ INPUT(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ INPUT(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ INPUT(__VA_ARGS__) #define LD(...) \ long double __VA_ARGS__; \ INPUT(__VA_ARGS__) template void scan(T &a) { cin >> a; } template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(pair &p) { scan(p.first); scan(p.second); } void INPUT() {} template void INPUT(Head &head, Tail &... tail) { scan(head); INPUT(tail...); } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } #pragma endregion #pragma region debug #pragma region output template std::ostream &std::operator<<(std::ostream &os, const std::pair &p) { os << p.first << " " << p.second; return os; } template std::ostream &std::operator<<(std::ostream &os, const std::vector &v) { for(int i = 0; i < (int)v.size(); i++) { if(i) os << " "; os << v[i]; } return os; } #pragma endregion #pragma region view template void view(const T e) { std::cerr << e; } template void view(const std::pair &p) { std::cerr << "("; view(p.first); std::cerr << ", "; view(p.second); std::cerr << ")"; } template void view(const std::set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::unordered_set &s) { if(s.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector &v) { if(v.empty()) { std::cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector> &vv) { std::cerr << "{\n"; for(const auto &v : vv) { std::cerr << "\t"; view(v); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::vector> &v) { std::cerr << "{\n"; for(const auto &c : v) { std::cerr << "\t("; view(c.first); std::cerr << ", "; view(c.second); std::cerr << ")\n"; } std::cerr << "}"; } template void view(const std::map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } template void view(const std::unordered_map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); std::cerr << "] : "; view(t.second); std::cerr << '\n'; } std::cerr << "}"; } #pragma endregion // when debugging : g++ foo.cpp -DLOCAL #ifdef LOCAL void debug_out() {} template void debug_out(Head H, Tail... T) { view(H); std::cerr << ", "; debug_out(T...); } #define debug(...) \ do { \ std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \ debug_out(__VA_ARGS__); \ std::cerr << "\b\b]\n"; \ } while(0) #define dump(x) \ do { \ std::cerr << __LINE__ << " " << #x << " : "; \ view(x); \ std::cerr << '\n'; \ } while(0) #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #pragma endregion int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); srand((unsigned)time(NULL)); INT(r, c); INT(a, b, d, e); a--, b--, d--, e--; VEC(string, s, r); vvi dist(r, vi(c, -1)); dist[a][b] = 0; queue> q; q.emplace(a, b); int x, y; while(!q.empty()) { tie(x, y) = q.front(); if(x == d && y == e) drop(dist[x][y]); q.pop(); for(int dir = 0; dir < 4; dir++) { int u = x + dx[dir]; int v = y + dy[dir]; // u += r; // u %= r; // v += c; // v %= c; if(u < 0 || u >= r || v < 0 || v >= c) continue; if(dist[u][v] != -1 || s[u][v] == '#') continue; dist[u][v] = dist[x][y] + 1; q.emplace(u, v); } } return 0; }