#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; using pii = pair; using pll = pair; using tp3 = tuple; using Mat = vector>; constexpr int INF = 1 << 28; constexpr ll INFL = 1ll << 60; constexpr int dh[4] = { 0,1,0,-1 }; constexpr int dw[4] = { -1,0,1,0 }; bool isin(const int H, const int W, const int h, const int w) { return 0 <= h && h < H && 0 <= w && w < W; } template T minch(T& l, T r) { return l = min(l, r); } template T maxch(T& l, T r) { return l = max(l, r); } template void output(const T& val) { cout << val << endl; } template void output(const vector& vec, const bool newline = false) { for (const T& val : vec)cout << val << (newline ? '\n' : ' '); cout << endl; } template void output(const vector>& mat) { for (const auto& row : mat)output(row); } // ============ template finished ============ int main() { int R, C; pii s, g; cin >> R >> C >> s.first >> s.second >> g.first >> g.second; s.first--, s.second--, g.first--, g.second--; cout << abs(s.first - g.first) + abs(s.second - g.second) << endl; return 0; vector c(R); rep(i, R)cin >> c[i]; Mat d(R, vector(C, INFL)); priority_queue, greater> pq; d[s.first][s.second] = 0; pq.push(make_tuple(0, s.first, s.second)); while (!pq.empty()) { int p, h, w; tie(p, h, w) = pq.top(); pq.pop(); rep(dir, 4) { int nr = h + dh[dir], nc = w + dw[dir]; if (nr < 0)nr = R - 1; if (R <= nr)nr = 0; if (nc < 0)nc = C - 1; if (C <= nc)nc = 0; if (c[nr][nc] == '#' || d[nr][nc] <= p + 1)continue; d[nr][nc] = p + 1; pq.push(make_tuple(p + 1, nr, nc)); } } output(d[g.first][g.second]); return 0; }