#include using namespace std; auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; }; template struct dijkstra { struct edge { int to; T w; }; const T inf = numeric_limits::max(); vector> g; dijkstra(int n) : g(n) {} void add(int from, int to, T w, bool directed = true) { g[from].push_back({to, w}); if (not directed) g[to].push_back({from, w}); } vector run(int s) const { vector d(g.size(), inf); priority_queue, vector>, greater<>> pq; pq.emplace(d[s] = 0, s); while (not pq.empty()) { T dv; int v; tie(dv, v) = pq.top(), pq.pop(); if (dv != d[v]) continue; for (auto e : g[v]) if (chmin(d[e.to], dv + e.w)) pq.emplace(d[e.to], e.to); } return d; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin >> h >> w; int sx, sy; cin >> sx >> sy; --sx, --sy; int gx, gy; cin >> gx >> gy; --gx, --gy; vector s(h); for (auto&& e : s) { cin >> e; } auto $ = [&](int i, int j) { return i * w + j; }; dijkstra g(h * w); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (i + 1 < h) { g.add($(i, j), $(i + 1, j), 1, false); } if (j + 1 < w) { g.add($(i, j), $(i, j + 1), 1, false); } } } cout << g.run($(sx, sy))[$(gx, gy)] << '\n'; }