#include using namespace std; int R, C, sy, sx, gy, gx; vector> c; vector> ans; void bfs() { queue> q; q.push(make_pair(sy, sx)); ans[sy][sx] = 0; while (!q.empty()) { int y = q.front().first, x = q.front().second; q.pop(); int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1}; for (int i = 0; i < 4; ++i) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 || R <= ny || nx < 0 || C <= nx) { continue; } if (ans[ny][nx] != -1) { continue; } q.push(make_pair(ny, nx)); ans[ny][nx] = ans[y][x] + 1; } } } int main() { cin >> R >> C >> sy >> sx >> gy >> gx; c.resize(R, vector(C)), ans.resize(R, vector(C, -1)); for (int i = 0; i < R; ++i) { for (int j = 0; j < C; ++j) { cin >> c[i][j]; } } --sy, --sx, --gy, --gx; bfs(); cout << ans[gy][gx] << endl; }