There is an N\times N grid, with rows numbered 1 to N from top to bottom and columns numbered 1 to N from left to right. The cell at the intersection of the i-th row and j-th column is denoted (i, j).

Chef is standing at position (x_{1}, y_{1}) of this grid. He wants to reach position (x_{2}, y_{2}).

To achieve this, he can take either horizontal or vertical steps.
That is, from cell (x, y), he can move to one of (x-1, y), (x+1, y), (x, y-1), (x, y+1).

However, Chef does not need to stay within the grid, that is, he can choose to go outside it if he so wishes.

The cost of a horizontal/vertical step is 0 if both the starting and ending square lie outside the grid, and 1 otherwise.  
Formally, suppose Chef takes one step from (x, y) to (x', y'). Then,
If (x, y) lies within the grid, the cost of this step is 1
If (x', y') lies within the grid, the cost of this step is 1
If both the above don't hold, the cost of this step is 0

Find the minimum cost to reach (x_{2}, y_{2}) from (x_{1}, y_{1}).

------ Input Format ------ 

- The first line of input will contain a single integer T, denoting the number of
test cases.
- Each test case consists of one line of input containing five space-separated integers N, x_{1}, y_{1}, x_{2}, and y_{2}.

------ Output Format ------ 

For each test case, output on a new line the minimum cost of traveling from (x_{1}, y_{1}) to (x_{2}, y_{2}).

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤ x_{1},y_{1},x_{2},y_{2} ≤ N$

----- Sample Input 1 ------ 
6
3 1 1 2 3
5 2 3 3 4
3 1 1 2 1
6 2 1 5 5
8 3 4 6 7
10 4 5 9 9

----- Sample Output 1 ------ 
2
2
1
3
5
6
----- explanation 1 ------ 
Test case $1$: One optimal sequence of moves is as follows:
- Chef moves from $(1, 1)$ to $(0, 1)$ with a cost of $1$. Note that $(0, 1)$ is outside the grid.
- Now, Chef can move on the outside from $(0, 1)$ to $(2, 4)$ with no cost.
- Finally, Chef moves from $(2, 4)$ to $(2, 3)$ with cost $1$, for a total cost of $2$.

Test case $2$: Chef can move as follows: $(2, 3) \to (3, 3) \to (3, 4)$, for a cost of $2$.

Test case $4$: Chef can move as follows: $(2, 1) \to (2, 0)$ with cost $1$, then $(2, 0) \to (5, 7)$ on the outside with cost $0$, then $(5, 7) \to (5, 6) \to (5, 5)$ with cost $2$ for a total cost of $3$.

Difficulty: EASY
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	(a, x1, y1, x2, y2) = map(int, input().split())
	iny = abs(x2 - x1) + abs(y2 - y1)
	f = min(x1, a + 1 - x1, y1, a + 1 - y1)
	l = min(x2, a + 1 - x2, y2, a + 1 - y2)
	out = f + l
	print(min(iny, out))


for _ in range(int(input())):
	(n, a, b, c, d) = map(int, input().split())
	w1 = abs(c - a) + abs(d - b)
	w2 = min(a, b, n - a + 1, n - b + 1) + min(c, d, n - c + 1, n - d + 1)
	print(min(w1, w2))


def getOutside(n, x1, y1, x2, y2):
	minx1 = min(abs(x1), abs(x1 - n - 1))
	minx2 = min(abs(x2), abs(x2 - n - 1))
	miny1 = min(abs(y1), abs(y1 - n - 1))
	miny2 = min(abs(y2), abs(y2 - n - 1))
	min2 = min(minx2, miny2)
	min1 = min(minx1, miny1)
	return min1 + min2
t = int(input())
for ni in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	inside = abs(x2 - x1) + abs(y2 - y1)
	outside = getOutside(n, x1, y1, x2, y2)
	print(min(outside, inside))


for _ in range(int(input())):
	(a, b, c, d, e) = map(int, input().split())
	g = min(b, a - b + 1)
	h = min(c, a - c + 1)
	i = min(d, a - d + 1)
	j = min(e, a - e + 1)
	k = abs(d - b) + abs(e - c)
	l = min(i, j) + min(g, h)
	print(min(k, l))


for _ in range(int(input())):
	(N, x1, y1, x2, y2) = map(int, input().split())
	cost_internal = abs(x2 - x1) + abs(y2 - y1)
	cost_external = min(x1 - 1, N - x1, y1 - 1, N - y1) + min(x2 - 1, N - x2, y2 - 1, N - y2) + 2
	print(min(cost_internal, cost_external))


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	cost1 = abs(x2 - x1) + abs(y2 - y1)
	cost2 = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(cost1, cost2))


from math import *
t = int(input())
for _ in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	c1 = abs(x2 - x1) + abs(y2 - y1)
	c2 = min(n - max(x1, y1) + 1, min(x1, y1)) + min(min(x2, y2), n - max(x2, y2) + 1)
	print(min(c1, c2))


for _ in range(int(input())):
	(N, x1, y1, x2, y2) = map(int, input().split())
	z1 = min(x1, y1, N - x1 + 1, N - y1 + 1)
	z1 += min(x2, y2, N - x2 + 1, N - y2 + 1)
	z2 = abs(x2 - x1) + abs(y2 - y1)
	print(min(z1, z2))


T = int(input())
for i in range(0, T):
	(N, x1, y1, x2, y2) = map(int, input().split())
	X = min(N - x1 + 1, x1)
	Y = min(N - y1 + 1, y1)
	X2 = min(N - x2 + 1, x2)
	Y2 = min(N - y2 + 1, y2)
	print(min(abs(x1 - x2) + abs(y1 - y2), min(X, Y) + min(X2, Y2)))


t = int(input())
for _ in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	step1 = min(x1, y1, n - x1 + 1, n - y1 + 1)
	step2 = min(x2, y2, n - x2 + 1, n - y2 + 1)
	ans1 = step1 + step2
	ans2 = abs(x2 - x1) + abs(y2 - y1)
	print(min(ans1, ans2))


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	inside_cost = abs(x1 - x2) + abs(y1 - y2)
	outside_cost = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(inside_cost, outside_cost))


T = int(input())
while T:
	T -= 1
	(n, x1, y1, x2, y2) = list(map(int, input().split()))
	print(min(abs(x1 - x2) + abs(y1 - y2), min(x1, n - x1 + 1, y1, n - y1 + 1) + min(x2, n - x2 + 1, y2, n - y2 + 1)))


t = int(input())
for i in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	in_cost = abs(x1 - x2) + abs(y1 - y2)
	out_cost = min(x1, y1, n - x1 + 1, n - y1 + 1) + min(x2, y2, n - x2 + 1, n - y2 + 1)
	print(min(out_cost, in_cost))


a = int(input(''))
for i in range(a):
	(N, x1, y1, x2, y2) = [int(x) for x in input('').split()]
	t = min(x1, y1, N - x1 + 1, N - y1 + 1) + min(x2, y2, N - x2 + 1, N - y2 + 1)
	u = abs(x2 - x1) + abs(y2 - y1)
	print(min(t, u))


testcases = int(input())
for eachcase in range(testcases):
	(length, row1, col1, row2, col2) = map(int, input().split())
	legal = abs(row1 - row2) + abs(col1 - col2)
	illegal = 2 + min(row1 - 1, col1 - 1, length - row1, length - col1) + min(row2 - 1, col2 - 1, length - row2, length - col2)
	print(min(legal, illegal))


for i in range(int(input())):
	(m, a, b, c, d) = map(int, input().split())
	mini1 = min(list([a - 1, m - a, b - 1, m - b]))
	mini2 = min(list([c - 1, m - c, d - 1, m - d]))
	mini1 += 1
	mini2 += 1
	mini3 = abs(c - a) + abs(d - b)
	print(min(mini1 + mini2, mini3))


for t in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	c1 = min(n - x1, n - y1, x1 - 1, y1 - 1) + min(n - x2, n - y2, x2 - 1, y2 - 1) + 2
	c2 = abs(x2 - x1) + abs(y2 - y1)
	if c1 < c2:
		print(c1)
	else:
		print(c2)


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	direct = abs(x1 - x2) + abs(y1 - y2)
	to_edge = min(x1, y1, n - x1 + 1, n - y1 + 1)
	from_edge = min(x2, y2, n - x2 + 1, n - y2 + 1)
	print(min(direct, to_edge + from_edge))


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	l = abs(x1 - x2) + abs(y1 - y2)
	if l < min(x1, y1, n - y1 + 1, n - x1 + 1) + min(x2, y2, n - y2 + 1, n - x2 + 1):
		print(l)
	else:
		print(min(x1, y1, n - y1 + 1, n - x1 + 1) + min(x2, y2, n - y2 + 1, n - x2 + 1))


for x in range(int(input())):
	(n, a, b, c, d) = map(int, input().split())
	e = 0
	if abs(a - (n + 1) / 2) < abs(b - (n + 1) / 2):
		if b < (n + 1) / 2:
			e += b
		else:
			e += n - b + 1
	elif a < (n + 1) / 2:
		e += a
	else:
		e += n - a + 1
	if abs(c - (n + 1) / 2) < abs(d - (n + 1) / 2):
		if d < (n + 1) / 2:
			e += d
		else:
			e += n - d + 1
	elif c < (n + 1) / 2:
		e += c
	else:
		e += n - c + 1
	f = abs(c - a) + abs(b - d)
	if e > f:
		print(f)
	else:
		print(e)


T = int(input())
for i in range(0, T):
	Lis = [int(k) for k in input().split()]
	N = Lis[0]
	x1 = Lis[1]
	y1 = Lis[2]
	x2 = Lis[3]
	y2 = Lis[4]
	d = abs(x1 - x2) + abs(y1 - y2)
	l = min(x1, y1, N + 1 - x1, N + 1 - y1) + min(x2, y2, N + 1 - x2, N + 1 - y2)
	if d < l:
		print(d)
	else:
		print(l)


T = int(input())
for t in range(T):
	s = input().split()
	n = int(s[0])
	x1 = int(s[1])
	y1 = int(s[2])
	x2 = int(s[3])
	y2 = int(s[4])
	dis1 = 0
	dis2 = abs(y2 - y1) + abs(x2 - x1)
	dis1 = min(min(x1 - 1, n - x1), min(y1 - 1, n - y1)) + min(min(x2 - 1, n - x2), min(y2 - 1, n - y2)) + 2
	print(min(dis1, dis2))


t = int(input())
for i in range(t):
	(N, x1, y1, x2, y2) = map(int, input().split())
	internal_cost = abs(x2 - x1) + abs(y2 - y1)
	external_cost = min(x1 - 1, N - x1, y1 - 1, N - y1) + min(x2 - 1, N - x2, y2 - 1, N - y2) + 2
	print(min(internal_cost, external_cost))


def borderPath(N, x, y):
	N += 1
	xd = x if N - x >= x else N - x
	yd = y if N - y >= y else N - y
	return xd if xd <= yd else yd
for _ in range(int(input())):
	(N, x1, y1, x2, y2) = map(int, input().split())
	internalPath = abs(x1 - x2) + abs(y1 - y2)
	externalPath = borderPath(N, x1, y1) + borderPath(N, x2, y2)
	output = internalPath if internalPath <= externalPath else externalPath
	print(output)


def lenToBorder(n, x, y):
	n += 1
	xd = x if n - x >= x else n - x
	yd = y if n - y >= y else n - y
	return xd if xd <= yd else yd
t = int(input())
for i_ in range(t):
	(n, x1, y1, x2, y2) = [int(a) for a in input().split()]
	landlen = abs(x1 - x2) + abs(y1 - y2)
	borlen = lenToBorder(n, x1, y1) + lenToBorder(n, x2, y2)
	ans = landlen if landlen <= borlen else borlen
	print(ans)


tests = input()

def dist(x, y):
	return x - y if x - y >= 0 else y - x

def inbound(x1, y1, x2, y2):
	score = 0
	score += dist(x1, x2)
	score += dist(y1, y2)
	return score

def outbound(N, x1, y1, x2, y2):
	score = 0
	bestX1 = dist(x1, 0) if dist(x1, 0) <= dist(x1, N + 1) else dist(x1, N + 1)
	bestY1 = dist(y1, 0) if dist(y1, 0) <= dist(y1, N + 1) else dist(y1, N + 1)
	bestX2 = dist(x2, 0) if dist(x2, 0) <= dist(x2, N + 1) else dist(x2, N + 1)
	bestY2 = dist(y2, 0) if dist(y2, 0) <= dist(y2, N + 1) else dist(y2, N + 1)
	score += bestX1 if bestX1 <= bestY1 else bestY1
	score += bestX2 if bestX2 <= bestY2 else bestY2
	return score
for i in range(int(tests)):
	(A, B, C, D, E) = input().split(' ')
	(A, B, C, D, E) = (int(A), int(B), int(C), int(D), int(E))
	inb = inbound(B, C, D, E)
	outb = outbound(A, B, C, D, E)
	print(inb if inb <= outb else outb)


for i in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	p = abs(x1 - x2) + abs(y1 - y2)
	v = min(x1, y1, n - x1 + 1, n - y1 + 1) + min(x2, y2, n - x2 + 1, n - y2 + 1)
	print(min(p, v))


T = int(input())
for i in range(0, T):
	(N, x1, y1, x2, y2) = map(int, input().split())
	Cost = 0
	print(min(abs(x2 - x1) + abs(y2 - y1), min(x1, N + 1 - x1, y1, N + 1 - y1) + min(x2, N + 1 - x2, y2, N + 1 - y2)))


def func(n, x1, y1, x2, y2):
	print(min(abs(x2 - x1) + abs(y2 - y1), min(x1, abs(n - x1 + 1), y1, abs(n - y1 + 1)) + min(x2, abs(n - x2 + 1), y2, abs(n - y2 + 1))))
n = int(input())
for i in range(n):
	(q, w, e, r, t) = map(int, input().split())
	func(q, w, e, r, t)


k = int(input())
for i in range(k):
	(n, x1, y1, x2, y2) = map(int, input().split())
	intcost = abs(x2 - x1) + abs(y2 - y1)
	extcost = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(intcost, extcost))


t = int(input())
for i in range(0, t):
	(a, b, c, x, y) = map(int, input().split(' '))
	s = 0
	d1 = min(b - 1, a - b) + 1
	d2 = min(c - 1, a - c) + 1
	d11 = min(x - 1, a - x) + 1
	d21 = min(y - 1, a - y) + 1
	s = min((d11, d21)) + min((d1, d2))
	r = 0
	d = abs(x - b)
	e = abs(y - c)
	r = d + e
	if s < r:
		print(s)
	else:
		print(r)


for _ in range(int(input())):
	(n, x1, y1, x, y) = tuple(map(int, input().split()))
	incost = abs(x1 - x) + abs(y1 - y)
	dx = min(x - 1, n - x) + 1
	dy = min(y - 1, n - y) + 1
	dx1 = min(x1 - 1, n - x1) + 1
	dy1 = min(y1 - 1, n - y1) + 1
	outcost = min((dx1, dy1)) + min((dx, dy))
	print(min((incost, outcost)))


T = int(input())
for i in range(T):
	(n, x, y, p, q) = map(int, input().split())
	s1 = abs(p - x) + abs(q - y)
	s2 = min(x - 1, n - x, y - 1, n - y) + min(p - 1, n - p, q - 1, n - q) + 2
	print(min(s1, s2))


def find_distance(grid_size, x1, y1, x2, y2):
	cost = 0
	cost_x1 = 0
	if x1 <= int(grid_size / 2):
		cost_x1 = x1
	else:
		cost_x1 = grid_size + 1 - x1
	cost_y1 = 0
	if y1 <= int(grid_size / 2):
		cost_y1 = y1
	else:
		cost_y1 = grid_size + 1 - y1
	cost_x2 = 0
	if x2 <= int(grid_size / 2):
		cost_x2 = x2
	else:
		cost_x2 = grid_size + 1 - x2
	cost_y2 = 0
	if y2 <= int(grid_size / 2):
		cost_y2 = y2
	else:
		cost_y2 = grid_size + 1 - y2
	cost1 = min(cost_x1, cost_y1)
	cost2 = min(cost_x2, cost_y2)
	cost = min(cost1 + cost2, abs(x1 - x2) + abs(y1 - y2))
	return cost
inp = input()
num_cases = int(inp)
for i in range(num_cases):
	inp = input()
	inp = inp.split(' ')
	grid_size = int(inp[0])
	x1 = int(inp[1])
	y1 = int(inp[2])
	x2 = int(inp[3])
	y2 = int(inp[4])
	distance = find_distance(grid_size, x1, y1, x2, y2)
	print(distance)


T = int(input())
for i in range(0, T):
	(n, x1, y1, x2, y2) = map(int, input().split())
	cost = 0
	a = min(abs(x1 - 0), abs(n - x1) + 1, abs(y1 - 0), abs(n - y1) + 1)
	cost += a
	b = min(abs(x2 - 0), abs(n - x2) + 1, abs(y2 - 0), abs(n - y2) + 1)
	cost += b
	c = abs(y2 - y1) + abs(x2 - x1)
	print(min(cost, c))


t = int(input())
while t:
	t = t - 1
	(n, x1, y1, x2, y2) = map(int, input().split())
	pathIn = abs(x2 - x1) + abs(y2 - y1)
	pathOut = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(pathIn, pathOut))


T = int(input())
for _ in range(T):
	(N, x1, y1, x2, y2) = map(int, input().split(' '))
	inner_moves = abs(x2 - x1) + abs(y2 - y1)
	move_to_outside = min(x1, y1, N - x1 + 1, N - y1 + 1) + min(x2, y2, N - x2 + 1, N - y2 + 1)
	print(min(inner_moves, move_to_outside))


t = int(input())
while t != 0:
	(n, x1, y1, x2, y2) = map(int, input().split())
	min1 = abs(x2 - x1) + abs(y2 - y1)
	min2 = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(min1, min2))
	t = t - 1


t = int(input())
for i in range(t):
	(n, x1, y1, x2, y2) = input().split(' ')
	n = int(n)
	x1 = int(x1)
	y1 = int(y1)
	x2 = int(x2)
	y2 = int(y2)
	minx = min(x1, x2)
	maxx = max(x1, x2)
	miny = min(y1, y2)
	maxy = max(y1, y2)
	nx = maxx - minx
	ny = maxy - miny
	sumi = nx + ny
	minxy = min(x1, y1)
	maxxy = max(x1, y1)
	minx2y = min(x2, y2)
	maxx2y = max(x2, y2)
	out = min(minxy, n - maxxy + 1)
	inp = min(minx2y, n - maxx2y + 1)
	ans = out + inp
	print(min(sumi, ans))


T = int(input())
for _ in range(T):
	(N, X1, Y1, X2, Y2) = map(int, input().split())
	P1 = abs(X2 - X1) + abs(Y2 - Y1)
	P2 = min(min(X1, N + 1 - X1), min(Y1, N + 1 - Y1)) + min(min(X2, N + 1 - X2), min(Y2, N + 1 - Y2))
	print(min(P1, P2))


testcase = int(input())
for i in range(testcase):
	(n, x1, y1, x2, y2) = map(int, input().split())
	cost1 = abs(x2 - x1) + abs(y2 - y1)
	cost2 = min(x1, y1, n + 1 - x1, n + 1 - y1) + min(x2, y2, n + 1 - x2, n + 1 - y2)
	print(cost1 if cost1 < cost2 else cost2)


for _ in range(int(input())):
	(n, x, y, a, b) = map(int, input().split())
	ans = min(abs(x - a) + abs(y - b), min(x, y, n + 1 - x, n + 1 - y) + min(a, b, n + 1 - a, n + 1 - b))
	print(ans)


t = int(input())
for _ in range(t):
	(N, x1, y1, x2, y2) = map(int, input().split())
	cost_i = abs(x2 - x1) + abs(y2 - y1)
	cost_e = min(x1 - 1, N - x1, y1 - 1, N - y1) + min(x2 - 1, N - x2, y2 - 1, N - y2) + 2
	print(min(cost_i, cost_e))


t = int(input())
while t > 0:
	(n, x1, y1, x2, y2) = map(int, input().split())
	res = 0
	in_dist = abs(x2 - x1) + abs(y2 - y1)
	od = 0
	op1 = min(x1 - 1, n - x1)
	op2 = min(y1 - 1, n - y1)
	op1_ = min(x2 - 1, n - x2)
	op2_ = min(y2 - 1, n - y2)
	od = min(op1, op2) + min(op1_, op2_)
	out_dist = od + 2
	res = min(in_dist, out_dist)
	print(res)
	t -= 1


for t in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	opt_min = abs(x1 - x2) + abs(y1 - y2)
	(xy1, xy2) = (min(x1 - 1, y1 - 1, n - x1, n - y1), min(x2 - 1, y2 - 1, n - x2, n - y2))
	print(min(opt_min, xy1 + xy2 + 2))


def Haptprogram():
	(n, x1, y1, x2, y2) = map(int, input().split())
	costInternal = abs(x1 - x2) + abs(y1 - y2)
	costExternal = min(x1 - 1, n - x1, y1 - 1, n - y1) + min(x2 - 1, n - x2, y2 - 1, n - y2) + 2
	print(min(costInternal, costExternal))
t = int(input())
while t > 0:
	Haptprogram()
	t = t - 1


for _ in range(int(input())):
	(n, x, y, a, b) = map(int, input().split())
	res1 = abs(a - x) + abs(b - y)
	k = n - x
	l = n - y
	m = n - a
	n = n - b
	res2 = min(k + 1, l + 1, x, y) + min(m + 1, n + 1, a, b)
	print(min(res1, res2))


for _ in range(int(input())):
	(N, x1, y1, x2, y2) = map(int, input().split())
	a = abs(x1 - x2)
	d = abs(y1 - y2)
	c = a + d
	c1 = min(x1 - 1, N - x1, y1 - 1, N - y1) + min(x2 - 1, N - x2, y2 - 1, N - y2) + 2
	print(min(c, c1))


from collections import Counter, defaultdict
import sys
import math

def sol(arr):
	N = arr[0]
	(x1, y1, x2, y2) = (arr[1], arr[2], arr[3], arr[4])
	min_x1 = min(x1, N - x1 + 1)
	min_x2 = min(x2, N - x2 + 1)
	min_y1 = min(y1, N - y1 + 1)
	min_y2 = min(y2, N - y2 + 1)
	ans = min(min(min_x1, min_y1) + min(min_x2, min_y2), abs(x1 - x2) + abs(y1 - y2))
	return ans
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return str(input())

def invr():
	return map(int, input().split())
T = inp()
for i in range(T):
	arr = inlt()
	print(sol(arr))


T = int(input())
for i in range(T):
	(N, x1, y1, x2, y2) = map(int, input().split())
	A = abs(x1 - x2) + abs(y1 - y2)
	B = min(N - max(x1, y1) + 1, min(x1, y1)) + min(min(x2, y2), N - max(x2, y2) + 1)
	print(min(A, B))


for i in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	a = abs(x1 - x2) + abs(y1 - y2)
	b = min(n - max(x1, y1) + 1, min(x1, y1)) + min(min(x2, y2), n - max(x2, y2) + 1)
	print(min(a, b))


n = int(input())
for i in range(n):
	arr = [int(x) for x in input().split()]
	N = arr[0]
	(x1, y1) = (arr[1], arr[2])
	(x2, y2) = (arr[3], arr[4])
	arr1 = [x1, y1, N - x1 + 1, N - y1 + 1]
	arr1.sort()
	a = arr1[0]
	arr2 = [x2, y2, N - x2 + 1, N - y2 + 1]
	arr2.sort()
	b = arr2[0]
	final = abs(x2 - x1) + abs(y2 - y1)
	print(min(a + b, final))


t = int(input())
dist_nearest_edge = lambda x, y: min(x - 1, n - x, y - 1, n - y)
for _ in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split(' '))
	c1 = abs(x1 - x2) + abs(y1 - y2)
	c2 = dist_nearest_edge(x1, y1) + dist_nearest_edge(x2, y2) + 2
	print(min(c1, c2))


for _ in range(int(input())):
	data = input().split(' ')
	(n, x1, y1, x2, y2) = (int(data[0]), int(data[1]), int(data[2]), int(data[3]), int(data[4]))
	calc = [abs(x1 - x2) + abs(y1 - y2), min(x1 - 1, abs(n - y1), abs(n - x1), y1 - 1) + min(x2 - 1, abs(n - x2), abs(n - y2), y2 - 1) + 2]
	print(min(calc))


t = int(input())
for _ in range(t):
	(n, x1, y1, x2, y2) = list(map(int, input().split()))
	inside = abs(x2 - x1) + abs(y2 - y1)
	costOfGoingOut = min(x1 - 0, n - x1 + 1, y1 - 0, n - y1 + 1)
	costOfComingIn = min(x2 - 0, n - x2 + 1, y2 - 0, n - y2 + 1)
	outside = costOfComingIn + costOfGoingOut
	print(min(inside, outside))


for i in range(int(input())):
	l = list(map(int, input().split()))
	r1 = abs(l[3] - l[1]) + abs(l[4] - l[2])
	r2 = min(l[1], l[2], l[0] + 1 - max(l[1], l[2])) + min(l[0] + 1 - max(l[3], l[4]), l[3], l[4])
	print(min(r1, r2))


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	offgridcost = min(min(n + 1 - y1, y1), min(n + 1 - x1, x1)) + min(min(n + 1 - x2, x2), min(n + 1 - y2, y2))
	ingridcost = abs(x1 - x2) + abs(y1 - y2)
	print(min(offgridcost, ingridcost))


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = map(int, input().split())
	side = min(min(x1, n - x1 + 1), min(y1, n - y1 + 1)) + min(min(x2, n - x2 + 1), min(y2, n - y2 + 1))
	var = abs(x2 - x1) + abs(y2 - y1)
	print(min(side, var))


tc = int(input())
for i in range(tc):
	(N, x1, y1, x2, y2) = map(int, input().split())
	internal = abs(x2 - x1) + abs(y2 - y1)
	external = min(x1 - 1, N - x1, y1 - 1, N - y1) + min(x2 - 1, N - x2, y2 - 1, N - y2) + 2
	print(min(internal, external))


t = int(input())
for i in range(0, t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	internal_sum = abs(x2 - x1) + abs(y2 - y1)
	external_sum = min(n - x1, n - y1, x1 - 1, y1 - 1) + min(n - x2, n - y2, x2 - 1, y2 - 1) + 2
	print(min(internal_sum, external_sum))


t = int(input())
for i in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	if (x1 < 1 or y1 < 1 or x1 > n or (y1 > n)) and (x2 < 1 or y2 < 1 or x2 > n or (y2 > n)):
		print(0)
	else:
		sv = min(x1 - 1, n - x1)
		sh = min(y1 - 1, n - y1)
		s = min(sv, sh) + 1
		dv = min(x2 - 1, n - x2)
		dh = min(y2 - 1, n - y2)
		d = min(dv, dh) + 1
		l = abs(x1 - x2) + abs(y1 - y2)
		k = s + d
		print(min(l, k))


from math import *
t = int(input())
for i in range(t):
	(n, x1, y1, x2, y2) = map(int, input().split())
	if max(x1, y1) > n:
		if max(x2, y2) > n:
			print(0)
		else:
			print(min(x2, y2, n + 1 - x2, n + 1 - y2))
	elif max(x2, y2) > n:
		if max(x1, y1) > n:
			print(0)
		else:
			print(min(x1, y1, n + 1 - x1, n + 1 - y1))
	else:
		print(min(abs(x2 - x1) + abs(y2 - y1), min(x1, y1, n + 1 - x1, n + 1 - y1) + min(x2, y2, n + 1 - x2, n + 1 - y2)))


for i in range(int(input())):
	(n, x1, y1, x2, y2) = list(map(int, input().split()))

	def minout(x, y):
		m = min(min(x, n + 1 - x), min(y, n + 1 - y))
		return m
	mincostout = minout(x1, y1) + minout(x2, y2)
	mincostin = abs(x2 - x1) + abs(y2 - y1)
	print(min(mincostin, mincostout))


for i in range(int(input())):
	(n, x1, y1, x2, y2) = list(map(int, input().split()))

	def minoutsteps(x, y):
		if x < y:
			min = x
			if n + 1 - y < x:
				min = n + 1 - y
		else:
			min = y
			if n + 1 - x < y:
				min = n + 1 - x
		return min
	mincostout = minoutsteps(x1, y1) + minoutsteps(x2, y2)
	mincostin = abs(x2 - x1) + abs(y2 - y1)
	if mincostout < mincostin:
		print(mincostout)
	else:
		print(mincostin)


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = [int(i) for i in input().split()]
	inside_dist = abs(x2 - x1) + abs(y2 - y1)
	min_start = min(x1, y1, n - x1 + 1, n - y1 + 1)
	min_end = min(x2, y2, n - x2 + 1, n - y2 + 1)
	outside_dist = min_start + min_end
	print(min(inside_dist, outside_dist))


for i in range(int(input())):
	(n, x1, y1, x2, y2) = input().split()
	n = int(n)
	x1 = int(x1)
	y1 = int(y1)
	x2 = int(x2)
	y2 = int(y2)
	l1 = [x1, y1, n + 1 - x1, n + 1 - y1]
	l2 = [x2, y2, n + 1 - x2, n + 1 - y2]
	l1.sort()
	l2.sort()
	a = l1[0] + l2[0]
	b = abs(x2 - x1) + abs(y2 - y1)
	if a > b:
		print(b)
	else:
		print(a)


for _ in range(int(input())):
	(n, x1, y1, x2, y2) = [int(i) for i in input().split()]
	direct = abs(x1 - x2) + abs(y1 - y2)
	exit = min(min(x1, y1), min(n - x1, n - y1) + 1)
	entry = min(min(x2, y2), min(n - x2, n - y2) + 1)
	outside = exit + entry
	print(min(direct, outside))