from collections import deque MAX_INT = 10 ** 18 def solve(H, W, s_r, s_c): dists = [[MAX_INT for _ in range(W)] for _ in range(H)] queue = deque() dists[s_r][s_c] = 0 queue.append((s_r, s_c)) while len(queue) > 0: r, c = queue.popleft() for dr, dc in ((-1, 0), (1, 0), (0 , -1), (0, 1)): new_r = dr + r new_c = dc + c if 0 <= new_r < H and 0 <= new_c < W: if dists[new_r][new_c] == MAX_INT: dists[new_r][new_c] = dists[r][c] + 1 queue.append((new_r, new_c)) return dists def main(): H, W = map(int, input().split()) A, B = map(int, input().split()) R1, C1, R2, C2 = map(int, input().split()) P, Q = map(int, input().split()) dists_ab = solve(H, W, A - 1, B - 1) dists_pq = solve(H, W, P - 1, Q - 1) answer = MAX_INT for r in range(R1 - 1, R2): for c in range(C1 - 1, C2): ans = dists_ab[r][c] + dists_pq[r][c] + dists_pq[A - 1][B - 1] answer = min(ans, answer) print(answer) if __name__ == "__main__": main() # main2()