from collections import deque H, W = map(int, input().split()) A, B = map(lambda x: int(x)-1, input().split()) R1, C1, R2, C2 = map(lambda x: int(x)-1, input().split()) P, Q = map(lambda x: int(x)-1, input().split()) vector = [(1, 0), (0, 1), (-1, 0), (0, -1)] dist1 = [[10**4]*W for _ in range(H)] dist1[A][B] = 0 queue = deque() queue.append((A, B)) while queue: i, j = queue.popleft() for vi, vj in vector: if 0 <= i+vi < H and 0 <= j + vj < W and dist1[i+vi][j+vj] == 10**4: dist1[i+vi][j+vj] = dist1[i][j] + 1 queue.append((i+vi, j+vj)) dist2 = [[10**4]*W for _ in range(H)] dist2[P][Q] = 0 queue = deque() queue.append((P, Q)) while queue: i, j = queue.popleft() for vi, vj in vector: if 0 <= i+vi < H and 0 <= j + vj < W and dist2[i+vi][j+vj] == 10**4: dist2[i+vi][j+vj] = dist2[i][j] + 1 queue.append((i+vi, j+vj)) ans = 10**18 for i in range(R1, R2+1): for j in range(C1, C2+1): ans = min(ans, dist1[i][j] + dist2[i][j] + dist2[A][B]) print(ans)