h, w = list(map(int, input().split())) sy, sx = list(map(lambda x: int(x)-1, input().split())) gy0, gx0, gy1, gx1 = list(map(lambda x: int(x), input().split())) gy0, gx0 = gy0-1, gx0-1 ty, tx = list(map(lambda x: int(x)-1, input().split())) dp = [[[-1, -1, -1] for _ in range(w)] for _ in range(h)] dp[sy][sx][0] = 0 B = [(sy, sx, 0)] c = 0 while A:= B: B = [] while A: y, x, a = A.pop() if not a and gy0 <= y < gy1 and gx0 <= x < gx1 and dp[y][x][1] == -1: dp[y][x][1] = c A.append((y, x, 1)) if a == 1 and y == ty and x == tx and dp[y][x][2] == -1: dp[y][x][2] = c A.append((y, x, 2)) for ny, nx in [(y, x-1), (y, x+1), (y-1, x), (y+1, x)]: if 0 <= ny < h and 0 <= nx < w and dp[ny][nx][a] == -1: dp[ny][nx][a] = c+1 B.append((ny, nx, a)) c += 1 print(dp[sy][sx][2])