A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of $n$ rows and $m$ columns. The rows of the floor are numbered from $1$ to $n$ from top to bottom, and columns of the floor are numbered from $1$ to $m$ from left to right. The cell on the intersection of the $r$-th row and the $c$-th column is denoted as $(r,c)$. The initial position of the robot is $(r_b, c_b)$.

In one second, the robot moves by $dr$ rows and $dc$ columns, that is, after one second, the robot moves from the cell $(r, c)$ to $(r + dr, c + dc)$. Initially $dr = 1$, $dc = 1$. If there is a vertical wall (the left or the right walls) in the movement direction, $dc$ is reflected before the movement, so the new value of $dc$ is $-dc$. And if there is a horizontal wall (the upper or lower walls), $dr$ is reflected before the movement, so the new value of $dr$ is $-dr$.

Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at $(r_d, c_d)$. The job of the robot is to clean that dirty cell.

Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes.

Given the floor size $n$ and $m$, the robot's initial position $(r_b, c_b)$ and the dirty cell's position $(r_d, c_d)$, find the time for the robot to do its job.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows.

A test case consists of only one line, containing six integers $n$, $m$, $r_b$, $c_b$, $r_d$, and $c_d$ ($1 \le n, m \le 100$, $1 \le r_b, r_d \le n$, $1 \le c_b, c_d \le m$) — the sizes of the room, the initial position of the robot and the position of the dirt cell.


-----Output-----

For each test case, print an integer — the time for the robot to clean the dirty cell. We can show that the robot always cleans the dirty cell eventually.


-----Examples-----

Input
5
10 10 6 1 2 8
10 10 9 9 1 1
9 8 5 6 2 1
6 9 2 2 5 8
2 2 1 1 2 1
Output
7
10
9
3
0


-----Note-----

In the first example, the floor has the size of $10\times 10$. The initial position of the robot is $(6, 1)$ and the position of the dirty cell is $(2, 8)$. See the illustration of this example in the problem statement.

In the second example, the floor is the same, but the initial position of the robot is now $(9, 9)$, and the position of the dirty cell is $(1, 1)$. In this example, the robot went straight to the dirty cell and clean it.

In the third example, the floor has the size $9 \times 8$. The initial position of the robot is $(5, 6)$, and the position of the dirty cell is $(2, 1)$.

In the fourth example, the floor has the size $6 \times 9$. The initial position of the robot is $(2, 2)$ and the position of the dirty cell is $(5, 8)$.

In the last example, the robot was already standing in the same column as the dirty cell, so it can clean the cell right away.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation

Complete search


Solutions:
import sys, math, cmath, time, collections

def solve():
	(n, m, rb, cb, rd, cd) = invr()
	dr = 1
	cr = 1
	cnt = 0
	while True:
		if rb == rd:
			break
		if cb == cd:
			break
		if rb == n:
			dr = -1
		elif rb == 1:
			dr = 1
		if cb == m:
			cr = -1
		elif cb == 1:
			cr = 1
		rb = rb + dr
		cb = cb + cr
		cnt = cnt + 1
	print(cnt)

def main():
	global tt
	t = inp()
	for tt in range(1, t + 1):
		solve()

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

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

def inls():
	return list(map(str, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def is_coprime(x, y):
	return gcd(x, y) == 1

def gcd(p, q):
	while q != 0:
		(p, q) = (q, p % q)
	return p

def convert(set):
	return list(set)

def unique(list):
	list_set = set(list)
	unique_list = convert(list_set)
	return unique_list

def sort_string(string):
	r = sorted(string)
	return ''.join(r)

def reverse_string(string):
	return string[::-1]

def convert_to_list(string):
	lis = []
	lis[:0] = string
	return lis

def seive(n):
	a = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	return a

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def power_two(x):
	return x and (not x & x - 1)

def ceil(a, b):
	return -(-a // b)
main()


t = int(input())
for i in range(0, t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rd >= rb:
		rt = rd - rb
	else:
		rt = abs(n - rb) + abs(n - rd)
	if cd >= cb:
		ct = cd - cb
	else:
		ct = abs(m - cb) + abs(m - cd)
	print(ct if ct < rt else rt)


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	d1 = rd - rb if rd >= rb else 2 * n - rd - rb
	d2 = cd - cb if cd >= cb else 2 * m - cd - cb
	print(min(d1, d2))


n = int(input())
for i in range(n):
	(x, y, x1, y1, x2, y2) = list(map(int, input().split()))
	d = 1
	v = 1
	c = 0
	c1 = 0
	while x1 != x2:
		c += 1
		if d == 1 and x1 != x:
			x1 += 1
		elif d == 1 and x1 == x:
			d = -1
			x1 -= 1
		elif d == -1 and x1 == 0:
			d = 1
			x1 += 1
		elif d == -1 and x1 != 0:
			x1 -= 1
	while y1 != y2:
		c1 += 1
		if v == 1 and y1 != y:
			y1 += 1
		elif v == 1 and y1 == y:
			v = -1
			y1 -= 1
		elif v == -1 and y1 == 0:
			v = 1
			y1 += 1
		elif v == -1 and y1 != 0:
			y1 -= 1
	if c > c1:
		print(c1)
	else:
		print(c)


def do(n, m, rob_y, rob_x, dirt_y, dirt_x):
	(rx, ry) = (rob_x, rob_y)
	(dx, dy) = (dirt_x, dirt_y)
	c = 0
	(ddx, ddy) = (1, 1)
	while True:
		if rx == dx or ry == dy:
			break
		if rx == m or rx == 0:
			ddx = -ddx
		if ry == n or ry == 0:
			ddy = -ddy
		rx += ddx
		ry += ddy
		c += 1
	print(c)
t = int(input())
for i in range(t):
	(n, m, rob_y, rob_x, dirt_y, dirt_x) = list(map(int, input().split()))
	do(n, m, rob_y, rob_x, dirt_y, dirt_x)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	t = 0
	r = 1
	c = 1
	while not (rb == rd or cb == cd):
		t += 1
		if rb == n or rb == 0:
			r = -r
		if cb == m or cb == 0:
			c = -c
		rb += r
		cb += c
	print(t)


a = int(input())
some_list = []
for i in range(a):
	(n, m, x, y, r1, r2) = map(int, input().split())
	dr = 1
	dc = 1
	if x < r1:
		seconds_1 = r1 - x
	elif x > r1:
		seconds_1 = n - x + (n - r1)
	elif x == r1:
		seconds_1 = 0
	if y > r2:
		seconds_2 = m - y + (m - r2)
	elif y < r2:
		seconds_2 = r2 - y
	elif y == r2:
		seconds_2 = 0
	z = min(seconds_1, seconds_2)
	some_list.append(z)
	some_list.append('\n')
print(*some_list, sep='')


import heapq
from collections import Counter
import math
import sys
input = sys.stdin.readline

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

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

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def inis():
	return input().split()
for _ in range(int(input())):
	(a, b, c, d, e, f) = invr()
	x = y = 1
	t = 0
	while c != e and d != f:
		if c != a:
			c += x
		else:
			x = -x
			c += x
		if d != b:
			d += y
		else:
			y = -y
			d += y
		t += 1
	print(t)


for i in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(ans1, ans2) = (rd - rb, cd - cb)
	if rb > rd:
		ans1 = 2 * n - rb - rd
	if cb > cd:
		ans2 = 2 * m - cb - cd
	print(min(ans1, ans2))


t = int(input())
for i in range(t):
	(n, m, x, y, a, b) = map(int, input().split())
	x1 = x
	y1 = y
	dx = 1
	dy = 1
	c = 0
	while True:
		if x1 == a or y1 == b:
			print(c)
			break
		if x1 == n:
			dx = -dx
		if y1 == m:
			dy = -dy
		x1 += dx
		y1 += dy
		c += 1


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, functools, copy, random
from functools import reduce
import operator
inf = 10 ** 9 + 7
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def LS():
	return input().split()

def endl():
	return print('\n')

def lcm(a, b):
	return a * b // math.gcd(a, b)

def grayCode(n):
	return n ^ n >> 1
sys.setrecursionlimit(20000)
graph = collections.defaultdict(list)
grid = []
visited = set()
ways = ['D', 'U', 'R', 'L']
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]

def find(i):
	if par[i] != i:
		par[i] = find(par[i])
	return par[i]

def union(a, b):
	a = find(a)
	b = find(b)
	if a == b:
		return False
	par[a] = b
	return True

def initGraph(m):
	for _ in range(m):
		(u, v) = LI()
		graph[u - 1].append(v - 1)
		graph[v - 1].append(u - 1)
	return graph

def initGraphW(m):
	for _ in range(m):
		(a, b, c) = LI()
		graph[a - 1].append((b - 1, c))
		graph[b - 1].append((a - 1, c))

def initGrid(h, w):
	for i in range(h):
		s = S()
		grid.append(list(s))
	return grid

def soe(limit):
	a = [True] * limit
	a[0] = a[1] = False
	for (i, isprime) in enumerate(a):
		if isprime:
			yield i
			for n in range(i * i, limit, i):
				a[n] = False
	return a
for _ in range(I()):
	(n, m, sx, sy, ex, ey) = LI()
	cnt = 0
	dr = 1
	dc = 1
	while not (sx == ex or sy == ey):
		if sx + dc == n + 1 or sx + dc == 0:
			dc = -dc
			continue
		if sy + dr == m + 1 or sy + dr == 0:
			dr = -dr
			continue
		sx += dc
		sy += dr
		cnt += 1
	print(cnt)


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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

def inps():
	return input().strip()

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	(n, m, rb, cb, rd, cd) = invr()
	(strt, end) = (rb, cb)
	(a, b) = (1, 1)
	steps = 0
	while strt != rd and end != cd:
		if strt + a > n:
			a = -a
		if end + b > m:
			b = -b
		if strt + a < 1:
			a = -a
		if end + b < 1:
			b = -b
		strt += a
		end += b
		steps += 1
	print(steps)


def myfunction():
	t = int(input())
	for i in range(t):
		(r, c, x_r, y_r, x_d, y_d) = [int(x) for x in input().split()]
		count = 0
		fr = 1
		fc = 1
		while True:
			if x_r == x_d or y_r == y_d:
				break
			if fr + x_r > r or fr + x_r < 1:
				fr *= -1
			if fc + y_r > c or fc + y_r < 1:
				fc *= -1
			x_r += fr
			y_r += fc
			count += 1
		print(count)
myfunction()


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rd >= rb:
		h = rd - rb
	else:
		h = 2 * n - (rb + rd)
	if cd >= cb:
		k = cd - cb
	else:
		k = 2 * m - (cb + cd)
	print(min(h, k))


testNum = int(input())
i = 0
timeArray = []
while i < testNum:
	inpArr = input().split()
	inp = list(map(int, inpArr))
	g_1 = inp[2] <= inp[4]
	g_2 = inp[3] <= inp[5]
	if g_1 and g_2:
		outTime = min(inp[4] - inp[2], inp[5] - inp[3])
	elif g_1 and (not g_2):
		outTime = min(inp[4] - inp[2], 2 * inp[1] - inp[5] - inp[3])
	elif g_2 and (not g_1):
		outTime = min(inp[5] - inp[3], 2 * inp[0] - inp[2] - inp[4])
	else:
		outTime = min(2 * inp[0] - inp[2] - inp[4], 2 * inp[1] - inp[3] - inp[5])
	timeArray.append(outTime)
	i += 1
for x in timeArray:
	print(x)


w = int(input())
l = []
for i in range(w):
	t = list(map(int, input().split()))
	l.append(t)
for i in l:
	n = i[0]
	m = i[1]
	rb = i[2]
	cb = i[3]
	rd = i[4]
	cd = i[5]
	if rb == rd or cb == cd:
		print(0)
	elif cb > cd:
		if rb > rd:
			print(min(2 * n - rb - rd, 2 * m - cb - cd))
		elif rb < rd:
			print(min(rd - rb, 2 * m - cd - cb))
	elif cb < cd:
		if rb > rd:
			print(min(cd - cb, 2 * n - rb - rd))
		elif rb < rd:
			print(min(cd - cb, rd - rb))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, m, r, c, rd, cd) = [int(i) for i in input().split()]
	d1 = 0
	d2 = 0
	if rd < r:
		d1 = 2 * (n - r) + r - rd
	else:
		d1 = rd - r
	if cd < c:
		d2 = 2 * (m - c) + c - cd
	else:
		d2 = cd - c
	print(min(d1, d2))


def time(n, m, r_b, c_b, r_d, c_d):
	if r_b > r_d:
		a = 2 * n - r_b - r_d
	else:
		a = r_d - r_b
	if c_b > c_d:
		b = 2 * m - c_b - c_d
	else:
		b = c_d - c_b
	return min(a, b)
testCase = int(input())
outpiutlist = []
for i in range(testCase):
	(n, m, r_b, c_b, r_d, c_d) = input().split()
	outpiutlist.append(time(int(n), int(m), int(r_b), int(c_b), int(r_d), int(c_d)))
for i in outpiutlist:
	print(i)


for t in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	ans = 0
	if rb == rd or cb == cd:
		ans = 0
	elif rd > rb and cd < cb:
		row = rd - rb
		col = m - cb + (m - cd)
		ans = min(row, col)
	elif rd < rb and cd > cb:
		row = n - rb + (n - rd)
		col = cd - cb
		ans = min(row, col)
	elif rd > rb and cd > cb:
		ans = min(rd - rb, cd - cb)
	elif rd < rb and cd < cb:
		row = n - rb + (n - rd)
		col = m - cb + (m - cd)
		ans = min(row, col)
	print(ans)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb > rd:
		ud = 2 * n - rd - rb
	else:
		ud = rd - rb
	if cb > cd:
		rl = 2 * m - cd - cb
	else:
		rl = cd - cb
	print(min(rl, ud))


t = int(input())
for _ in range(t):
	(n, m, rx, ry, dx, dy) = map(int, input().split())
	if rx == dx or ry == dy:
		print(0)
	elif rx < dx and ry < dy:
		print(min(dx - rx, dy - ry))
	elif rx < dx:
		print(min(dx - rx, m - dy + (m - ry)))
	elif ry < dy:
		print(min(n - dx + (n - rx), dy - ry))
	else:
		print(min(n - dx + (n - rx), m - dy + (m - ry)))


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = tuple(map(int, input().rstrip().split()))
	if rd >= rb:
		r_time = rd - rb
	else:
		r_time = n - rd + (n - rb)
	if cd >= cb:
		c_time = cd - cb
	else:
		c_time = m - cd + (m - cb)
	print(min(r_time, c_time))


t = int(input())
for _ in range(t):
	(n, m, a, b, c, d) = map(int, input().split())
	cost1 = 0
	cost2 = 0
	if a == c or b == d:
		print(0)
		continue
	if c > a:
		cost1 = c - a
	else:
		cost1 = 2 * (n - a) + a - c
	if d > b:
		cost2 = d - b
	else:
		cost2 = 2 * (m - b) + b - d
	print(min(cost1, cost2))


t = int(input())
for k in range(t):
	(n, m, a, b, c, d) = map(int, input().split())
	res = 100000000000
	if a > c:
		res = min(res, n - a + n - c)
	else:
		res = min(res, n - a - n + c)
	if b > d:
		res = min(res, m - b + m - d)
	else:
		res = min(res, m - b - m + d)
	print(res)


def solve():
	inp = map(int, input().split())
	(n, m, rB, cB, rd, cd) = inp
	if rd >= rB:
		sy = rd - rB
	else:
		sy = 2 * n - rd - rB
	if cd >= cB:
		sx = cd - cB
	else:
		sx = 2 * m - cd - cB
	print(min(sy, sx))

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_list_strings():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve():
	(n, m, rb, cb, rd, cd) = get_ints()
	ans = 0
	flag1 = 0
	flag2 = 0
	while True:
		if rb == rd or cb == cd:
			print(ans)
			return
		if flag1 == 0:
			if rb + 1 <= n:
				rb += 1
			else:
				rb -= 1
				flag1 = 1
		else:
			rb -= 1
		if flag2 == 0:
			if cb + 1 <= m:
				cb += 1
			else:
				cb -= 1
				flag2 = 1
		else:
			cb -= 1
		ans += 1
T = get_int()
while T:
	solve()
	T -= 1


T = int(input())
for _ in range(T):
	(H, W, a, b, c, d) = map(int, input().split())
	ans = float('inf')
	if a <= c:
		ans = min(ans, c - a)
	if b <= d:
		ans = min(ans, d - b)
	this = 0
	ans = min(ans, H - a + H - c)
	ans = min(ans, W - b + W - d)
	print(ans)


def find(H, W, r1, b1, r2, b2, dr=1, db=1, num=0):
	if r1 == r2 or b1 == b2:
		print(num)
	else:
		if r1 + dr > H or r1 + dr < 0:
			dr *= -1
		if b1 + db > W or b1 + db < 0:
			db *= -1
		find(H, W, r1 + dr, b1 + db, r2, b2, dr, db, num + 1)
for _ in range(int(input())):
	(H, W, r1, b1, r2, b2) = map(int, input().split())
	find(H, W, r1, b1, r2, b2)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rd >= rb and cd >= cb:
		print(min(rd - rb, cd - cb))
	elif rd >= rb:
		print(min(rd - rb, (m - cb) * 2 + cb - cd))
	elif cd >= cb:
		print(min(cd - cb, (n - rb) * 2 + rb - rd))
	else:
		print(min((n - rb) * 2 + rb - rd, (m - cb) * 2 + cb - cd))


for i in range(int(input())):
	line = input()
	(rows, cols, x1, y1, x2, y2) = map(int, line.split(' '))
	if x2 >= x1:
		x_value = x2 - x1
	else:
		x_value = 2 * rows - (x1 + x2)
	if y2 >= y1:
		y_value = y2 - y1
	else:
		y_value = 2 * cols - (y1 + y2)
	ans = min(x_value, y_value)
	print(ans)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb <= rd and cb <= cd:
		print(min(rd - rb, cd - cb))
	elif rb <= rd:
		print(min(rd - rb, 2 * m - (cb + cd)))
	elif cb <= cd:
		print(min(cd - cb, 2 * n - (rb + rd)))
	else:
		print(min(2 * n - (rb + rd), 2 * m - (cb + cd)))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from math import inf
from collections import defaultdict as dd

def main(t):
	(n, m, r1, c1, r2, c2) = iia()
	count = 0
	(dr, dc) = (1, 1)
	while count < n * m:
		if r1 == r2 or c1 == c2:
			print(count)
			return
		if count == 0:
			if r1 == n:
				dr = -dr
			if c1 == m:
				dc = -dc
		else:
			if c1 == 1 or c1 == m:
				dc = -dc
			if r1 == 1 or r1 == n:
				dr = -dr
		r1 += dr
		c1 += dc
		count += 1
t = 1
t = ii1()
for _ in range(t):
	main(_)


for _ in range(int(input())):
	(n, m, rx, ry, dx, dy) = list(map(int, input().split()))
	cnt = 0
	p1 = 1
	p2 = 1
	while rx != dx and ry != dy:
		if rx == n:
			p1 = -p1
		if ry == m:
			p2 = -p2
		rx += p1
		ry += p2
		cnt += 1
	print(cnt)


sr = lambda : input()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
inf = 10 ** 18
mod = 998244353
test = ir()
for t in range(test):
	(n, m, bh, bw, dh, dw) = lr()
	if bh <= dh:
		dy = dh - bh
	else:
		dy = n - bh + (n - dh)
	if bw <= dw:
		dx = dw - bw
	else:
		dx = m - bw + (m - dw)
	print(min(dy, dx))


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	count = 0
	robot = [rb, cb]
	target = [rd, cd]
	i = 1
	j = 1
	r_s = 0
	c_s = 0
	if rb == rd or cb == cd:
		print(0)
	else:
		if rb > rd:
			r_s = n - rb + (n - rd)
		if cb > cd:
			c_s = m - cb + (m - cd)
		if rb < rd:
			r_s = rd - rb
		if cd > cb:
			c_s = cd - cb
		print(min(r_s, c_s))


result = []
for _ in range(int(input())):
	(n, m, y, x, yc, xc) = list(map(int, input().split()))
	result.append(min(xc - x if xc - x >= 0 else 2 * m - x - xc, yc - y if yc - y >= 0 else 2 * n - y - yc))
print('\n'.join(map(str, result)))


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import Decimal
from decimal import *
from collections import defaultdict, deque
import heapq
import bisect
from decimal import Decimal
getcontext().prec = 25
abcd = 'abcdefghijklmnopqrstuvwxyz'
ABCD = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
months = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
MOD = 1000000007
BUFSIZE = 8192
from bisect import bisect_left, bisect_right

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(a, b, c, d, e, f) = map(int, input().split(' '))
	if c <= e:
		v = e - c
	else:
		v = 2 * a - c - e
	if d <= f:
		v2 = f - d
	else:
		v2 = 2 * b - d - f
	print(min(v, v2))


test_case = input('')
test_case = int(test_case)
z = 0
while z < test_case:
	l = [int(item) for item in input('').split()]
	i = l[2]
	k = l[3]
	count = 0
	flag = 0
	if l[2] == l[4] or l[3] == l[5]:
		print(count)
		flag = 1
	if flag == 1:
		z = z + 1
		continue
	else:
		while i < l[0] and k < l[1]:
			l[2] = i + 1
			l[3] = k + 1
			count = count + 1
			if l[2] == l[4] or l[3] == l[5]:
				print(count)
				flag = 1
				break
			i = i + 1
			k = k + 1
		if flag == 1:
			z = z + 1
			continue
		if l[2] == l[0] and l[3] == l[1]:
			i = l[2]
			k = l[3]
			while i > 1 and k > 1:
				l[2] = i - 1
				l[3] = k - 1
				count = count + 1
				if l[2] == l[4] or l[3] == l[5]:
					print(count)
					flag = 1
					break
				i = i - 1
				k = k - 1
		if flag == 1:
			z = z + 1
			continue
		elif l[2] == l[0]:
			i = l[2]
			k = l[3]
			while i > 1 and k < l[1]:
				l[2] = i - 1
				l[3] = k + 1
				count = count + 1
				if l[2] == l[4] or l[3] == l[5]:
					print(count)
					flag = 1
					break
				i = i - 1
				k = k + 1
			if flag == 1:
				z = z + 1
				continue
			if l[3] == l[1]:
				i = l[2]
				k = l[3]
				while i > 1 and k > 1:
					l[2] = i - 1
					l[3] = k - 1
					count = count + 1
					if l[2] == l[4] or l[3] == l[5]:
						print(count)
						flag = 1
						break
					i = i - 1
					k = k - 1
				if flag == 1:
					z = z + 1
					continue
		else:
			i = l[2]
			k = l[3]
			while i < l[0] and k > 1:
				l[2] = i + 1
				l[3] = k - 1
				count = count + 1
				if l[2] == l[4] or l[3] == l[5]:
					print(count)
					flag = 1
					break
				i = i + 1
				k = k - 1
			if flag == 1:
				z = z + 1
				continue
			if l[2] == l[0]:
				i = l[2]
				k = l[3]
				while i > 1 and k > 1:
					l[2] = i - 1
					l[3] = k - 1
					count = count + 1
					if l[2] == l[4] or l[3] == l[5]:
						print(count)
						break
					i = i - 1
					k = k - 1
	z = z + 1


t = int(input())
for tt in range(t):
	(n, m, a, b, x, y) = map(int, input().split())
	ans = 0
	x1 = 1
	y1 = 1
	while a != x and y != b:
		ans += 1
		if a == n:
			x1 *= -1
		if b == m:
			y1 *= -1
		a += x1
		b += y1
	print(ans)


for i in range(0, int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if cb <= cd:
		if rb <= rd:
			print(min(rd - rb, cd - cb))
		else:
			print(min(2 * n - rb - rd, cd - cb))
	elif rb <= rd:
		print(min(2 * m - cd - cb, rd - rb))
	else:
		print(min(2 * m - cd - cb, 2 * n - rb - rd))


t = int(input())
while t != 0:
	(rows, cols, ini_row, ini_col, final_row, final_col) = map(int, input().split())
	if ini_row <= final_row:
		row_steps = final_row - ini_row
	else:
		row_steps = rows - ini_row + (rows - final_row)
	if ini_col <= final_col:
		col_steps = final_col - ini_col
	else:
		col_steps = cols - ini_col + (cols - final_col)
	ans = min(row_steps, col_steps)
	print(ans)
	t -= 1


for t in range(int(input())):
	(r, x, a, b, c, d) = map(int, input().split())
	print(min(c - a if c >= a else 2 * (r - a) + a - c, d - b if d >= b else 2 * (x - b) + b - d))


def find(n, m, rb, cb, rd, cd):
	samerow = abs(rb - rd)
	if rd < rb:
		samerow = 2 * n - rb - rd
	samecol = abs(cb - cd)
	if cd < cb:
		samecol = 2 * m - cb - cd
	return min(samerow, samecol)
cases = int(input())
for _ in range(cases):
	line = input().split()
	(n, m, rb, cb, rd, cd) = (int(line[0]), int(line[1]), int(line[2]), int(line[3]), int(line[4]), int(line[5]))
	print(find(n, m, rb, cb, rd, cd))


import math
import sys
import itertools
import fractions
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
for t in wtf[1:]:
	(n, m, rb, cb, rd, cd) = map(int, t.split())
	tmp = []
	tmp.append(rd - rb if rb <= rd else 2 * n - rb - rd)
	tmp.append(cd - cb if cb <= cd else 2 * m - cd - cb)
	print(min(tmp))


ans = []
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	ans.append(min(n - rd + n - rb if rd < rb else rd - rb, m - cd + m - cb if cd < cb else cd - cb))
print(*ans, sep='\n')


t = int(input())
res = []
for i in range(t):
	l = input().split()
	l = [int(j) for j in l]
	n = l[0]
	m = l[1]
	rb = l[2]
	cb = l[3]
	rd = l[4]
	cd = l[5]
	if rb == rd or cb == cd:
		res.append(0)
	elif rb < rd and cb < cd:
		res.append(min(rd - rb, cd - cb))
	elif rb < rd:
		res.append(min(rd - rb, m - cd + m - cb))
	elif cb < cd:
		res.append(min(n - rd + n - rb, cd - cb))
	else:
		res.append(min(n - rd + n - rb, m - cd + m - cb))
for i in res:
	print(i)


t = int(input())
for i in range(t):
	(n, m, a, b, a1, b1) = list(map(int, input().split()))
	if a <= a1:
		a2 = a1 - a
	else:
		a2 = n - a + n - a1
	if b <= b1:
		b2 = b1 - b
	else:
		b2 = m - b + m - b1
	print(min(a2, b2))


import sys
input = sys.stdin.readline
num_test_cases = int(input())
for test_case_ittr in range(num_test_cases):
	(num_rows, num_cols, row, col, target_row, target_col) = [int(x) for x in input().split()]
	vert_ans = 0
	if target_row > row:
		vert_ans = target_row - row
	elif target_row < row:
		vert_ans = 2 * (num_rows - row) + row - target_row
	hor_ans = 0
	if target_col > col:
		hor_ans = target_col - col
	elif target_col < col:
		hor_ans = 2 * (num_cols - col) + col - target_col
	ans = min(vert_ans, hor_ans)
	print(ans)


def solve():
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb <= rd:
		Ox = rd - rb
	else:
		Ox = 2 * n - rb - rd
	if cb <= cd:
		Oy = cd - cb
	else:
		Oy = 2 * m - cb - cd
	print(min(Ox, Oy))
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	(dr, dc) = (1, 1)
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	time = 100000000000000000000000000
	if rb > rd:
		time = min(time, n - rb + n - rd)
	elif rb == rd:
		time = 0
	else:
		time = min(time, rd - rb)
	if cb > cd:
		time = min(time, m - cb + m - cd)
	elif cb == cd:
		time = 0
	else:
		time = min(time, cd - cb)
	print(time)


a = int(input())
for i in range(a):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	rk = 1
	ck = 1
	t = 0
	while True:
		if rd == rb or cb == cd:
			break
		if rb == n or rd == 0:
			rk *= -1
		if cb == m or cd == 0:
			ck *= -1
		rb += rk
		cb += ck
		t += 1
	print(t)


h = int(input())
for zzz in range(h):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(dr, dc, t) = (1, 1, 0)
	while rd != rb and cd != cb:
		if rb == n:
			dr = -dr
		if cb == m:
			dc = -dc
		t += 1
		rb += dr
		cb += dc
	print(t)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	ans = 999999999999
	if rb <= rd:
		ans = min(ans, rd - rb)
	else:
		ans = min(ans, 2 * n - rb - rd)
	if cb <= cd:
		ans = min(ans, cd - cb)
	else:
		ans = min(ans, 2 * m - cb - cd)
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, r, c, x, y) = map(int, input().split())
	temp = [x - r if r <= x else 2 * n - x - r, y - c if c <= y else 2 * m - y - c]
	print(min(temp))


n = input()
n = int(n)
storage = list()
for i in range(n):
	e = [int(s) for s in input().split(' ')]
	storage.append(e)
for i in range(n):
	r = storage[i][0]
	c = storage[i][1]
	if storage[i][4] == storage[i][2] or storage[i][5] == storage[i][3]:
		res = 0
	if storage[i][4] < storage[i][2] and storage[i][5] < storage[i][3]:
		rt = r - storage[i][2] + (r - storage[i][4])
		ct = c - storage[i][3] + (c - storage[i][5])
		res = min(rt, ct)
	if storage[i][4] > storage[i][2] and storage[i][5] > storage[i][3]:
		rt = storage[i][4] - storage[i][2]
		ct = storage[i][5] - storage[i][3]
		res = min(rt, ct)
	if storage[i][4] > storage[i][2] and storage[i][5] < storage[i][3]:
		rt = storage[i][4] - storage[i][2]
		ct = c - storage[i][3] + (c - storage[i][5])
		res = min(rt, ct)
	if storage[i][4] < storage[i][2] and storage[i][5] > storage[i][3]:
		rt = r - storage[i][2] + (r - storage[i][4])
		ct = storage[i][5] - storage[i][3]
		res = min(rt, ct)
	print(res)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb == rd or cb == cd:
		print(0)
	else:
		dr = 1
		dc = 1
		x = 0
		while rb != rd and cb != cd:
			if rb == n:
				dr *= -1
			if cb == m:
				dc *= -1
			rb += dr
			cb += dc
			x += 1
		print(x)


from sys import stdin
import math
from collections import deque
from heapq import heapify, heappop, heappush
from bisect import bisect_left, bisect_right
input = stdin.readline

def ceil(m, n):
	val = m // n
	if m % n != 0:
		return val + 1
	return val
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	chances = n * m
	diffc = cd - cb
	diffr = rd - rb
	if diffc >= 0:
		chances = min(chances, diffc)
	else:
		chances = min(chances, m - cd + m - cb)
	if diffr >= 0:
		chances = min(chances, diffr)
	else:
		chances = min(chances, n - rd + n - rb)
	print(chances)


import sys
from collections import deque

def g(l, n, f):
	a = 0
	x = n - 1
	while a <= x:
		m = a + (x - a) // 2
		if l[m] == f:
			return m
		elif l[m] > f:
			x = m - 1
		else:
			a = m + 1
	return None

def fun(p, q, m):
	if p <= q:
		return q - p
	else:
		return m - p + m - q
for _ in range(int(sys.stdin.readline())):
	(n, m, a, b, x, y) = map(int, sys.stdin.readline().split())
	print(min(fun(a, x, n), fun(b, y, m)))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, m, x, y, x1, y1) = list(map(int, input().split()))
	ans = 0
	(c1, c2) = (1, 1)
	while x != x1 and y != y1:
		x = x + c1
		y = y + c2
		ans = ans + 1
		if x > n or x < 1 or y > m or (y < 1):
			ans = ans - 1
			if x > n or x < 1:
				x = x - c1
				c1 = -1 * c1
				y = y - c2
			else:
				y = y - c2
				c2 = -1 * c2
				x = x - c1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	r = rb
	c = cb
	dr = 1
	dc = 1
	time = 0
	while True:
		if r == rd or c == cd:
			break
		rr = r + dr
		cc = c + dc
		if rr < 1 or rr > n:
			dr *= -1
		if cc < 1 or cc > m:
			dc *= -1
		r = r + dr
		c = c + dc
		time += 1
	print(time)


import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
from itertools import permutations
import string

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_int():
	return int(input())

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

def get_int_grid(n):
	return [get_ints() for _ in range(n)]

def get_str():
	return input().strip()

def get_strs():
	return get_str().split(' ')

def flat_list(arr):
	return [item for subarr in arr for item in subarr]

def yes_no(b):
	if b:
		return 'YES'
	else:
		return 'NO'
t = get_int()
for i in range(t):
	(n, m, rx, ry, dx, dy) = get_ints()
	if rx <= dx:
		xans = dx - rx
	else:
		xans = 2 * n - rx - dx
	if ry <= dy:
		yans = dy - ry
	else:
		yans = 2 * m - ry - dy
	print(min(xans, yans))


try:
	for i in range(int(input())):
		(n, m, xi, yi, xf, yf) = map(int, input().split())
		t = min(xf - xi if xf >= xi else n - xf + n - xi, yf - yi if yf >= yi else m - yf + m - yi)
		print(t)
except:
	print()


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	count = 0
	robot = [rb, cb]
	target = [rd, cd]
	i = 1
	j = 1
	while rb != rd and cb != cd:
		if rb == n:
			i = -1
		if cb == m:
			j = -1
		rb += i
		cb += j
		count += 1
	print(count)


t = int(input())
for _ in range(t):
	p = input()
	p = p.split()
	for i in range(len(p)):
		p[i] = int(p[i])
	a = p[4] - p[2]
	b = p[5] - p[3]
	if a == 0 or b == 0:
		print(0)
	elif a >= 0 and b >= 0:
		print(min(a, b))
	elif a >= 0 and b < 0:
		temp = 2 * (p[1] - p[3]) + p[3] - p[5]
		print(min(a, temp))
	elif b >= 0 and a < 0:
		temp = 2 * p[0] - p[2] - p[4]
		print(min(b, temp))
	else:
		temp = 2 * (p[1] - p[3]) + p[3] - p[5]
		temp1 = 2 * p[0] - p[2] - p[4]
		print(min(temp, temp1))


x = int(input())
for i in range(x):
	l = list(map(int, input().split()))
	if l[2] > l[4]:
		vertical = l[0] - l[4] + (l[0] - l[2])
	else:
		vertical = l[4] - l[2]
	if l[3] > l[5]:
		horizontal = 2 * l[1] - l[5] - l[3]
	else:
		horizontal = l[5] - l[3]
	print(min(horizontal, vertical))


t = int(input())
fl = []

def mod(a):
	if a > 0:
		return a
	else:
		return -a
for i in range(0, t):
	l = list(map(int, input().split()))
	tl = []
	n = l[0]
	m = l[1]
	xs = l[2]
	ys = l[3]
	xe = l[4]
	ye = l[5]
	f = 0
	if xs <= xe:
		tl.append(xe - xs)
	else:
		tl.append(n - xe + n - xs)
	if ys <= ye:
		tl.append(ye - ys)
	else:
		tl.append(m - ys + m - ye)
	fl.append(min(tl))
for i in fl:
	print(mod(i))


for _ in range(int(input())):
	(n, m, a, b, ta, tb) = map(int, input().split())
	if a > ta:
		v = 2 * (n - a) + a - ta
	else:
		v = ta - a
	if b > tb:
		h = 2 * (m - b) + b - tb
	else:
		h = tb - b
	ans = min(h, v)
	print(ans)


def solve():
	(n, m, x1, y1, x2, y2) = [int(i) for i in input().split()]
	if x1 == x2 or y1 == y2:
		print(0)
		return
	inf = 10000000000000
	a = n - x1 + n - x2
	b = m - y1 + m - y2
	if x1 < x2:
		a = min(a, x2 - x1)
	if y1 < y2:
		b = min(b, y2 - y1)
	if a != inf or b != inf:
		print(min(a, b))
		return
t = int(input())
for i in range(t):
	solve()


t = int(input())
for i in range(t):
	(n, m, a, b, c, d) = map(int, input().split())
	l = []
	if c >= a:
		l.append(c - a)
	else:
		l.append(n - a + (n - c))
	if d >= b:
		l.append(d - b)
	else:
		l.append(m - b + (m - d))
	print(min(l))


from __future__ import division, print_function
from collections import defaultdict
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def chk(m, um, mc, umc):
	ans = float('Inf')
	if m < 0 or um < 0 or mc < 0 or (umc < 0):
		return ans
	if m % 2 == 1 and mc == 1 + m - mc:
		ans = min(ans, m)
	if um % 2 == 0 and umc == um - umc:
		ans = min(ans, um)
	return ans
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

@bootstrap
def dfs(i, g, vis, color, count):
	vis[i] = True
	contains = False
	for j in g[i]:
		if not vis[j]:
			x = (yield dfs(j, g, vis, color, count))
			count[i] += x
			if x == 0:
				contains = True
	if contains == False and color[i] == 0:
		count[i] = 1
	else:
		count[i] = 0
	yield count[i]

@bootstrap
def dfs1(i, g, vis, count, g1):
	vis[i] = True
	ans = 1
	for j in g[i]:
		if not vis[j]:
			g1[i].append(j)
			x = (yield dfs1(j, g, vis, count, g1))
			ans += x
	count[i] = ans
	yield ans

@bootstrap
def dfs2(g1, i, vis):
	vis[i] = True
	for j in g1[i]:
		yield dfs2(g1, j, vis)
	yield 0

class FenwickTree:

	def __init__(self, x):
		self.bit = x
		for i in range(len(x)):
			j = i | i + 1
			if j < len(x):
				x[j] += x[i]

	def update(self, idx, x):
		while idx < len(self.bit):
			self.bit[idx] += x
			idx |= idx + 1

	def query(self, end):
		x = 0
		while end:
			x += self.bit[end - 1]
			end &= end - 1
		return x

	def findkth(self, k):
		idx = -1
		for d in reversed(range(len(self.bit).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(self.bit) and k >= self.bit[right_idx]:
				idx = right_idx
				k -= self.bit[idx]
		return idx + 1
from collections import deque
from math import ceil

def main():
	for _ in range(int(input())):
		(n, m, rb, cb, rd, cd) = map(int, input().split())
		ans = 0
		(dr, dc) = (1, 1)
		while 1:
			if rb == rd or cd == cb:
				print(ans)
				break
			if 1 <= rb + dr <= n:
				pass
			else:
				dr = -dr
			if 1 <= cb + dc <= m:
				pass
			else:
				dc = -dc
			rb += dr
			cb += dc
			ans += 1
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
for i in range(0, n):
	x = 1
	y = 1
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	c = 0
	while True:
		if rb == rd or cb == cd:
			print(c)
			break
		if rb == 1:
			y = 1
		if rb == n:
			y = -1
		if cb == 1:
			x = 1
		if cb == m:
			x = -1
		rb += y
		cb += x
		c += 1


def solve():
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	if rd >= rb:
		rCount = rd - rb
	else:
		rCount = rb - rd + 2 * (n - rb)
	if cd >= cb:
		cCount = cd - cb
	else:
		cCount = cb - cd + 2 * (m - cb)
	print(min(rCount, cCount))
for _ in range(int(input())):
	solve()


def solve(rows, cols, init_row, init_col, dirty_row, dirty_col):
	if init_row <= dirty_row:
		row_steps = dirty_row - init_row
	else:
		row_steps = rows - init_row + (rows - dirty_row)
	if init_col <= dirty_col:
		col_steps = dirty_col - init_col
	else:
		col_steps = cols - init_col + (cols - dirty_col)
	return min(row_steps, col_steps)
N = int(input())
for N in range(N):
	values = [int(x) for x in input().split(' ')]
	print(solve(*values))


def main():
	t = int(input())
	allans = []
	for _ in range(t):
		(n, m, rb, cb, rd, cd) = readIntArr()
		dr = dc = 1
		time = 0
		while True:
			if rb == rd or cb == cd:
				break
			if not 1 <= rb + dr <= n:
				dr *= -1
			if not 1 <= cb + dc <= m:
				dc *= -1
			rb += dr
			cb += dc
			time += 1
		allans.append(time)
	multiLineArrayPrint(allans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b, c):
	print('? {} {} {}'.format(a, b, c))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ansArr):
	print('! {}'.format(' '.join([str(x) for x in ansArr])))
	sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
	main()


t = int(input())
for k in range(t):
	(n, m, rb, cb, rd, cd) = [int(x) for x in input().split()]
	if rb == rd or cb == cd:
		print(0)
	else:
		if rb < rd:
			rr = rd - rb
		else:
			rr = n - rb + (n - rd)
		if cb < cd:
			cc = cd - cb
		else:
			cc = m - cb + (m - cd)
		print(min(rr, cc))


t = int(input())
for _ in range(t):
	arr = list(map(int, input().split()))
	(r, c) = (-1, -1)
	if arr[2] <= arr[4]:
		r = arr[4] - arr[2]
	else:
		r = 2 * arr[0] - arr[2] - arr[4]
	if arr[3] <= arr[5]:
		c = arr[5] - arr[3]
	else:
		c = 2 * arr[1] - arr[3] - arr[5]
	print(min(r, c))


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, sys.stdin.readline().split())
	ans = 0
	(tr, tc) = (rd - rb, cd - cb)
	if tr < 0:
		tr = 2 * n - rb - rd
	if tc < 0:
		tc = 2 * m - cb - cd
	print(min(tr, tc))


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	a1 = rd - rb if rd >= rb else n - rd + n - rb
	a2 = cd - cb if cd >= cb else m - cd + m - cb
	print(min(a1, a2))


T = int(input())
i = 0
timeArray = []
while i < T:
	nArr = input().split()
	n = list(map(int, nArr))
	case_one = n[2] <= n[4]
	case_two = n[3] <= n[5]
	if case_one and case_two:
		outTime = min(n[4] - n[2], n[5] - n[3])
	elif case_one and (not case_two):
		outTime = min(n[4] - n[2], 2 * n[1] - n[5] - n[3])
	elif case_two and (not case_one):
		outTime = min(n[5] - n[3], 2 * n[0] - n[2] - n[4])
	else:
		outTime = min(2 * n[0] - n[2] - n[4], 2 * n[1] - n[3] - n[5])
	timeArray.append(outTime)
	i += 1
for x in timeArray:
	print(x)


t = int(input())
for t1 in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	maxr = rd - rb if rb <= rd else n - rb + (n - rd)
	maxc = cd - cb if cb <= cd else m - cb + (m - cd)
	print(min(maxr, maxc))


t = int(input())
for i in range(t):
	a = list(map(int, input().split()))
	(rx, ry) = (0, 0)
	if a[2] > a[4]:
		rx = 1
	if a[3] > a[5]:
		ry = 1
	if (a[2] == a[4]) | (a[3] == a[5]):
		print(0)
	else:
		res = min(abs(a[2] - a[4]) + (a[0] - a[2]) * rx * 2, abs(a[3] - a[5]) + (a[1] - a[3]) * ry * 2)
		print(res)


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = [int(x) for x in input().split()]
	x_arrive = 0
	y_arrive = 0
	if cd - cb >= 0:
		x_arrive = cd - cb
	else:
		x_arrive = 2 * (m - cb) + (cb - cd)
	if rd - rb >= 0:
		y_arrive = rd - rb
	else:
		y_arrive = 2 * (n - rb) + (rb - rd)
	print(min(y_arrive, x_arrive))


import sys
for t in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split(' ')))
	ans = sys.maxsize
	tmp = cd - cb
	if tmp > 0:
		ans = tmp
	tmp = rd - rb
	if tmp > 0:
		ans = min(ans, tmp)
	if cd - cb == 0 or rd - rb == 0:
		ans = 0
	ans = min(ans, m - cb + m - cd, n - rb + n - rd)
	print(ans)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	ans = 0
	(s1, s2) = (1, 1)
	while rb != rd and cb != cd:
		if cb == m:
			s2 *= -1
		if rb == n:
			s1 *= -1
		cb += s2
		rb += s1
		ans += 1
	print(ans)


import sys

def solve(n, m, rb, cb, rd, cd):
	if rd >= rb:
		if cd >= cb:
			return min(abs(rd - rb), abs(cd - cb))
		else:
			return min(abs(rd - rb), abs(m - cb) * 2 + abs(cd - cb))
	elif cd >= cb:
		return min(abs(n - rb) * 2 + abs(rb - rd), abs(cd - cb))
	else:
		return min(abs(n - rb) * 2 + abs(rb - rd), abs(m - cb) * 2 + abs(cd - cb))
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, sys.stdin.readline().split(' '))
	t = solve(n, m, rb, cb, rd, cd)
	print(t)


for t in range(int(input())):
	(n, m, a, b, c, d) = map(int, input().split())
	print(min(c - a if c >= a else 2 * (n - a) + a - c, d - b if d >= b else 2 * (m - b) + b - d))


t = int(input())
for i in range(t):
	ans = 0
	(n, m, rb, cb, rd, cd) = [int(l) for l in input().split()]
	if rb <= rd:
		x = rd - rb
	else:
		x = n - rb + (n - rd)
	if cb <= cd:
		y = cd - cb
	else:
		y = m - cb + (m - cd)
	print(min(x, y))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = (int(i) for i in input().split())
	ans = 0
	if rb > rd and cb > cd:
		ans = min(n - rb + n - rd, m - cb + m - cd)
	elif rb > rd:
		ans = min(n - rb + n - rd, cd - cb)
	elif cb > cd:
		ans = min(m - cb + m - cd, rd - rb)
	else:
		ans = min(rd - rb, cd - cb)
	print(ans)


for _ in range(int(input())):
	(n, m, a, b, c, d) = map(int, input().split())
	r = 0
	f = 0
	f1 = 0
	for i in range(10010):
		if a == c or b == d:
			break
		if a < n and f == 0:
			a += 1
		else:
			if a <= 1:
				f = 0
				continue
			a -= 1
			f = 1
		if b < m and f1 == 0:
			b += 1
		else:
			if b <= 1:
				f1 = 0
				continue
			b -= 1
			f1 = 1
		r += 1
	print(r)


for _ in range(int(input())):
	l = list(map(int, input().split()))
	a = l[4] - l[2]
	b = l[5] - l[3]
	if a >= 0 and b >= 0:
		print(min(a, b))
	elif a < 0 and b >= 0:
		a = abs(a) + 2 * (l[0] - l[2])
		print(min(a, b))
	elif a >= 0 and b < 0:
		b = abs(b) + 2 * (l[1] - l[3])
		print(min(a, b))
	else:
		a = abs(a) + 2 * (l[0] - l[2])
		b = abs(b) + 2 * (l[1] - l[3])
		print(min(a, b))


t = int(input())
for i in range(t):
	A = list(map(int, input().split()))
	n = A[0]
	m = A[1]
	rb = A[2]
	cb = A[3]
	rd = A[4]
	cd = A[5]
	l = []
	if rb <= rd:
		l.append(rd - rb)
	else:
		l.append(2 * n - rb - rd)
	if cb <= cd:
		l.append(cd - cb)
	else:
		l.append(2 * m - cb - cd)
	m = float('inf')
	for i in l:
		if m > i:
			m = i
	print(m)


for i in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	print(min(rd - rb if rd >= rb else n - rb + n - rd, cd - cb if cd >= cb else m - cb + m - cd))


for i in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(dr, dc) = (1, 1)
	ans = 0
	while True:
		if rb == rd or cb == cd:
			print(ans)
			break
		if rb == n:
			dr = -dr
		if cb == m:
			dc = -dc
		rb += dr
		cb += dc
		ans += 1


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split(' '))
	if rb <= rd:
		val1 = rd - rb
	else:
		val1 = 2 * n - rd - rb
	if cb <= cd:
		val2 = cd - cb
	else:
		val2 = 2 * m - cd - cb
	print(min(val1, val2))


for _ in range(int(input())):
	(n, m, r1, c1, r2, c2) = map(int, input().split())
	x = r2 - r1
	y = c2 - c1
	if x >= 0 and y >= 0:
		print(min(x, y))
	elif x < 0 and y < 0:
		a = n - r1 + n - r2
		b = m - c1 + m - c2
		print(min(a, b))
	elif x >= 0 and y < 0:
		b = m - c1 + m - c2
		print(min(x, b))
	elif x < 0 and y >= 0:
		a = n - r1 + n - r2
		print(min(y, a))


for _ in range(int(input())):
	(n, m, x, y, a, b) = map(int, input().split())
	if x > a:
		d = n - x + (n - a)
	else:
		d = a - x
	if y > b:
		t = m - y + (m - b)
	else:
		t = b - y
	print(min(d, t))


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	if rb <= rd:
		x = rd - rb
	else:
		x = 2 * n - rb - rd
	if cb <= cd:
		y = cd - cb
	else:
		y = 2 * m - cb - cd
	print(min(x, y))


for _ in range(int(input())):
	(n, m, rb, rd, cb, cd) = map(int, input().split())
	arr = [cb - rb, cd - rd, 2 * n - cb - rb, 2 * m - cd - rd]
	minm = 10 ** 8
	for i in arr:
		if i < minm and i >= 0:
			minm = i
	print(minm)


t = int(input())
for i in range(t):
	(n, m, rx, ry, mx, my) = map(int, input().split())
	t1x = 0
	t1y = 0
	dx = 1
	dy = 1
	if mx >= rx:
		t1x = mx - rx
	else:
		t1x = 2 * n - mx - rx
	if my >= ry:
		t1y = my - ry
	else:
		t1y = 2 * m - my - ry
	print(min(t1x, t1y))


def solve(n, m, robot_row, robot_collum, spot_row, spot_collum):
	if robot_row == spot_row or robot_collum == spot_collum:
		print(0)
		return
	if robot_collum < spot_collum:
		collum_time = spot_collum - robot_collum
	else:
		collum_time = m - robot_collum + (m - spot_collum)
	if robot_row < spot_row:
		row_time = spot_row - robot_row
	else:
		row_time = n - robot_row + (n - spot_row)
	print(min(row_time, collum_time))
t = int(input())
for i in range(t):
	info = input().split(' ')
	solve(*[int(info[i]) for i in range(len(info))])


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split(' '))
	if rb == rd or cb == cd:
		print('0')
		continue
	if rb < rd:
		rm = rd - rb
	else:
		rm = (n - rb) * 2 + (rb - rd)
	if cb < cd:
		cm = cd - cb
	else:
		cm = (m - cb) * 2 + (cb - cd)
	print(min(rm, cm))


def res(size, bot_pos, dirty_pos):
	if dirty_pos >= bot_pos:
		return dirty_pos - bot_pos
	else:
		return (size - bot_pos) * 2 + bot_pos - dirty_pos
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	print(min(res(n, rb, rd), res(m, cb, cd)))


x = int(input())
while x > 0:
	w = [int(i) for i in input().split()]
	n = w[0]
	c = w[1]
	rb = w[2]
	rc = w[3]
	d = [w[4], w[5]]
	s = 1
	if d[0] - rb < 0 and d[1] - rc >= 0:
		a = n - rb
		t = min(d[1] - rc, n - d[0] + a)
		print(t)
	elif d[0] - rb >= 0 and d[1] - rc < 0:
		b = c - rc
		t = min(d[0] - rb, c - d[1] + b)
		print(t)
	elif d[0] - rb >= 0 and d[1] - rc >= 0:
		t = min(d[0] - rb, d[1] - rc)
		print(t)
	else:
		a = n - rb
		b = c - rc
		t = min(n - d[0] + a, c - d[1] + b)
		print(t)
	x -= 1


t = int(input())
ans = []
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(dr, dc) = (1, 1)
	(sr, sc) = (rb, cb)
	time = 0
	if rb < rd:
		r_time = rd - rb
	if rb > rd:
		r_time = n - rb + n - rd
	if rb == rd:
		r_time = 0
	if cb < cd:
		c_time = cd - cb
	if cb > cd:
		c_time = m - cb + m - cd
	if cb == cd:
		c_time = 0
	ans.append(min(r_time, c_time))
for _ in ans:
	print(_)


for i in range(int(input())):
	(n, m, a1, b1, a2, b2) = map(int, input().split())
	if a2 >= a1:
		c1 = a2 - a1
	else:
		c1 = n - a1 + n - a2
	if b2 >= b1:
		c2 = b2 - b1
	else:
		c2 = m - b1 + m - b2
	print(min(c1, c2))


res = []
for _ in range(int(input())):
	[n, m, rb, cb, rd, cd] = list(map(int, input().split()))
	r_cnt = 2 * (n - rb) + (rb - rd) if rb > rd else rd - rb
	c_cnt = 2 * (m - cb) + (cb - cd) if cb > cd else cd - cb
	res.append(min(r_cnt, c_cnt))
for ans in res:
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')

def simulate(dr, dc, rb, cb, time=0):
	if rd == rb or cb == cd:
		return time
	else:
		if rb == n or (rb == 1 and dr != 1):
			dr = -dr
		if cb == m or (cb == 1 and dc != 1):
			dc = -dc
		rb = rb + dr
		cb = cb + dc
		return simulate(dr, dc, rb, cb, time + 1)
for t in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	print(simulate(1, 1, rb, cb))


n = int(input())

def canClean(rn, cn, rd, cd):
	if rn == rd or cn == cd:
		return True
	else:
		return False
li = []
for _ in range(n):
	(n, m, rn, cn, rd, cd) = map(int, input().split())
	dx = 1
	dy = 1
	t = 0
	while not canClean(rn, cn, rd, cd):
		if rn + dy > n or rn + dy < 1:
			dy *= -1
		if cn + dx > m or cn + dx < 1:
			dx *= -1
		rn = rn + dy
		cn = cn + dx
		t += 1
	li.append(t)
for i in li:
	print(i)


test_count = int(input())
for i in range(test_count):
	(n, m, r, c, x, y) = map(int, input().split())
	moves = 0
	row_difference = 0
	column_difference = 0
	if row_difference == 0:
		if y - c >= 0:
			row_difference = y - c
		else:
			row_difference = m - c + (m - y)
	if column_difference == 0:
		if x - r >= 0:
			column_difference = x - r
		else:
			column_difference = n - r + (n - x)
	if row_difference <= column_difference:
		print(row_difference)
	else:
		print(column_difference)


numberTest = int(input())
for _ in range(numberTest):
	(n, m, a, b, c, d) = map(int, input().split())
	re1 = 0
	res2 = 0
	if a <= c:
		res1 = c - a
	else:
		res1 = 2 * n - a - c
	if b <= d:
		res2 = d - b
	else:
		res2 = 2 * m - b - d
	print(min(res1, res2))


import math
tc = int(input())
while tc != 0:
	tc = tc - 1
	(n, m, rb, cb, rd, cd) = [int(x) for x in input().split()]
	ans = math.inf
	if rd >= rb:
		ans = min(ans, rd - rb)
	else:
		ans = min(ans, 2 * (n - rb) + rb - rd)
	if cd >= cb:
		ans = min(ans, cd - cb)
	else:
		ans = min(ans, 2 * (m - cb) + cb - cd)
	print(ans)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, m, x, y, z, w) = map(int, sys.stdin.readline().split())
	dx = dy = 1
	ans = 0
	while True:
		if x == z or y == w:
			print(ans)
			break
		if x == n and dx == 1 or (x == 0 and dx == -1):
			dx *= -1
		if y == m and dy == 1 or (y == 0 and dy == -1):
			dy *= -1
		x += dx
		y += dy
		ans += 1


import math
cs = int(input())
for i in range(cs):
	(n, m, rb, cb, rd, cd) = input().split()
	n = int(n)
	cd = int(cd)
	m = int(m)
	rb = int(rb)
	cb = int(cb)
	rd = int(rd)
	sol = min(rd - rb if rd >= rb else n - rb + n - rd, cd - cb if cd >= cb else m - cb + m - cd)
	print(sol)


t = int(input())
resultado = []

def robot(case, tempo, check, check_c):
	posicao_atual = case[2:4]
	n_row = case[0]
	m_col = case[1]
	row_b = case[2]
	col_b = case[3]
	row_d = case[4]
	col_d = case[5]
	while row_b != row_d and col_b != col_d and (posicao_atual >= [0, 0]):
		if row_b < n_row and check == True:
			row_b += 1
			tempo += 1
		elif row_b == n_row:
			row_b -= 1
			tempo += 1
			check = False
		elif row_b < n_row and check == False:
			row_b -= 1
			tempo += 1
		if col_b < m_col and check_c == True:
			col_b += 1
		elif col_b == m_col:
			col_b -= 1
			check_c = False
		else:
			col_b -= 1
	return tempo
for i in range(t):
	case = input().split()
	case = [int(j) for j in case]
	tempo = 0
	check_row = True
	check_col = True
	x = robot(case, tempo, check_row, check_col)
	resultado.append(x)
for l in resultado:
	print(l)


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	r_time = None
	c_time = None
	if rb <= rd:
		r_time = rd - rb
	else:
		r_time = n - rb + n - rd
	if cb <= cd:
		c_time = cd - cb
	else:
		c_time = m - cb + m - cd
	print(min(r_time, c_time))


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(count_r, count_c) = (0, 0)
	if rb > rd:
		count_r = n - rb + (n - rd)
	if rb < rd:
		count_r = rd - rb
	if cb > cd:
		count_c = m - cb + (m - cd)
	if cd > cb:
		count_c = cd - cb
	print(min(count_r, count_c))


n = int(input())
for i in range(n):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	res = []
	(f, s) = (1, 1)
	if rb != rd and cb != cd:
		if rb > rd:
			res.append(n - rb + (n - rd))
		else:
			res.append(abs(rb - rd))
		if cb > cd:
			res.append(m - cb + (m - cd))
		else:
			res.append(abs(cb - cd))
		print(min(res))
	else:
		print(0)


import sys
input = sys.stdin.readline

def iinput():
	return int(input())

def sinput():
	return input().rstrip()

def i0input():
	return int(input()) - 1

def linput():
	return list(input().split())

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

def miinput():
	return map(int, input().split())

def li0input():
	return list(map(lambda x: int(x) - 1, input().split()))

def mi0input():
	return map(lambda x: int(x) - 1, input().split())
INF = 10 ** 20
MOD = 1000000007
t = iinput()
for _ in range(t):
	(n, m, rb, cb, rd, cd) = liinput()
	rb -= 1
	cb -= 1
	rd -= 1
	cd -= 1
	room = [[False for _ in range(m)] for _ in range(n)]
	for i in range(m):
		room[rd][i] = True
	for i in range(n):
		room[i][cd] = True
	ctr = 0
	dr = 1
	dc = 1
	while True:
		if room[rb][cb]:
			break
		else:
			ctr += 1
			if rb + dr < 0 or n <= rb + dr:
				dr *= -1
			if not 0 <= cb + dc < m:
				dc *= -1
			rb += dr
			cb += dc
	print(ctr)


for _ in range(int(input())):
	pos = list(map(int, input().split()))
	n = pos[0]
	m = pos[1]
	x = pos[2]
	y = pos[3]
	a = pos[4]
	b = pos[5]
	if x == a or y == b:
		print(0)
	if a < x and b < y:
		print(min(n - x + n - a, m - y + m - b))
	if a < x and y < b:
		print(min(n - x + n - a, b - y))
	if x < a and b < y:
		print(min(a - x, m - y + m - b))
	if a > x and b > y:
		print(min(a - x, b - y))


t = int(input())
for _ in range(t):
	(n, m, r_b, c_b, r_d, c_d) = map(int, input().split())
	(d_r, d_c) = (1, 1)
	time = 0
	while True:
		if r_b == r_d or c_b == c_d:
			print(time)
			break
		if r_b == 1:
			d_r = 1
		if r_b == n:
			d_r = -1
		if c_b == 1:
			d_c = 1
		if c_b == m:
			d_c = -1
		r_b += d_r
		c_b += d_c
		time += 1


t = int(input())
while t > 0:
	t -= 1
	nums = [int(c) for c in input().split(' ')]
	(n, m, rb, cb, rd, cd) = (nums[0], nums[1], nums[2], nums[3], nums[4], nums[5])
	ans = float('inf')
	if rd >= rb:
		ans = min(ans, rd - rb)
	else:
		ans = min(ans, 2 * (n - rb) + rb - rd)
	if cd >= cb:
		ans = min(ans, cd - cb)
	else:
		ans = min(ans, 2 * (m - cb) + cb - cd)
	print(ans)


for _ in range(int(input())):
	(n, m, rx, ry, cx, cy) = map(int, input().split())
	if rx > cx:
		ans_x = 2 * (n - rx) + rx - cx
	else:
		ans_x = cx - rx
	if ry > cy:
		ans_y = 2 * (m - ry) + ry - cy
	else:
		ans_y = cy - ry
	print(min(ans_x, ans_y))


def countTime(n, m, rb, cb, rd, cd):
	dr = rd - rb if rd >= rb else n - rb + (n - rd)
	dc = cd - cb if cd >= cb else m - cb + (m - cd)
	return dr if dr <= dc else dc
t = int(input())
ans = []
for i in range(t):
	(n, m, rb, cb, rd, cd) = [int(x) for x in input().split()]
	ans.append(countTime(n, m, rb, cb, rd, cd))
for i in ans:
	print(i)


X = lambda : map(int, input().split())
(t,) = X()
for i in range(t):
	(n, m, rb, cb, rd, cd) = X()
	time = 0
	indr = 1
	indc = 1
	while True:
		if rb == rd or cb == cd:
			break
		if rb < n and cb < m:
			rb += indr
			cb += indc
		else:
			if rb == n:
				indr = -1 * indr
			if cb == m:
				indc = -1 * indc
			rb += indr
			cb += indc
		time += 1
	print(time)


css = int(input())
for _ in range(css):
	inputSet = [int(num) for num in input().split()]
	(n, m) = inputSet[:2]
	(rb, cb) = inputSet[2:4]
	(rd, cd) = inputSet[4:]
	verticalCost = rd - rb if rd >= rb else n - rb + n - rd
	horizontalCost = cd - cb if cd >= cb else m - cb + m - cd
	print(min(verticalCost, horizontalCost))


n = int(input())
for i in range(n):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	j1 = 1
	i1 = 1
	count = 0
	while rb != rd and cb != cd:
		if rb == n:
			j1 = -1
		if rb == 1:
			j1 = 1
		if cb == m:
			i1 = -1
		if cb == 1:
			i1 = 1
		rb += j1
		cb += i1
		count += 1
	print(count)


for _ in range(int(input())):
	(n, m, x1, y1, x2, y2) = [int(x) for x in input().split()]
	if x2 >= x1:
		d1 = x2 - x1
	else:
		d1 = 2 * n - x1 - x2
	if y2 >= y1:
		d2 = y2 - y1
	else:
		d2 = 2 * m - y1 - y2
	print(min(d1, d2))


for _ in range(int(input())):
	(n, m, x, y, xk, yk) = map(int, input().split())
	(dx, dy) = (1, 1)
	ans = 0
	while True:
		if x == xk or y == yk:
			break
		if x == n and dx == 1:
			dx = -1
		elif x == 1 and dx == -1:
			dx = 1
		if y == m and dy == 1:
			dy = -1
		elif y == 1 and dy == -1:
			dy = 1
		ans += 1
		x += dx
		y += dy
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
import math
import heapq
import bisect
import collections

def ceil(a, b):
	return (a + b - 1) // b
BUFSIZE = 8192
inf = float('inf')

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
get = lambda : sys.stdin.readline().rstrip('\r\n')
MOD = 1000000007

def v():
	return list()
for _ in range(int(get())):
	(n, m, rb, cb, rd, cd) = map(int, get().split())
	a = 1
	b = 1
	ans = 0
	while True:
		if rb == rd or cb == cd:
			break
		if rb + a > n:
			a = -1
		if rb + a < 1:
			a = +1
		if cb + b > m:
			b = -1
		if cb + b < 1:
			b = +1
		rb += a
		cb += b
		ans += 1
	print(ans)


import math
import itertools
from sys import stdin, stdout

def test_case():
	a = [int(x) for x in stdin.readline().rstrip().split()]
	(rows, cols, robot_row, robot_col, dirt_row, dirt_col) = a
	if dirt_col >= robot_col:
		v = dirt_col - robot_col
	else:
		v = cols - robot_col + (cols - dirt_col)
	if dirt_row >= robot_row:
		h = dirt_row - robot_row
	else:
		h = rows - robot_row + (rows - dirt_row)
	print(min(v, h))
t = int(stdin.readline())
for i in range(t):
	test_case()


from sys import stdin
from heapq import heappush, heappop, heapify, heappushpop
lines = stdin.read().splitlines()
li = -1

def inp():
	global li
	li += 1
	return lines[li]

def inpint():
	global li
	li += 1
	return int(lines[li])

def inpints(change=0):
	global li
	li += 1
	return [int(i) + change for i in lines[li].split()]

def inpfloat():
	global li
	li += 1
	return float(lines[li])

def inpfloats(change=0):
	global li
	li += 1
	return [float(i) + change for i in lines[li].split()]
t = inpint()
for T in range(t):
	(R, C, rb, cb, rt, ct) = inpints()
	dr = 1
	dc = 1
	step = 0
	while True:
		if rb == rt or cb == ct:
			break
		if rb + dr > R or rb + dr < 0:
			dr = -dr
		rb += dr
		if cb + dc > C or cb + dc < 0:
			dc = -dc
		cb += dc
		step += 1
	print(step)


poctest = int(input())
for i in range(poctest):
	(n, m, zy, zx, by, bx) = map(int, input().split())
	dx = bx - zx
	dy = by - zy
	if dx >= 0:
		najdux = dx
	else:
		najdux = 2 * m - zx - bx
	if dy >= 0:
		najduy = dy
	else:
		najduy = 2 * n - zy - by
	print(min(najdux, najduy))


t = int(input())
for _ in range(t):
	(n, m, startRow, startCol, dirtyRow, dirtyCol) = map(int, input().split())
	if startRow <= dirtyRow and startCol <= dirtyCol:
		print(min(dirtyRow - startRow, dirtyCol - startCol))
	elif startRow <= dirtyRow or startCol <= dirtyCol:
		if startRow <= dirtyRow:
			print(min(dirtyRow - startRow, 2 * (m - startCol) + (startCol - dirtyCol)))
		else:
			print(min(dirtyCol - startCol, 2 * (n - startRow) + (startRow - dirtyRow)))
	else:
		print(min(2 * (m - startCol) + (startCol - dirtyCol), 2 * (n - startRow) + (startRow - dirtyRow)))


t = int(input())
for test_case in range(t):
	(n, m, r_b, c_b, r_d, c_d) = map(int, input().split())
	if r_b == r_d or c_b == c_d:
		print(0)
	else:
		if c_d > c_b:
			col_time = c_d - c_b
		elif c_d < c_b:
			col_time = m - c_b + m - c_d
		if r_d > r_b:
			row_time = r_d - r_b
		else:
			row_time = n - r_b + n - r_d
		if row_time >= col_time:
			print(col_time)
		else:
			print(row_time)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, m, r, b, rd, bd) = map(int, sys.stdin.readline().split())
	rc = 1
	dc = 1
	t = 0
	while r != rd or b != bd:
		if r == rd or b == bd:
			break
		if r == n:
			rc = -1
		if b == m:
			dc = -1
		r = r + rc
		b = b + dc
		t += 1
	print(t)


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb == rd or cb == cd:
		print(0)
	else:
		if rd - rb > 0:
			t1 = rd - rb
		else:
			t1 = 2 * (n - rb) + (rb - rd)
			pass
		if cd - cb > 0:
			t2 = cd - cb
		else:
			t2 = 2 * (m - cb) + (cb - cd)
		print(min(t1, t2))


import math

def Solve():
	(n, m, a, b, c, d) = map(int, input().split())
	C = 0
	(r1, r2) = (1, 1)
	while a != c and b != d:
		C = C + 1
		if a == n:
			r1 = -1
		if b == m:
			r2 = -1
		if a == 1:
			r1 = 1
		if b == 1:
			r2 = 1
		(a, b) = (a + r1, b + r2)
	print(C)
q = int(input())
while q > 0:
	Solve()
	q = q - 1


for _ in range(int(input())):
	(n, m, x, y, r, c) = map(int, input().split())
	if y > c:
		h = m - y + m - c
	else:
		h = c - y
	if x > r:
		v = n - x + n - r
	else:
		v = r - x
	print(min(h, v))


import sys
import os.path
from collections import *
import math
import bisect
import heapq as hq
from fractions import Fraction
from random import randint
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline

def solve():
	(n, m, r, c, x, y) = [int(x) for x in input().split()]
	r -= 1
	c -= 1
	x -= 1
	y -= 1
	dirx = 1
	diry = 1
	cnt = 0
	while r != x and c != y:
		u = r + dirx
		v = c + diry
		if u >= n or u < 0:
			dirx *= -1
		if v < 0 or v >= m:
			diry *= -1
		r += dirx
		c += diry
		cnt += 1
	print(cnt)
t = int(input())
while t:
	t -= 1
	solve()


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().strip().split())
	row = None
	if rd == rb:
		row = 0
	elif rd < rb:
		row = abs(n - rb) + abs(n - rd)
	else:
		row = abs(rb - rd)
	col = None
	if cd == cb:
		col = 0
	elif cd < cb:
		col = abs(m - cb) + abs(m - cd)
	else:
		col = abs(cb - cd)
	print(min(row, col))


from sys import maxsize
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	ans = maxsize
	if rb <= rd:
		ans = min(ans, rd - rb)
	else:
		ans = min(ans, n - rb + (n - rd))
	if cb <= cd:
		ans = min(ans, cd - cb)
	else:
		ans = min(ans, m - cb + (m - cd))
	print(ans)


for i in range(int(input())):
	(r, c, a, b, x, y) = map(int, input().split())
	print(min(y - b if y >= b else 2 * c - (b + y), x - a if x >= a else 2 * r - (a + x)))


def checker(rb, cb, rd, cd):
	if rb == rd or cb == cd:
		return 1
	else:
		return 0

def main():
	_t = int(input())
	for m in range(_t):
		(n, m, rb, cb, rd, cd) = map(int, input().split())
		(rb, cb, rd, cd) = (rb - 1, cb - 1, rd - 1, cd - 1)
		check1 = checker(rb, cb, rd, cd)
		if check1 == 1:
			print(0)
			continue
		answer = 0
		if rb == 0:
			x_mover = -1
		else:
			x_mover = 1
		if cb == 0:
			y_mover = -1
		else:
			y_mover = 1
		while True:
			if rb == n - 1 or rb == 0:
				x_mover *= -1
			if cb == m - 1 or cb == 0:
				y_mover *= -1
			rb += x_mover
			cb += y_mover
			check2 = checker(rb, cb, rd, cd)
			if check2 == 1:
				print(answer + 1)
				break
			else:
				answer += 1
main()


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb <= rd:
		a = rd - rb
	else:
		a = 2 * n - rb - rd
	if cb <= cd:
		b = cd - cb
	else:
		b = 2 * m - cb - cd
	print(min(a, b))


debug = False
if debug:
	s = '5\n    10 10 6 1 2 8\n    10 10 9 9 1 1\n    9 8 5 6 2 1\n    6 9 2 2 5 8\n    2 2 1 1 2 1'
	input_data = iter(s.split('\n'))
else:

	def func():
		while 1:
			yield input()
	input_data = func()

def get_data():
	n_data = int(next(input_data))
	for _ in range(n_data):
		(n, m, cleaner_row, cleaner_column, mud_row, mud_column) = map(int, next(input_data).split())
		yield (n, m, cleaner_row, cleaner_column, mud_row, mud_column)

def solve(n, m, cleaner_row, cleaner_column, mud_row, mud_column, dr=1, dc=1):
	steps = 0
	while cleaner_row != mud_row and cleaner_column != mud_column:
		if not 0 < cleaner_row + dr <= n:
			dr = -dr
		if not 0 < cleaner_column + dc <= m:
			dc = -dc
		cleaner_row = cleaner_row + dr
		cleaner_column = cleaner_column + dc
		steps += 1
	print(steps)

def main():
	for data in get_data():
		solve(*data)
main()


t = int(input())
for z in range(t):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	if rb == rd or cb == cd:
		print(0)
	else:
		time = 0
		row = 1
		col = 1
		while True:
			if rb == rd or cb == cd:
				break
			if rb == n:
				row = -1
			if cb == m:
				col = -1
			rb += row
			cb += col
			time += 1
		print(time)


import sys
int1 = lambda x: int(x) - 1
pDB = lambda *x: print(*x, end='\n', file=sys.stderr)
p2D = lambda x: print(*x, sep='\n', end='\n\n', file=sys.stderr)

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline().rstrip()
inf = 4294967295
md = 998244353

def cost(i, s, h):
	if i <= s:
		return s - i
	return h - i + h - s

def solve():
	(h, w, i, j, s, t) = LI()
	ans = min(cost(i, s, h), cost(j, t, w))
	print(ans)
for testcase in range(II()):
	solve()


def f(n, m, rb, cb, rd, cd):
	if rb > rd:
		distX = n - rb + n - rd
	else:
		distX = rd - rb
	if cb > cd:
		distY = m - cb + m - cd
	else:
		distY = cd - cb
	return min(distY, distX)
x = int(input())
c = list()
for i in range(x):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	c.append(str(f(n, m, rb, cb, rd, cd)))
print('\n'.join(c))


def read_ints():
	return [int(x) for x in input().rstrip().rsplit()]
nn = int(input())
while nn > 0:
	(n, m, rb, cb, rd, cd) = read_ints()
	dr = 1
	dc = 1
	counter = 0
	while True:
		if rb == rd or cb == cd:
			break
		counter += 1
		if rb + dr > n or rb + dr < 1:
			dr *= -1
		if cb + dc > m or cb + dc < 1:
			dc *= -1
		rb += dr
		cb += dc
	print(counter)
	nn -= 1


from sys import setrecursionlimit
from bisect import bisect_left, bisect_right
from collections import deque
from functools import lru_cache, reduce
from heapq import heappush, heappop
from math import sqrt, ceil, floor, log2
T = int(input())

def rl(t=int):
	return list(map(t, input().split()))
for t in range(1, T + 1):
	(n, m, sx, sy, tx, ty) = rl()
	(dx, dy) = (1, 1)
	if sx == 1:
		dx = -1
	if sy == 1:
		dy = -1
	ret = 0
	while sx != tx and sy != ty:
		if sx in [1, n]:
			dx = 1 if dx == -1 else -1
		if sy in [1, m]:
			dy = 1 if dy == -1 else -1
		sx += dx
		sy += dy
		ret += 1
	print(ret)


import sys
from sys import stdin, stdout
from math import ceil, floor, sqrt, log, log2, log10
from collections import Counter
from typing import runtime_checkable
int_max = float('inf')
int_min = -float('inf')
mod = 10 ** 9 + 7
ANS = []
input = stdin.readline

def solve():
	pass
t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	d1 = 0
	d2 = 0
	if rd < rb:
		d1 = 2 * (n - rb) + rb - rd
	else:
		d1 = rd - rb
	if cd < cb:
		d2 = 2 * (m - cb) + cb - cd
	else:
		d2 = cd - cb
	print(min(d1, d2))


instances = int(input())
for i in range(instances):
	data = [int(j) for j in input().split()]
	(n, m, r_b, c_b, r_d, c_d) = data
	(dr, dc) = (1, 1)
	time = 0
	while r_b != r_d and c_b != c_d:
		if r_b == n:
			dr = -dr
		if c_b == m:
			dc = -dc
		(r_b, c_b) = (r_b + dr, c_b + dc)
		time += 1
	print(time)


cases = int(input())
finalstr = ''
for case in range(cases):
	currentcase = input().split(' ')
	columnlist = []
	robotpos = []
	dirtpos = []
	for (index, newcase) in enumerate(currentcase):
		if index <= 1:
			columnlist.append(int(newcase))
		elif index <= 3:
			robotpos.append(int(newcase))
		else:
			dirtpos.append(int(newcase))
	time = []
	if robotpos[0] != dirtpos[0] and robotpos[1] != dirtpos[1]:
		for i in range(2):
			if robotpos[i] < dirtpos[i]:
				time.append(dirtpos[i] - robotpos[i])
			elif robotpos[i] > dirtpos[i]:
				time.append(2 * columnlist[i] - robotpos[i] - dirtpos[i])
		time.sort()
		finalstr += f'{time[0]}\n'
	else:
		finalstr += f'0\n'
print(finalstr)


x = int(input())
ll = []
for i in range(x):
	y = input('')
	ly = list(map(int, y.split()))
	ll.append(ly)

def diff(i, f, l):
	if i < f:
		res = f - i
	elif i == f:
		res = 0
	else:
		res = l - i + (l - f)
	return res
for ly in ll:
	a = diff(ly[2], ly[4], ly[0])
	b = diff(ly[3], ly[5], ly[1])
	if a <= b:
		print(a)
	else:
		print(b)


for _ in range(int(input())):
	(a, b, c, d, e, f) = map(int, input().split())
	if d <= f:
		n = f - d
	else:
		n = b - d + (b - f)
	if c <= e:
		m = e - c
	else:
		m = a - e + (a - c)
	print(min(n, m))


import sys
input = sys.stdin.readline

def print(*args, end='\n', sep=' ') -> None:
	sys.stdout.write(sep.join(map(str, args)) + end)

def solve() -> None:
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb > rd:
		t = 2 * n - rb - rd
	else:
		t = rd - rb
	if cb > cd:
		t = min(t, 2 * m - cb - cd)
	else:
		t = min(t, cd - cb)
	print(t)

def main() -> None:
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
input = sys.stdin.readline
M = 1000000007

def solve():
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rd == rb or cd == cb:
		return 0
	if rd > rb:
		if cd > cb:
			return min(rd - rb, cd - cb)
		return min(rd - rb, m - cb + m - cd)
	if cd > cb:
		return min(cd - cb, n + n - rb - rd)
	return min(n + n - rb - rd, m + m - cb - cd)
for _ in range(int(input())):
	print(solve())


t = int(input())
while t > 0:
	t -= 1
	(rows, cols, row_bot, col_bot, row_dirt, col_dirt) = list(map(int, input().split(' ')))
	(row_time, col_time) = (0, 0)
	if row_bot > row_dirt:
		row_time = 2 * rows - row_bot - row_dirt
	else:
		row_time = row_dirt - row_bot
	if col_bot > col_dirt:
		col_time = 2 * cols - col_bot - col_dirt
	else:
		col_time = col_dirt - col_bot
	print(min(row_time, col_time))


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = [int(x) for x in input().split()]
	countr = 0
	countc = 0
	if rd >= rb:
		countr = rd - rb
	else:
		countr = n - rb + (n - rd)
	if cd >= cb:
		countc = cd - cb
	else:
		countc = m - cb + (m - cd)
	print(min(countr, countc))


for _ in range(int(input())):
	(n, m, a, b, c, d) = map(int, input().split())
	if a <= c:
		ansr = c - a
	else:
		ansr = n - a + n - c
	if b <= d:
		ansc = d - b
	else:
		ansc = m - b + m - d
	print(min(ansr, ansc))


from __future__ import print_function
from os import environ
import sys
import math
from bisect import bisect_left
if environ.get('epoweripi'):
	sys.stdin = open('../../input.txt', 'r')
	sys.stdout = open('../../output.txt', 'w')
input = sys.stdin.readline
mod = 10 ** 9 + 7

def I():
	return input().strip()

def II():
	return int(input().strip())

def MI():
	return map(int, input().split())

def LI():
	return list(map(int, input().split()))
if sys.version[0] == '3':
	print = sys.stdout.write

	def P(z=''):
		print(str(z) + '\n')

	def LP(z=[]):
		for i in z:
			print(str(i) + ' ')
		print('\n')
else:

	def P(z=''):
		print(str(z))

	def LP(z=[]):
		for i in z:
			print(str(i) + ' ', end='')
		print()

def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def sieve(n):
	isPrimes = [True for i in range(n + 1)]
	(isPrimes[0], isPrimes[1]) = (False, False)
	p = 2
	while p * p <= n:
		if isPrimes[p] == True:
			for j in range(p * p, n + 1, p):
				isPrimes[j] = False
		p += 1

def swap(arr, i, j):
	(arr[i], arr[j]) = (arr[j], arr[i])

def solve():
	(n, m, pos_r, pos_c, rd, cd) = MI()
	count = 0
	dr = 1
	dc = 1
	while pos_r != rd and pos_c != cd:
		if pos_r == n or pos_r == 0:
			dr = -dr
		if pos_c == m or pos_c == 0:
			dc = -dc
		pos_r += dr
		pos_c += dc
		count += 1
	P(count)

def main():
	for _ in range(II()):
		solve()
main()


count = int(input())
n = [0] * count
m = [0] * count
r = [0] * count
c = [0] * count
r_1 = [0] * count
c_1 = [0] * count
for i in range(count):
	(n[i], m[i], r[i], c[i], r_1[i], c_1[i]) = map(int, input().split())

def f(n, m, r, c, r_1, c_1):
	t = 0
	dr = 1
	dc = 1
	while r != r_1 and c != c_1:
		if 1 > r + dr or r + dr > n:
			dr = -1
		if 1 > c + dc or c + dc > m:
			dc = -1
		r = r + dr
		c = c + dc
		t += 1
	return t
for i in range(count):
	print(f(n[i], m[i], r[i], c[i], r_1[i], c_1[i]))


import sys, bisect, math
from collections import deque, Counter
input = lambda : sys.stdin.readline().strip()
ceil = lambda x: math.ceil(x)
mod = 10 ** 9 + 7

def solve():
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	cnt = 0
	(r, c) = (1, 1)
	while rb != rd and cb != cd:
		if rb == n:
			r = -1
		if cb == m:
			c = -1
		rb += r
		cb += c
		cnt += 1
	return cnt
t = 1
t = int(input())
for _ in range(t):
	print(solve())


def main():
	for i in range(int(input())):
		print(f'{solve(*parse())}')

def parse():
	(n, m, rb, cb, rd, cd) = input().split()
	return (int(n), int(m), int(rb), int(cb), int(rd), int(cd))

def solve(n, m, rb, cb, rd, cd):
	dr = 1
	dc = 1
	c = 0
	moves = 0
	if rb == rd or cb == cd:
		c = 1
	if rb == n:
		dr = dr * -1
	if cb == m:
		dc = dc * -1
	while c == 0:
		moves += 1
		rb += dr
		cb += dc
		if rb == n or rb == 1:
			dr = dr * -1
		if cb == m or cb == 1:
			dc = dc * -1
		if rb == rd:
			c = 1
		if cb == cd:
			c = 1
	return moves
main()


import sys
from math import ceil, log2
input = sys.stdin.readline

def solve():
	(n, m, r, c, rr, cc) = map(int, input().split())
	(dr, dc) = (1, 1)
	cnt = 0
	while not (r == rr or c == cc):
		if r + dr <= 0 or r + dr > n:
			dr = -dr
		if c + dc <= 0 or c + dc > m:
			dc = -dc
		r += dr
		c += dc
		cnt += 1
	return cnt
t = int(input())
for i in range(t):
	print(solve())


a = int(input())
for i in range(a):
	b = [int(j) for j in input().split()]
	if b[2] == b[4] or b[3] == b[5]:
		print('0')
	else:
		t1 = 0
		t2 = 0
		if b[2] > b[4]:
			t1 = b[0] - b[2] + (b[0] - b[4])
		else:
			t1 = b[4] - b[2]
		if b[3] > b[5]:
			t2 = b[1] - b[3] + (b[1] - b[5])
		else:
			t2 = b[5] - b[3]
		print(min(t1, t2))


for i in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb == rd or cb == cd:
		print(0)
	else:
		if rb < rd:
			maxrow = rd - rb
		else:
			maxrow = (n - rb) * 2 + (rb - rd)
		if cb < cd:
			maxcol = cd - cb
		else:
			maxcol = (m - cb) * 2 + (cb - cd)
		print(min(maxrow, maxcol))


for iii in range(int(input())):
	(n, m, x1, y1, x2, y2) = map(int, input().split())
	x = 0
	if x2 >= x1:
		x = x2 - x1
	else:
		x = 2 * (n - x1) + x1 - x2
	y = 0
	if y2 >= y1:
		y = y2 - y1
	else:
		y = 2 * (m - y1) + y1 - y2
	print(min(x, y))


def clean(n, m, xbot, ybot, xdirt, ydirt, count, dirx, diry):
	if xbot == xdirt or ybot == ydirt:
		print(count)
		return
	if xbot + dirx > n or xbot + dirx < 1:
		dirx = -1 * dirx
	if ybot + diry > m or ybot + diry < 1:
		diry = -1 * diry
	clean(n, m, xbot + dirx, ybot + diry, xdirt, ydirt, count + 1, dirx, diry)

def main():
	t = int(input())
	for i in range(t):
		testcase = input().split(' ')
		clean(int(testcase[0]), int(testcase[1]), int(testcase[2]), int(testcase[3]), int(testcase[4]), int(testcase[5]), 0, 1, 1)
main()


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	t = float('inf')
	if rb <= rd:
		t = min(t, rd - rb)
	else:
		t = min(t, n - rd + (n - rb))
	if cb <= cd:
		t = min(t, cd - cb)
	else:
		t = min(t, m - cd + (m - cb))
	print(t)


def rc(r, c, rb, cb, rd, cd):
	rr = rb
	cc = cb
	tt = 0
	dr = 1
	dc = 1
	while (cc != cd) & (rr != rd):
		if rr == r:
			dr = -1
		if cc == c:
			dc = -1
		cc = cc + dc
		rr = rr + dr
		tt = tt + 1
	return tt
t = int(input())
ls = []
for i in range(t):
	s = str(input())
	ls.append(s)
la = []
for s in ls:
	lt = s.split(' ')
	r = int(lt[0])
	c = int(lt[1])
	rb = int(lt[2])
	cb = int(lt[3])
	rd = int(lt[4])
	cd = int(lt[5])
	ans = rc(r, c, rb, cb, rd, cd)
	la.append(ans)
for x in la:
	print(x)


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def I():
	return int(input())

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
import itertools
from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from math import factorial, inf
from itertools import groupby
from itertools import permutations as comb
mod = 1000000007
MOD = 998244353

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a, b, c):
	print(c, a, b, flush=True)
	n = I()
	return n

def linear_sieve(n):
	is_composite = [False] * n
	prime = []
	for i in range(2, n):
		if not is_composite[i]:
			prime.append(i)
		for j in prime:
			is_composite[i * j] = True
			if i % prime == 0:
				break
	return prime

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l

def binpow(a, b, c):
	res = 1
	a = a % c
	while b > 0:
		if b & 1:
			res = res * a % c
		b = b >> 1
		a = a * a % c
	return res
d = []
primes = []
prim = [0] * (10 ** 5 + 1)

def sieve(n):
	for i in range(n):
		d.append(i)
	for i in range(2, n):
		if d[i] == i:
			prim[i] = 1
			primes.append(i)
		j = 0
		while j < len(primes) and primes[j] <= d[i] and (primes[j] * i < n):
			d[i * primes[j]] = primes[j]
			j += 1

def primeFactors(n):
	factors = []
	while n != 1:
		factors.append(d[n])
		n //= d[n]
	return factors
t = 1
t = I()
for _ in range(t):
	(a, b, c, d, e, f) = lint()
	ans = 100000001
	if d <= f:
		ans = min(ans, f - d)
	else:
		ans = min(ans, 2 * b - f - d)
	if c <= e:
		ans = min(ans, e - c)
	else:
		ans = min(ans, 2 * a - e - c)
	print(ans)


import sys
from math import sqrt, gcd
from collections import deque
sys.setrecursionlimit(10 ** 8)
I = lambda : int(input())
S = lambda : input().strip()
M = lambda : map(int, input().strip().split())
L = lambda : list(map(int, input().strip().split()))
mod = 1000000007
for i in range(I()):
	(n, m, rb, cb, rd, cd) = M()
	if rb == rd or cb == cd:
		print(0)
		continue
	if rb > rd:
		if cb > cd:
			print(min(2 * n - rb - rd, 2 * m - cb - cd))
		else:
			print(min(2 * n - rb - rd, cd - cb))
	elif cb > cd:
		print(min(rd - rb, 2 * m - cb - cd))
	else:
		print(min(rd - rb, cd - cb))


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	move = [1, 1]
	time = 0
	while rd != rb and cd != cb:
		if rb == n:
			move[0] = -1
		if cb == m:
			move[1] = -1
		rb += move[0]
		cb += move[1]
		time += 1
	print(time)


for _ in range(int(input())):
	ct = 0
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	dx = 1
	dy = 1
	while rb != rd and cb != cd:
		ct += 1
		if rb + 1 > n:
			dx = -1
		if cb + 1 > m:
			dy = -1
		rb = rb + dx
		cb = cb + dy
	print(ct)


t = int(input())
for _ in range(t):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	(stepI, stepJ) = (1, 1)
	time = 0
	while rb != rd and cb != cd:
		if rb == n and stepI == 1 or (rb == 1 and stepI == -1):
			stepI = 0 - stepI
		if cb == m and stepJ == 1 or (cb == 1 and stepJ == -1):
			stepJ = 0 - stepJ
		rb += stepI
		cb += stepJ
		time += 1
	print(time)


import math
t = int(input())
for test in range(t):
	(n, m, xi, yi, xd, yd) = map(int, input().split())
	op1 = xd - xi
	op2 = yd - yi
	if op1 < 0:
		op1 = abs(n - xi) + abs(n - xd)
	if op2 < 0:
		op2 = abs(m - yi) + abs(m - yd)
	print(min(op1, op2))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = map(int, input().split())
	if rb > rd:
		ret = n - rb + n - rd
	else:
		ret = rd - rb
	if cb > cd:
		ret = min(ret, m - cb + m - cd)
	else:
		ret = min(ret, cd - cb)
	print(ret)


t = int(input())
test_case = []
for i in range(t):
	test = map(int, input().split())
	test_case.append(test)
for test in test_case:
	(n, m, rb, cb, rd, cd) = test
	if rd > rb and cd > cb:
		print(min((rd - rb, cd - cb)))
	elif rd < rb and cd > cb:
		print(min(rb - rd + 2 * (n - rb), cd - cb))
	elif rd < rb and cd < cb:
		print(min(rb - rd + 2 * (n - rb), cb - cd + 2 * (m - cb)))
	elif rd > rb and cd < cb:
		print(min(rd - rb, cb - cd + 2 * (m - cb)))
	else:
		print(0)


for i in range(int(input())):
	(n, m, rb, cb, rd, cd) = tuple(map(int, input().split()))
	if rb == rd or cb == cd:
		print(0)
	elif rb < rd and cb < cd:
		print(min(rd - rb, cd - cb))
	elif rb > rd and cb > cd:
		a = min(n - rb + (n - rd), m - cb + (m - cd))
		print(a)
	elif rb > rd and cb < cd:
		print(min(n - rb + (n - rd), cd - cb))
	elif rb < rd and cb > cd:
		print(min(rd - rb, m - cb + (m - cd)))


cases = int(input())
for i in range(cases):
	(n, m, r_b, c_b, r_d, c_d) = [int(x) for x in input().split()]
	x = 1
	y = 1
	count = 0
	while True:
		if r_b == r_d or c_b == c_d:
			break
		if r_b == n and y == 1:
			y = -1
		if c_b == m and x == 1:
			x = -1
		if r_b == 1 and y == -1:
			y = 1
		if c_b == 1 and x == -1:
			x = 1
		r_b += y
		c_b += x
		count += 1
	print(count)


import sys, os, math, cmath, timeit, functools, operator, bisect
from sys import stdin, stdout, setrecursionlimit
from io import BytesIO, IOBase
from collections import defaultdict, Counter
from math import *
from heapq import merge, heapify, heappop, heappush
ONLINE_JUDGE = __debug__

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not a:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			a = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = a.count(b'\n') + (not a)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(a), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
input = lambda : stdin.readline().rstrip('\r\n')
start = timeit.default_timer()
primes = []
mod = 10 ** 9 + 7

def power(x, b):
	p = 1
	while b > 0:
		if b & 1:
			p = p * x
			p = p % mod
		b = b >> 1
		x = x ** x
		x = x % mod
	return p % mod

def seive(n):
	a = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			primes.append(p)

def is_sorted(arr):
	return all((arr[i] <= arr[i + 1] for i in range(len(arr) - 1)))

def invmod(n):
	return power(n, mod - 2)

def binary_search(a, x, lo=0, hi=None):
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		midval = a[mid]
		if midval < x:
			lo = mid + 1
		elif midval > x:
			hi = mid
		else:
			return mid
	return -1

def yn(x):
	if x:
		stdout.write('YES' + '\n')
	else:
		stdout.write('NO' + '\n')

def nCr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def solve():
	(n, m, rb, cb, rd, cd) = rowinp()
	(dr, dc) = (1, 1)
	moves = 0
	while rb != rd and cb != cd:
		if rb == n:
			dr = -1
		if cb == m:
			dc = -1
		if rb == 0:
			dr = 1
		if cd == 0:
			dc = 1
		rb += dr
		cb += dc
		moves += 1
	print(moves)

def main():
	tc = 1
	tc = int(input())
	for _ in range(tc):
		solve()
	stop = timeit.default_timer()
	if not ONLINE_JUDGE:
		stdout.write('Time Elapsed : ' + str(stop - start) + ' seconds')
	stdout.close()

def lst() -> list:
	return list(map(int, input().split()))

def rowinp():
	return map(int, input().split())

def strin() -> str:
	return input()

def chlst() -> list:
	return [x for x in input()]

def sint() -> int:
	return int(input())
main()


t = int(input())
for _ in range(t):
	(n, m, r1, c1, r2, c2) = map(int, input().split())
	(row, col) = (0, 0)
	if r1 == r2 or c1 == c2:
		print(0)
		continue
	if r1 > r2:
		row = abs(r1 - n) * 2 + abs(r1 - r2)
	else:
		row = r2 - r1
	if c1 > c2:
		col = abs(c1 - m) * 2 + abs(c1 - c2)
	else:
		col = c2 - c1
	print(min(row, col))


for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split()))
	row = 1
	col = 1
	ans = 0
	while rb != rd and cb != cd:
		if rb == n:
			row = -1
		if cb == m:
			col = -1
		(rb, cb) = (rb + row, cb + col)
		ans += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m, rb, cb, rd, cd) = list(map(int, input().split(' ')))
	nr = rd - rb if rd >= rb else 2 * (n - rb) + rb - rd
	nc = cd - cb if cd >= cb else 2 * (m - cb) + cb - cd
	print(nr if nr < nc else nc)


class io:
	s = input
	i = lambda : int(io.s())
	f = lambda : float(io.s())
	ts = lambda : io.s().split()
	ti = lambda : map(int, io.ts())
	tf = lambda : map(float, io.ts())
	ls = lambda : list(io.ts())
	li = lambda : list(io.ti())
	lf = lambda : list(io.tf())
for _ in range(int(input())):
	(n, m, rb, cb, rd, cd) = io.li()
	cnt = 0
	dr = dc = 1
	while rb != rd and cb != cd:
		if rb + dr < 1 or rb + dr > n:
			dr = -dr
		if cb + dc < 1 or cb + dc > m:
			dc = -dc
		rb += dr
		cb += dc
		cnt += 1
	print(cnt)


from sys import stdout, stdin
from io import IOBase, BytesIO
from os import read, supports_follow_symlinks, write, fstat
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size: int=...):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))

def input():
	return stdin.readline().rstrip('\r\n')

def one_line(size, robot, dirt):
	if robot == dirt:
		return 0
	if robot < dirt:
		return dirt - robot
	return (size - robot) * 2 + (robot - dirt)

def main():
	t = int(input())
	for _ in ' ' * t:
		(n, m, rb, cb, rd, cd) = map(int, input().split())
		ans1 = one_line(n, rb, rd)
		ans2 = one_line(m, cb, cd)
		print(min(ans1, ans2))
main()


t = int(input())
for _ in range(t):
	(n, m, rx, ry, dx, dy) = map(int, input().split())
	ans = 0
	i = rx
	j = ry
	mi = 1
	mj = 1
	while i != dx and j != dy:
		if i + 1 > n:
			mi = 0
		if i - 1 < 1:
			mi = 1
		if j + 1 > m:
			mj = 0
		if j - 1 < 1:
			mj = 1
		if mi:
			i = i + 1
		else:
			i = i - 1
		if mj:
			j = j + 1
		else:
			j = j - 1
		ans = ans + 1
	print(ans)


from sys import stdin

def inpu():
	return stdin.readline()
for t in range(int(inpu())):
	(ro, col, rb, cb, rd, cd) = map(int, inpu().split())
	c1 = 0
	if rb > rd:
		c1 = ro - rb + (ro - rd)
	else:
		c1 = rd - rb
	c2 = 0
	if cb > cd:
		c2 = col - cb + (col - cd)
	else:
		c2 = cd - cb
	print(min(c1, c2))