#include using namespace std; struct cell { int x, y; int dis; cell() {} cell(int x, int y, int dis) : x(x), y(y), dis(dis) {} }; // Utility method returns true if (x, y) lies inside Board bool isInside(int x, int y, int N) { if (x >= 1 && x <= N && y >= 1 && y <= N) return true; return false; } int minStepToReachTarget(int knightPos[], int targetPos[], int N) { int dx[] = {-2, -2, 0, 2, 2, 0}; int dy[] = {-1, 1, 2, 1, -1, -2}; queue q; q.push(cell(knightPos[0], knightPos[1], 0)); cell t; int x, y; bool visit[N + 1][N + 1]; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) visit[i][j] = false; visit[knightPos[0]][knightPos[1]] = true; while (!q.empty()) { t = q.front(); q.pop(); visit[t.x][t.y] = true; if (t.x == targetPos[0] && t.y == targetPos[1]) return t.dis; for (int i = 0; i < 8; i++) { x = t.x + dx[i]; y = t.y + dy[i]; if (isInside(x, y, N) && !visit[x][y]){ q.push(cell(x, y, t.dis + 1)); } } } return false; } int main() { int N; cin >> N; int i_start; int j_start; int i_end; int j_end; int knightPos[2]; int targetPos[2]; cin >> i_start >> j_start >> i_end >> j_end; knightPos[0] = i_start; knightPos[1] = j_start; targetPos[0] = i_end; targetPos[1] = j_end; cout << minStepToReachTarget(knightPos, targetPos, N); return 0; }