from collections import deque DIRECT = [0, 1, 0, -1, 0] H, W = map(int, input().split()) A, B = map(lambda x: int(x) - 1, input().split()) t, l, b, r = map(lambda x: int(x) - 1, input().split()) P, Q = map(lambda x: int(x) - 1, input().split()) dist = [[H * W + 1] * W for _ in range(H)] que = deque([(A, B)]) dist[A][B] = 0 destroy_point = (-1, -1) step1 = -1 if t <= A <= b and l <= B <= r: destroy_point = (A, B) step1 = 0 else: # step 1 while que: ci, cj = que.popleft() for d in range(4): di, dj = DIRECT[d], DIRECT[d + 1] ni, nj = ci + di, cj + dj if not (0 <= ni < H and 0 <= nj < W): continue if dist[ni][nj] <= dist[ci][cj] + 1: continue dist[ni][nj] = dist[ci][cj] + 1 if t <= ni <= b and l <= nj <= r: step1 = dist[ni][nj] destroy_point = (ni, nj) que = [] break que.append((ni, nj)) # step 2 step2 = 0 # step 3 que = deque([destroy_point]) dist = [[H * W + 1] * W for _ in range(H)] di, dj = destroy_point dist[di][dj] = 0 step3 = -1 if destroy_point == (P, Q): step3 = 0 else: while que: ci, cj = que.popleft() for d in range(4): di, dj = DIRECT[d], DIRECT[d + 1] ni, nj = ci + di, cj + dj if not (0 <= ni < H and 0 <= nj < W): continue if dist[ni][nj] <= dist[ci][cj] + 1: continue dist[ni][nj] = dist[ci][cj] + 1 if (P, Q) == (ni, nj): step3 = dist[ni][nj] que = [] break que.append((ni, nj)) # step 4 que = deque([(P, Q)]) dist = [[H * W + 1] * W for _ in range(H)] dist[P][Q] = 0 step4 = -1 if (P, Q) == (A, B): step4 = 0 else: while que: ci, cj = que.popleft() for d in range(4): di, dj = DIRECT[d], DIRECT[d + 1] ni, nj = ci + di, cj + dj if not (0 <= ni < H and 0 <= nj < W): continue if dist[ni][nj] <= dist[ci][cj] + 1: continue dist[ni][nj] = dist[ci][cj] + 1 if (A, B) == (ni, nj): step4 = dist[ni][nj] que = [] break que.append((ni, nj)) print(step1 + step2 + step3 + step4)