from collections import deque H, W = map(int, input().split()) A, B = map(int, input().split()) top, left, bottom, right = map(int, input().split()) P, Q = map(int, input().split()) df = [[-1, 0],[0, -1], [1, 0], [0, 1]] def bfs(x, y): dists = [[-1] * (W) for _ in range(H)] dists[x][y] = 0 que = deque([[x, y]]) while que: cx, cy = que.popleft() for dx, dy in df: nx, ny = cx+dx, cy+dy if nx < 0 or nx >= H or ny < 0 or ny >= W: continue if dists[nx][ny] != -1: continue dists[nx][ny] = dists[cx][cy]+1 que.append([nx, ny]) return dists d1 = bfs(A-1, B-1) d2 = bfs(P-1, Q-1) d = abs(A-P) + abs(B-Q) ans = 10**18 for i in range(top-1, bottom): for j in range(left-1, right): ans = min(ans, d1[i][j] + d2[i][j]+d) print(ans)