import java.util.*; public class java4 { static class cell { int x, y; int dis; public cell(int x, int y, int dis) { this.x = x; this.y = y; this.dis = dis; } } static boolean isInside(int x, int y, int N) { if (x >= 0 && x < N && y >= 0 && y < N) return true; return false; } static int minStepToReachTarget(int knightPos[], int targetPos[], int N) { int dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 }; int dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 }; Queue q = new LinkedList<>(); q.add(new cell(knightPos[0], knightPos[1], 0)); cell t; int x, y; boolean visit[][] = new boolean [N + 1][N + 1]; visit[knightPos[0]][knightPos[1]] = true; while (!q.isEmpty()) { t = q.poll(); 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]) { visit[x][y] = true; q.add(new cell(x, y, t.dis + 1)); } } } return Integer.MAX_VALUE; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = Integer.parseInt(scanner.nextLine());; int knightPos[] = { Integer.parseInt(scanner.nextLine()), Integer.parseInt(scanner.nextLine()) }; int targetPos[] = { Integer.parseInt(scanner.nextLine()), Integer.parseInt(scanner.nextLine()) }; System.out.println( minStepToReachTarget(knightPos, targetPos, N)); } }