You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$.

Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$.

In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$.

Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$.

What is the minimum number of steps you need to make $I$ greater or equal to $k$?


-----Input-----

The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection.

The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially.

The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially.

It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$.


-----Output-----

Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$.


-----Example-----
Input
3
3 5
1 2
3 4
2 1000000000
1 1
999999999 999999999
10 3
5 10
7 8

Output
7
2000000000
0



-----Note-----

In the first test case, we can achieve total intersection $5$, for example, using next strategy:   make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps;  make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step;  make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps;  make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps.  In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$

In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps.

In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.

Difficulty: HARD
Tags: Mathematics

Complete search

Implementation

Greedy algorithms
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
for tc in range(int(input())):
	(n, k) = input().split(' ')
	(al, ar) = input().split(' ')
	(bl, br) = input().split(' ')
	(n, k, al, ar, bl, br) = (int(n), int(k), int(al), int(ar), int(bl), int(br))
	if al > bl:
		(al, ar, bl, br) = (bl, br, al, ar)
	if ar < bl:
		blank = bl - ar
		get = br - al
		need = blank + get
		if k < get:
			print(blank + k)
		elif k <= n * get:
			print(int(k / get) * need + min(k % get, blank) + k % get)
		else:
			print(n * need + (k - n * get) * 2)
	else:
		get = max(br, ar) - al
		init = n * (min(br, ar) - bl)
		if k <= init:
			print(0)
		elif k <= n * get:
			print(k - init)
		else:
			print(n * get - init + 2 * (k - n * get))


import sys
import math
read = sys.stdin.buffer.read
readlines = sys.stdin.buffer.readlines
t = int(input())
for case in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	origin_inter = max(0, min(r1, r2) - max(l1, l2))
	ans = 0
	k -= origin_inter * n
	if k <= 0:
		ans = 0
	elif origin_inter > 0:
		if k <= n * (r1 - l1 + (r2 - l2) - origin_inter * 2):
			ans += k
		else:
			ans = 2 * k - n * (r1 - l1 + (r2 - l2) - origin_inter * 2)
	else:
		margin = max(l1, l2) - min(r1, r2)
		len_inter = max(r1, r2) - min(l1, l2)
		ans = 10 ** 10
		for i in range(1, n + 1):
			tmp_i = margin * i
			if k > len_inter * i:
				tmp_i += len_inter * i + (k - len_inter * i) * 2
			else:
				tmp_i += k
			ans = min(tmp_i, ans)
	print(ans)


def profit(l1, r1, l2, r2):
	if r1 > r2:
		(r1, r2) = (r2, r1)
		(l1, l2) = (l2, l1)
	if l2 > r1:
		return r2 - l1
	elif l2 > l1:
		return r2 - l1 - r1 + l2
	else:
		return r2 - r1 + l1 - l2

def tax(l1, r1, l2, r2):
	if r1 > r2:
		(r1, r2) = (r2, r1)
		(l1, l2) = (l2, l1)
	if l2 > r1:
		return l2 - r1
	else:
		return 0

def overlap(l1, r1, l2, r2):
	if r1 > r2:
		(r1, r2) = (r2, r1)
		(l1, l2) = (l2, l1)
	if l2 > r1:
		return 0
	elif l2 > l1:
		return r1 - l2
	else:
		return r1 - l1
tt = int(input())
while tt:
	tt -= 1
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	s = 0
	p = profit(l1, r1, l2, r2)
	t = tax(l1, r1, l2, r2)
	o = overlap(l1, r1, l2, r2)
	k -= n * o
	if k <= 0:
		print(s)
		continue
	s += p + t
	k -= p
	n -= 1
	if k <= 0:
		print(s + k)
		continue
	if k - p * n > 0:
		s += (p + t) * n
		s += 2 * (k - p * n)
		print(s)
		continue
	n = k // p
	if k - n * p > t:
		s += (p + t) * n
		s += k - n * p + t
		print(s)
		continue
	s += (p + t) * n
	s += 2 * (k - n * p)
	print(s)


import io
import os
from collections import Counter, defaultdict, deque
from math import inf

def intersect(int1, int2):
	l = max(int1[0], int2[0])
	r = min(int1[1], int2[1])
	return (l, r)

def union(int1, int2):
	l = min(int1[0], int2[0])
	r = max(int1[1], int2[1])
	return (l, r)

def intervalLen(interval):
	(l, r) = interval
	return r - l

def minimize(f, lo, hi):
	assert lo <= hi
	while hi - lo >= 3:
		mid1 = lo + (hi - lo) // 3
		mid2 = hi - (hi - lo) // 3
		if False:
			indices = [lo, mid1, mid2, hi]
			values = [f(x) for x in indices]
			deltas = [y - x for (x, y) in zip(values, values[1:])]
			print(indices)
			print(values)
			print(['>' if 0 > d else '<' if 0 < d else '=' for d in deltas])
			print()
		delta = f(mid2) - f(mid1)
		if delta < 0:
			lo = mid1
		else:
			assert delta >= 0
			hi = mid2
	return min((f(x) for x in range(lo, hi + 1)))

def solve(N, K, LR1, LR2):
	if N == 0:
		return inf
	dist = 0
	if LR1[1] < LR2[0]:
		dist = LR2[0] - LR1[1]
	elif LR2[1] < LR1[0]:
		(LR1, LR2) = (LR2, LR1)
		dist = LR2[0] - LR1[1]
	assert dist >= 0
	if dist == 0:
		iLen = intervalLen(intersect(LR1, LR2))
		if iLen * N >= K:
			return 0
		K -= iLen * N
		uLen = intervalLen(union(LR1, LR2))
		if (uLen - iLen) * N >= K:
			return K
		moves = (uLen - iLen) * N
		K -= moves
		moves += K * 2
		return moves
	else:

		def cost(numTouch):
			return numTouch * dist + solve(numTouch, K, [LR1[0], LR1[1] + dist], LR2)
		return minimize(cost, 0, N)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, K) = [int(x) for x in input().split()]
	LR1 = [int(x) for x in input().split()]
	LR2 = [int(x) for x in input().split()]
	ans = solve(N, K, LR1, LR2)
	print(ans)


import math

def solve(n, k, l1, r1, l2, r2):
	if l1 > l2:
		(l1, l2) = (l2, l1)
		(r1, r2) = (r2, r1)
	k -= n * max(min(r1, r2) - l2, 0)
	if k <= 0:
		return 0
	le_dif = l2 - l1
	ri_dif = max(r1, r2) - min(r1, r2)
	to_join = 0 if r1 >= l2 else l2 - r1
	eq_len_intersection = le_dif + ri_dif - to_join
	if eq_len_intersection >= k:
		return to_join + k
	elif eq_len_intersection + to_join < 2 * eq_len_intersection:
		req = math.ceil(k / eq_len_intersection)
		if req <= n:
			lo_lim = k // eq_len_intersection
			res = (eq_len_intersection + to_join) * lo_lim
			rem = k - eq_len_intersection * lo_lim
			if rem > 0:
				res += min(to_join + rem, 2 * rem)
			return res
		else:
			return (eq_len_intersection + to_join) * n + 2 * (k - eq_len_intersection * n)
	else:
		return to_join + eq_len_intersection + 2 * (k - eq_len_intersection)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	print(solve(n, k, l1, r1, l2, r2))


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]
INF = 10 ** 20

def solve(N, K, L1, R1, L2, R2):
	if L2 < L1:
		(L1, L2) = (L2, L1)
		(R1, R2) = (R2, R1)
	if R1 <= L2:
		min_cost = INF
		d = L2 - R1
		A = R2 - L1
		for i in range(1, N + 1):
			cost = d * i
			if K <= A * i:
				cost += K
			else:
				cost += A * i + 2 * (K - A * i)
			min_cost = min(min_cost, cost)
		return min_cost
	elif L2 < R1 < R2:
		c = R1 - L2
		K -= N * c
		if K <= 0:
			return 0
		else:
			A = R2 - L1 - c
			if K <= A * N:
				return K
			else:
				return A * N + 2 * (K - A * N)
	else:
		c = R2 - L2
		K -= N * c
		if K <= 0:
			return 0
		else:
			A = R1 - L1 - c
			if K <= A * N:
				return K
			else:
				return A * N + 2 * (K - A * N)
T0 = int(input())
for _ in range(T0):
	(N, K) = map(int, input().split())
	(L1, R1) = map(int, input().split())
	(L2, R2) = map(int, input().split())
	print(solve(N, K, L1, R1, L2, R2))


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

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

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	(n, k) = mdata()
	(l1, r1) = mdata()
	(l2, r2) = mdata()
	if l1 > l2:
		(l1, l2, r1, r2) = (l2, l1, r2, r1)
	d = max(0, l2 - r1)
	c = max(0, min(r1, r2) - l2)
	ans = 0
	k -= c * n
	if k > 0:
		if max(r1, r2) - l1 - c == 0:
			ans += 2 * k
		else:
			a = k // (max(r1, r2) - l1 - c)
			if a == 0:
				ans += d + k
			elif a < n:
				ans += (d + max(r1, r2) - l1 - c) * a
				a = k % (max(r1, r2) - l1 - c)
				ans += min(2 * a, d + a)
			else:
				ans += (d + max(r1, r2) - l1 - c) * n
				ans += (k - n * (max(r1, r2) - l1 - c)) * 2
	out(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	if d < b:
		(a, b, c, d) = (c, d, a, b)
	if a <= c <= b:
		cur = (b - c) * n
		print(max(k - cur, 0)) if (d - a) * n > k else print((d - a) * n - cur + (k - (d - a) * n) * 2)
	elif c < a:
		cur = (b - a) * n
		print(max(k - cur, 0)) if (d - c) * n > k else print((d - c) * n - cur + (k - (d - c) * n) * 2)
	else:
		(ans, cur) = (10 ** 18, 0)
		for i in range(n):
			cur += c - b
			if k > d - a:
				k -= d - a
				cur += d - a
			else:
				cur += k
				k -= k
			ans = min(ans, cur + k * 2)
		print(ans)


import sys
import array
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l2 < l1:
		(l1, l2) = (l2, l1)
		(r1, r2) = (r2, r1)
	ans = 10 ** 18
	pre = 0
	in_c = 0
	already = 0
	if r1 <= l2:
		pre = l2 - r1
		in_c = r2 - l1
	elif r2 <= r1:
		already = (r2 - l2) * n
		pre = 0
		in_c = r1 - r2 + l2 - l1
	else:
		already = (r1 - l2) * n
		pre = 0
		in_c = r2 - r1 + l2 - l1
	if already >= k:
		print(0)
		continue
	dx = k - already
	if pre == 0:
		need = 0 if in_c == 0 else dx // in_c
		if in_c * n < dx:
			score = in_c * n + (dx - in_c * n) * 2
		else:
			score = dx
		print(score)
	else:
		score = 10 ** 18
		for i in range(1, n + 1):
			cnt = 0
			cnt += pre * i
			r = k - min(in_c * i, k)
			cnt += min(in_c * i, k)
			cnt += 2 * r
			score = min(score, cnt)
		print(score)


import sys
input = sys.stdin.readline

def intersection(a, b):
	if a[0] >= b[0] and a[1] <= b[1]:
		return -(a[1] - a[0])
	if b[0] > a[1]:
		return b[0] - a[1]
	elif b[1] < a[0]:
		return a[0] - b[1]
	if a[1] < b[1]:
		return b[0] - a[1]
	return a[0] - b[1]

def check(a, b):
	diff = intersection(a, b)
	if diff < 0:
		if abs(diff) * n >= k:
			return True
	return False
for nt in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if r2 - l2 < r1 - l1:
		minn = (l2, r2)
		maxx = (l1, r1)
	else:
		minn = (l1, r1)
		maxx = (l2, r2)
	if check(minn, maxx):
		print(0)
		continue
	diff = intersection(minn, maxx)
	one = abs(minn[0] - maxx[0]) + abs(minn[1] - maxx[1])
	r = (min(minn[0], maxx[0]), max(minn[1], maxx[1]))
	if diff < 0:
		k -= abs(diff) * n
		done = r[1] - r[0] + diff
	else:
		done = r[1] - r[0]
	if one >= 2 * done and (done != 0 and k // done != 0):
		print(max(0, one + 2 * (k - done)))
		continue
	if done * n <= k:
		print(max(0, one * n + 2 * (k - done * n)))
		continue
	left = k % done
	ans = k // done * one
	if diff < 0:
		print(ans + left)
	elif ans != 0:
		print(min(ans + 2 * left, ans + diff + left))
	else:
		print(ans + diff + left)


for tt in range(int(input())):
	(n, z) = input().split(' ')
	(Al, Ar) = input().split(' ')
	(Bl, Br) = input().split(' ')
	(n, z, Al, Ar, Bl, Br) = (int(n), int(z), int(Al), int(Ar), int(Bl), int(Br))
	if Al > Bl:
		(Al, Ar, Bl, Br) = (Bl, Br, Al, Ar)
	if Ar < Bl:
		blank = Bl - Ar
		get = Br - Al
		need = blank + get
		if z < get:
			print(blank + z)
		elif z <= n * get:
			print(int(z / get) * need + min(z % get, blank) + z % get)
		else:
			print(n * need + (z - n * get) * 2)
	else:
		get = max(Br, Ar) - Al
		init = n * (min(Br, Ar) - Bl)
		if z <= init:
			print(0)
		elif z <= n * get:
			print(z - init)
		else:
			print(n * get - init + 2 * (z - n * get))


for i in ' ' * int(input()):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	meeting = 0
	if r1 > l2:
		meeting = (min(r1, r2) - l2) * n
	if meeting >= k:
		print(0)
		continue
	k -= meeting
	tot = max(r1, r2) - min(l1, l2) - meeting // n
	mxtot = tot * n
	distformeet = 0
	if r1 < l2:
		distformeet = l2 - r1
	if mxtot >= k:
		q = (k - distformeet) // tot
		if (k - distformeet) % tot:
			q += 1
		q = min(n, max(q, 1))
		if q * tot >= k:
			print(q * distformeet + k)
		else:
			print(2 * k - q * tot + q * distformeet)
	else:
		print(n * distformeet + mxtot + (k - mxtot) * 2)


from sys import stdin
from collections import defaultdict
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().rstrip().split(' ')))
	(l1, r1) = list(map(int, input().rstrip().split(' ')))
	(l2, r2) = list(map(int, input().rstrip().split(' ')))
	overLapCost = max(l1, l2) - min(l1, l2) + max(r1, r2) - min(r1, r2)
	overLapLength = max(r1, r2) - min(l1, l2)
	extraCost = max(l1, l2) - min(r1, r2)
	if extraCost < 0:
		overLapLength += extraCost
		k += extraCost * n
	if k < 0:
		print(0)
		continue
	extraCost = max(extraCost, 0)
	cost = 0
	if overLapLength > 0:
		fullNeeded = k // overLapLength
		k -= overLapLength * min(fullNeeded, n)
		cost += overLapCost * min(fullNeeded, n)
	else:
		fullNeeded = 10000000000.0
	if fullNeeded >= n:
		cost += 2 * k
	elif fullNeeded > 0:
		cost += min(2 * k, extraCost + k)
	else:
		cost += extraCost + k
	print(cost)


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if r1 < l2 or r2 < l1:
		gap = max(l2 - r1, l1 - r2)
		ans = 10 ** 18
		for i in range(1, n + 1):
			cand = i * gap
			plus1 = (max(r1, r2) - min(l1, l2)) * i
			if plus1 >= k:
				cand += k
			else:
				cand += plus1 + (k - plus1) * 2
			ans = min(ans, cand)
		print(ans)
	else:
		inter = min(r1, r2) - max(l1, l2)
		now = n * inter
		plus1 = (max(r1, r2) - min(l1, l2) - inter) * n
		if now >= k:
			print(0)
		elif now + plus1 >= k:
			print(k - now)
		else:
			print(plus1 + (k - now - plus1) * 2)


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
sys.setrecursionlimit(3 * 10 ** 6)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def ri():
	return int(f.readline().strip())

def ria():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def rs():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k)))) % NUMBER
from collections import deque, defaultdict
import heapq
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

def dfs(g, timeIn, timeOut, depths, parents):
	cnt = 0
	stack = [[1, 0, 0, 0]]
	while stack:
		(v, neig_i, parent, depth) = stack[-1]
		parents[v] = parent
		depths[v] = depth
		if neig_i == 0:
			timeIn[v] = cnt
			cnt += 1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i += 1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u, 0, v, depth + 1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
cnt = 0

@bootstrap
def dfs(v, adj, timeIn, timeOut, depths, parents, parent=0, depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt += 1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u, adj, timeIn, timeOut, depths, parents, v, depth + 1)
	timeOut[v] = cnt
	cnt += 1
	yield

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

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

def get_num_2_5(n):
	twos = 0
	fives = 0
	while n > 0 and n % 2 == 0:
		n //= 2
		twos += 1
	while n > 0 and n % 5 == 0:
		n //= 5
		fives += 1
	return (twos, fives)

def shift(a, i, num):
	for _ in range(num):
		(a[i], a[i + 1], a[i + 2]) = (a[i + 2], a[i], a[i + 1])

def equal(x, y):
	return abs(x - y) <= 1e-09

def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
	return ang + 360 if ang < 0 else ang

def getLength(a, b):
	return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0, 0))
	while len(q) > 0:
		(v, depth) = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth + 1].add(u)
				q.append((u, depth + 1))
	return d

def isSubsetSum(arr, n, summ):
	subarr = [[False for i in range(summ + 1)] for i in range(n + 1)]
	for i in range(n + 1):
		subarr[i][0] = True
	for i in range(1, summ + 1):
		subarr[0][i] = False
	for i in range(1, n + 1):
		for j in range(1, summ + 1):
			if j < arr[i - 1]:
				subarr[i][j] = subarr[i - 1][j]
			if j >= arr[i - 1]:
				subarr[i][j] = subarr[i - 1][j] or subarr[i - 1][j - arr[i - 1]]
	return subarr[n][summ]

def bfs(v, adj, onpath):
	s = set()
	d = defaultdict(set)
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0, 0))
	while len(q) > 0:
		(v, depth) = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth + 1].add(u)
				q.append((u, depth + 1))
	return d

@bootstrap
def dfs(v, p, adj, b, path):
	if v == b:
		path.append(b)
		yield True
	for u in adj[v]:
		if u == p:
			continue
		on = (yield dfs(u, v, adj, b, path))
		if on:
			path.append(v)
			yield True
	yield False

@bootstrap
def dfs1(v, p, adj, d, mcd, onpath, c, root):
	if len(mcd) < d + 1:
		mcd.append([c[v], d, root, v])
	if mcd[d][0] > c[v]:
		mcd[d][0] = c[v]
		mcd[d][3] = v
	for u in adj[v]:
		if u == p or onpath[u]:
			continue
		yield dfs1(u, v, adj, d + 1, mcd, onpath, c, root)
	yield False

def solution(n, k, l1, r1, l2, r2):
	if l2 < l1:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l2 > r1:
		cur_inter = 0
	else:
		cur_inter = (min(r2, r1) - max(l1, l2)) * n
	if cur_inter >= k:
		return 0
	if cur_inter > 0 or (l2 == r2 and r2 <= r1):
		cnt = 0
		if (max(r2, r1) - l1) * n >= k:
			return k - cur_inter
		cur_inter = (max(r2, r1) - l1) * n
		cnt += (max(r2, r1) - min(r1, r2) + l2 - l1) * n
		left = k - cur_inter
		return cnt + left * 2
	if cur_inter == 0:
		cnt = 0
		left = k
		if r2 - l1 >= left:
			return k + (l2 - r1)
		cnt += r2 - r1 + l2 - l1
		cur_inter += r2 - l1
		if k <= (r2 - l1) * n:
			for i in range(n - 1):
				left = k - cur_inter
				if cur_inter + (r2 - l1) >= k:
					if left <= l2 - r1:
						return cnt + 2 * left
					return cnt + (k - cur_inter) + (l2 - r1)
				cur_inter += r2 - l1
				cnt += r2 - r1 + l2 - l1
		cur_inter = (r2 - l1) * n
		cnt = n * (r2 - r1) + n * (l2 - l1)
		left = k - cur_inter
		return cnt + left * 2

def main():
	T = 1
	T = ri()
	for i in range(T):
		(n, k) = ria()
		(l1, r1) = ria()
		(l2, r2) = ria()
		x = solution(n, k, l1, r1, l2, r2)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


def solve():
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	gap = l2 - r1
	if gap >= 0:
		u = r1 - l1
		v = r2 - l2
		if u > v:
			(u, v) = (v, u)
		if k <= 0:
			print(0)
			return
		step = gap
		cur = 0
		mx = max(r2, r1) - min(l1, l2)
		ans = gap + 2 * k
		for i in range(n):
			if cur + mx >= k:
				ans = min(ans, step + max(0, k - cur))
			step += mx
			cur += mx
			ans = min(ans, step + max(k - cur, 0) * 2)
			step += gap
		ans = min(ans, step + max(k - cur, 0) * 2)
		print(ans)
	else:
		mx = (max(r2, r1) - min(l1, l2)) * n
		mn = (min(r1, r2) - max(l1, l2)) * n
		if k <= mx:
			print(max(0, k - mn))
			return
		else:
			print((k - mx) * 2 + (mx - mn))
			return
t = int(input())
for _ in range(t):
	solve()


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	(n, k) = map(int, inputi().split())
	(l1, r1) = map(int, inputi().split())
	(l2, r2) = map(int, inputi().split())
	overlap = min(r1, r2) - max(l1, l2)
	if overlap * n >= k:
		print(0)
		return
	maxoverlap = max(r1, r2) - min(l1, l2)
	if overlap >= 0:
		if maxoverlap * n >= k:
			k -= overlap * n
			print(k)
		else:
			k -= maxoverlap * n
			print((maxoverlap - overlap) * n + k * 2)
	else:
		cost = 0
		for i in range(n):
			noverlap = min(maxoverlap, k)
			cost += -overlap + noverlap
			k -= noverlap
			if k <= -overlap:
				cost += k * 2
				break
		else:
			cost += k * 2
		print(cost)

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


def anss1(stp, numneed, stc):
	ans1 = stp
	if stc >= numneed:
		ans1 += numneed
	else:
		ans1 += stc
		numneed -= stc
		ans1 += 2 * numneed
	return ans1

def anss2(stp, numneed, stc):
	ans2 = 0
	for i in range(n):
		if i > 0 and numneed < stp:
			ans2 += 2 * numneed
			numneed = 0
		else:
			ans2 += stp
			if stc >= numneed:
				ans2 += numneed
				numneed = 0
				break
			ans2 += stc
			numneed -= stc
	if numneed > 0:
		ans2 += 2 * numneed
	return ans2
for t in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	stp = 0
	stc = 0
	curr = 0
	if r1 < l2 or r2 < l1:
		a1 = min(l1, l2)
		b1 = min(r1, r2)
		a2 = max(l1, l2)
		b2 = max(r1, r2)
		stp = a2 - b1
		stc = b2 - a1
	else:
		points = list(sorted([l1, r1, l2, r2]))
		curr = points[2] - points[1]
		stc = points[3] - points[0] - curr
	numneed = k - n * curr
	if numneed <= 0:
		print(0)
	else:
		print(min(anss1(stp, numneed, stc), anss2(stp, numneed, stc)))


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(n, k, a, b, c, d):
	console('----- solving ------')
	((a, b), (c, d)) = sorted([(a, b), (c, d)])
	console(n, k, a, b, c, d)
	outer_range = max(b, d) - min(a, c)
	console('outer_range', outer_range)
	overlapping = b - a + (d - c) - outer_range
	console('overlapping', overlapping)
	stage_1 = 0
	stage_2 = outer_range
	if overlapping < 0:
		overlapping = 0
		stage_1 = c - b
	else:
		k = k - overlapping * n
		stage_2 = outer_range - overlapping
	if k < 0:
		return 0
	if stage_2 == 0:
		return k * 2
	console('stage_1, stage_2, k', stage_1, stage_2, k)
	complete_sets = min(n, k // stage_2)
	console('complete_sets', complete_sets)
	required = complete_sets * (stage_2 + stage_1)
	console('required (1)', required)
	remaining = k - complete_sets * stage_2
	console('remaining', remaining)
	if complete_sets == n:
		required += remaining * 2
	elif complete_sets > 0:
		required += min(remaining * 2, stage_1 + remaining)
		required = min(required, stage_1 + stage_2 + (k - stage_2) * 2)
	else:
		required += stage_1 + remaining
	return required

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	(n, k) = list(map(int, input().split()))
	(a, b) = list(map(int, input().split()))
	(c, d) = list(map(int, input().split()))
	res = solve(n, k, a, b, c, d)
	print(res)


from itertools import count
import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def output(*args):
	sys.stdout.buffer.write(('\n'.join(map(str, args)) + '\n').encode('utf-8'))

def main():
	t = int(input())
	ans_a = [''] * t
	for ti in range(t):
		(n, k) = map(int, input().split())
		(al, ar) = map(int, input().split())
		(bl, br) = map(int, input().split())
		if al > bl:
			(al, ar, bl, br) = (bl, br, al, ar)
		intersection = max(0, min(ar, br) - bl) * n
		if intersection >= k:
			ans_a[ti] = '0'
			continue
		cost1 = 0
		cost2 = 0
		ans = 0
		if ar < bl:
			cost1 = bl - ar
			cost2 = br - al
		elif br < ar:
			cost2 = bl - al + ar - br
		else:
			cost2 = bl - al + br - ar
		ans2 = 10 ** 18
		for i in range(n):
			ans += cost1
			x = min(cost2, k - intersection)
			ans += x
			intersection += x
			if intersection == k:
				break
			ans2 = min(ans2, ans + (k - intersection) * 2)
		if intersection < k:
			ans += (k - intersection) * 2
		ans = min(ans, ans2)
		ans_a[ti] = str(ans)
	output(*ans_a)
main()


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

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

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

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

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

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	inf = 10 ** 16
	(n, k) = MI()
	(al, ar) = MI()
	(bl, br) = MI()
	if al > bl:
		(al, ar, bl, br) = (bl, br, al, ar)
	d = max(0, min(ar, br) - max(al, bl))
	k -= d * n
	if k <= 0:
		print(0)
		continue
	if (al, ar) == (bl, br):
		print(k * 2)
		continue
	cost1 = max(0, bl - ar)
	score = 0
	ans1 = cost1
	ar = max(ar, bl)
	cost2 = abs(al - bl) + abs(ar - br)
	score += min(cost2, k)
	ans1 += min(cost2, k) + (k - score) * 2
	score = 0
	ans2 = ans3 = inf
	t = min(k // cost2, n)
	if t:
		score += t * cost2
		ans2 = ans3 = t * (cost1 + cost2)
		ans2 += (k - score) * 2
		if t < n:
			ans3 += cost1 + (k - score)
		else:
			ans3 = inf
	print(min(ans1, ans2, ans3))


import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')
T = ni()
for _ in range(T):
	(n, k) = nm()
	(a, b) = nm()
	(c, d) = nm()
	if d < b:
		(a, b, c, d) = (c, d, a, b)
	if a <= c <= b:
		cur = (b - c) * n
		if (d - a) * n > k:
			print(max(k - cur, 0))
		else:
			ans = (d - a) * n - cur
			print(ans + (k - (d - a) * n) * 2)
	elif c < a:
		cur = (b - a) * n
		if (d - c) * n > k:
			print(max(k - cur, 0))
		else:
			ans = (d - c) * n - cur
			print(ans + (k - (d - c) * n) * 2)
	else:
		ans = 10 ** 18
		cur = 0
		for i in range(n):
			cur += c - b
			if k > d - a:
				k -= d - a
				cur += d - a
			else:
				cur += k
				k -= k
			ans = min(ans, cur + k * 2)
		print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	over = min(r1, r2) - max(l1, l2)
	moves = 0
	if (l1, r1) == (l2, r2):
		if (r1 - l1) * n >= k:
			print(0)
		else:
			k -= (r1 - l1) * n
			print(2 * k)
	elif over > 0:
		over *= n
		if over >= k:
			print(0)
		else:
			o = max(r1, r2) - min(l1, l2) - over // n
			if over + o * n >= k:
				print(k - over)
			else:
				moves += o * n
				over += o * n
				print(moves + (k - over) * 2)
	else:
		d = -1 * over
		o = max(r1, r2) - min(l1, l2)
		x = k // o
		k = k % o
		if n > x:
			moves += x * (d + o)
			if d < k or x == 0:
				moves += d + k
			else:
				moves += 2 * k
		else:
			moves += n * (d + o) + ((x - n) * o + k) * 2
		print(moves)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	over = max(0, min(r1, r2) - max(l1, l2))
	cover = max(0, max(l1, l2) - min(r1, r2))
	extend = max(r1, r2) - min(l1, l2) - over
	i = over * n
	copy = n
	operation = 0
	if i < k:
		m = min(k - i, extend)
		operation += cover + m
		i += m
		n -= 1
	while i < k:
		m = min(k - i, extend)
		if n == 0:
			m = k - i
			operation += 2 * m
			i = k
		elif cover < m:
			operation += m + cover
			i += m
			n -= 1
		else:
			operation += 2 * (k - i)
			i = k
	print(operation)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	ans = 10 ** 18
	if r1 < l2:
		shared = 0
		initial_cost = l2 - r1
		max_len = r2 - l1
	else:
		shared = min(r1, r2) - l2
		initial_cost = 0
		max_len = max(r1, r2) - l1 - shared
	k = max(0, k - shared * n)
	if k == 0:
		ans = 0
	for i in range(n + 1):
		if i == 0 and initial_cost > 0:
			continue
		cost = i * initial_cost
		rest = max(0, k - i * max_len)
		ans = min(ans, cost + (k - rest) + rest * 2)
	print(ans)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l2 < l1:
		(l1, l2, r1, r2) = (l2, l1, r2, r1)
	d = max(r1, r2) - min(l1, l2)
	res = 0
	if r1 < l2:
		sd = l2 - r1
		if k <= d * n:
			if k % d >= sd:
				res = (d + sd) * (k // d) + k % d + sd
			elif k < sd:
				res = sd + k
			else:
				res = (d + sd) * (k // d) + k % d * 2
		else:
			res = (d + sd) * n + (k - d * n) * 2
	else:
		c1 = d - (r1 - l1) + d - (r2 - l2)
		c0 = d - c1
		if k <= c0 * n:
			res = 0
		elif k <= d * n:
			res = k - c0 * n
		else:
			res = d * n - c0 * n + (k - d * n) * 2
	print(res)


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		(l1, r1) = map(int, input().split())
		(l2, r2) = map(int, input().split())
		if l1 > l2:
			p1 = l1
			p2 = r1
			l1 = l2
			r1 = r2
			l2 = p1
			r2 = p2
		total = 0
		moves = 0
		if r1 >= l2:
			intersection = min(r1, r2) - max(l1, l2)
			l3 = min(l1, l2)
			r3 = max(r1, r2)
			leftover = r3 - l3 - intersection
			k -= intersection * n
			if k <= 0:
				print(0)
			elif k - leftover * n <= 0:
				print(k)
			else:
				k -= leftover * n
				moves += leftover * n
				moves += k * 2
				k = 0
				print(moves)
		else:
			l3 = l1
			r3 = r2
			a = l2 - r1
			if (r3 - l3) * n >= k:
				if k <= r3 - l3:
					print(a + k)
					continue
				else:
					k -= r3 - l3
					moves += a + r3 - l3
				if r3 - l3 + a >= 2 * (r3 - l3):
					moves += 2 * k
				else:
					moves += (r3 - l3 + a) * (k // (r3 - l3))
					moves += min(k % (r3 - l3) + a, 2 * (k % (r3 - l3)))
				print(moves)
			else:
				k -= (r3 - l3) * n
				moves += (r3 - l3 + a) * n
				moves += k * 2
				print(moves)
prog()


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	(l1, r1) = list(map(int, input().split()))
	(l2, r2) = list(map(int, input().split()))
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l2 <= r1:
		per_interval = abs(l1 - l2) + abs(r1 - r2)
		already_overlap = min(r1, r2) - max(l1, l2)
		k -= already_overlap * n
		k = max(k, 0)
		if per_interval * n >= k:
			print(k)
			continue
		cost = per_interval * n
		cost += 2 * (k - cost)
		print(cost)
		continue
	diff = l2 - r1
	per_interval = r2 - l1
	if per_interval >= k:
		print(diff + k)
		continue
	cost = diff + per_interval
	rem_n = n - 1
	rem_k = k - per_interval
	while rem_n and diff < per_interval and (rem_k >= per_interval):
		rem_n -= 1
		rem_k -= per_interval
		cost += diff + per_interval
	if rem_n and diff < per_interval:
		cost_from_new = diff + rem_k
		cost_from_cur = 2 * rem_k
		cost += min(cost_from_new, cost_from_cur)
	else:
		cost += 2 * rem_k
	print(cost)


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	(n, k) = rl()
	(l1, r1) = rl()
	(l2, r2) = rl()
	l = min(l1, l2)
	r = max(r1, r2)
	if min(r1, r2) >= max(l1, l2):
		intersect = min(r1, r2) - max(l1, l2)
		total_intersection_at_start = n * intersect
		total_accessible_with_cost_one = n * (r - l)
		if k <= total_intersection_at_start:
			ans = 0
		elif k <= total_accessible_with_cost_one:
			ans = k - total_intersection_at_start
		else:
			ans = total_accessible_with_cost_one - total_intersection_at_start + (k - total_accessible_with_cost_one) * 2
	else:
		cost_to_touch = max(l1, l2) - min(r1, r2)
		ans = 10 ** 10
		for i in range(1, n + 1):
			cost_to_touch_i = cost_to_touch * i
			total_accessible_with_cost_one = i * (r - l)
			if k <= total_accessible_with_cost_one:
				ans_i = cost_to_touch_i + k
			else:
				ans_i = cost_to_touch_i + total_accessible_with_cost_one + (k - total_accessible_with_cost_one) * 2
			ans = min(ans_i, ans)
	print(ans)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	ans = 0
	if k <= 0:
		print(0)
	elif l1 >= l2 and r1 <= r2:
		k -= (r1 - l1) * n
		if k <= 0:
			print(0)
		else:
			trans = n * (l1 - l2 + r2 - r1)
			ans += min(k, trans)
			k -= trans
			print(ans if k <= 0 else ans + 2 * k)
	elif l2 >= l1 and r2 <= r1:
		k -= (r2 - l2) * n
		if k <= 0:
			print(0)
		else:
			trans = n * (l2 - l1 + r1 - r2)
			ans += min(k, trans)
			k -= trans
			print(ans if k <= 0 else ans + 2 * k)
	elif r2 <= l1:
		ans += l1 - r2
		trans = r1 - l2
		ans += min(k, trans)
		k -= trans
		if k <= 0:
			print(ans)
		else:
			for i in range(2, n + 1):
				t1 = l1 - r2 + min(k, r1 - l2)
				ans += min(t1, 2 * min(k, r1 - l2))
				k -= min(k, r1 - l2)
				if k <= 0:
					break
			print(ans if k <= 0 else ans + 2 * k)
	elif l2 >= r1:
		ans += l2 - r1
		trans = r2 - l1
		ans += min(k, trans)
		k -= trans
		if k <= 0:
			print(ans)
		else:
			for i in range(2, n + 1):
				t1 = l2 - r1 + min(k, r2 - l1)
				ans += min(t1, 2 * min(k, r2 - l1))
				k -= min(k, r2 - l1)
				if k <= 0:
					break
			print(ans if k <= 0 else ans + 2 * k)
	else:
		if l1 > l2 or (l1 == l2 and r1 > r2):
			(l1, l2) = (l2, l1)
			(r1, r2) = (r2, r1)
		k -= n * (r1 - l2)
		if k <= 0:
			print(0)
		else:
			trans = n * (abs(r2 - r1) + abs(l2 - l1))
			ans += min(k, trans)
			k -= trans
			print(ans if k <= 0 else ans + 2 * k)


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(n, k) = map(int, stdin.readline().split())
	(l1, r1) = map(int, stdin.readline().split())
	(l2, r2) = map(int, stdin.readline().split())
	if l1 >= r2 or l2 >= r1:
		ans = float('inf')
		for usenum in range(1, n + 1):
			if max(abs(l1 - r2), abs(l2 - r1)) * usenum >= k:
				ans = min(ans, min(abs(l1 - r2), abs(l2 - r1)) * usenum + k)
			else:
				rem = k - max(abs(l1 - r2), abs(l2 - r1)) * usenum
				ans = min(ans, min(abs(l1 - r2), abs(l2 - r1)) * usenum + max(abs(l1 - r2), abs(l2 - r1)) * usenum + rem * 2)
		print(ans)
	else:
		ans = float('inf')
		over = min(r1 - l1, r2 - l2, r1 - l2, r2 - l1)
		usenum = n
		if over * usenum >= k:
			print(0)
			continue
		k -= over * usenum
		useable = max(r1 - l1, r2 - l2, r1 - l2, r2 - l1) - over
		if useable * usenum >= k:
			print(k)
			continue
		else:
			rem = k - useable * usenum
			print(useable * usenum + rem * 2)


import math
m = int(input())
for l in range(m):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	moves = 0
	if l1 > l2:
		p = l2
		l2 = l1
		l1 = p
		p = r1
		r1 = r2
		r2 = p
	if l2 < r1:
		if n * (min(r1, r2) - l2) >= k:
			moves = 0
			k = 0
			print(0)
			continue
		else:
			k -= n * (min(r1, r2) - l2)
	for i in range(n):
		mmoves = moves
		kp = k
		l21 = l2
		if r1 - l1 < l2 - l1:
			mmoves += l2 - r1
			l21 = r1
		if k <= 0:
			moves = min(mmoves + k, moves + 2 * kp)
			k = 0
			break
		if l21 <= r1:
			k -= l21 - l1
			mmoves += l21 - l1
			if k <= 0:
				if moves == 0:
					moves = mmoves
				v1 = moves + 2 * kp
				v2 = mmoves + k
				moves = min(v1, v2)
				k = 0
				break
		if r1 > r2:
			k -= r1 - r2
			mmoves += r1 - r2
			if k < 0:
				if moves == 0:
					moves = mmoves
				moves = min(mmoves + k, moves + 2 * kp)
				k = 0
				break
		else:
			k -= r2 - r1
			mmoves += r2 - r1
			if k <= 0:
				if moves == 0:
					moves = mmoves
				moves = min(mmoves + k, moves + 2 * kp)
				k = 0
				break
		moves = mmoves
	if k != 0:
		moves += 2 * k
	print(moves)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

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

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

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

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

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	(n, k) = MI()
	(al, ar) = MI()
	(bl, br) = MI()
	if al > bl:
		(al, ar, bl, br) = (bl, br, al, ar)
	p1 = max(0, bl - ar)
	p2 = abs(al - bl) + abs(ar - br)
	d = max(0, min(ar, br) - max(al, bl))
	k -= d * n
	if k <= 0:
		print(0)
		continue
	if p2 == 0:
		print(k * 2)
		continue
	if k // (p2 - p1) == 0 or p1 * 2 - p2 >= 0:
		ans = p1
		cost = min(p2 - p1, k)
		ans += cost
		k -= cost
		if k:
			ans += 2 * k
	else:
		t = min(k // (p2 - p1), n)
		n -= t
		k -= (p2 - p1) * t
		ans = p2 * t
		if n and k > p1:
			ans += k + p1
		else:
			ans += 2 * k
	print(ans)


t = int(input())
for _ in range(0, t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if max(l1, l2) <= min(r1, r2):
		cur_inter = n * (min(r1, r2) - max(l1, l2))
		can_inter = n * (max(r1, r2) - min(l1, l2))
		if k <= cur_inter:
			print(0)
		elif k <= can_inter:
			print(k - cur_inter)
		else:
			print(can_inter - cur_inter + 2 * (k - can_inter))
	else:
		ans = 10 ** 10 + 7
		for i in range(1, n + 1):
			to_inter = i * (max(l1, l2) - min(r1, r2))
			can_inter = i * (max(r1, r2) - min(l1, l2))
			if k <= can_inter:
				to_inter += k
			else:
				to_inter += can_inter + 2 * (k - can_inter)
			ans = min(ans, to_inter)
		print(ans)


import sys
readline = sys.stdin.buffer.readline

def even(n):
	return 1 if n % 2 == 0 else 0

def main():
	(n, k) = map(int, readline().split())
	a = list(map(int, readline().split()))
	b = list(map(int, readline().split()))
	if a[0] > b[0]:
		(a, b) = (b, a)
	score = 0
	cost = 0
	score_init = max(0, min(a[1], b[1]) - b[0]) * n
	if score_init >= k:
		print(0)
		return
	if a[0] <= b[0] and b[1] <= a[1]:
		res = b[0] - a[0] + a[1] - b[1]
		score += res * n
		cost += res * n
		if k <= score + score_init:
			print(k - score_init)
			return
	elif a[1] > b[0]:
		res = b[0] - a[0] + b[1] - a[1]
		score += res * n
		cost += res * n
		if k <= score + score_init:
			print(k - score_init)
			return
	else:
		one_term_score = b[1] - a[0]
		one_term_cost = a[1] - a[0] + b[1] - b[0] + (b[0] - a[1]) * 2
		if k < one_term_score * n:
			let = k // one_term_score
			if let == 0:
				print(b[0] - a[1] + k)
				return
			score += one_term_score * let
			cost += one_term_cost * let
			if k - score < b[0] - a[1]:
				pass
			else:
				cost += b[0] - a[1] + (k - score)
				score = k
		else:
			score += one_term_score * n
			cost += one_term_cost * n
	if score + score_init < k:
		cost += 2 * (k - score - score_init)
	print(cost)
n = int(readline())
for i in range(n):
	main()


def inter(l1, r1, l2, r2):
	l = max(l1, l2)
	r = min(r1, r2)
	return max(r - l, 0)

def solve():
	(n, k) = map(int, input().split())
	(al, ar) = map(int, input().split())
	(bl, br) = map(int, input().split())
	res = inter(al, ar, bl, br) * n
	goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br)
	minhodi = 1000000000000
	hodi = 0
	to_soed = max(0, max(al, bl) - min(ar, br))
	if res >= k:
		print(0)
		return 0
	for i in range(n):
		hodi += to_soed
		ineed = k - res
		if goodt >= ineed:
			hodi += ineed
			minhodi = min(minhodi, hodi)
			break
		hodi += goodt
		res += goodt
		minhodi = min(hodi + (k - res) * 2, minhodi)
	print(minhodi)
for i in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	nk = input().split()
	n = int(nk[0])
	k = int(nk[1])
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l2 >= r1:
		reach = l2 - r1
		span = r2 - l1
		if k <= span:
			print(reach + k)
			continue
		else:
			steps = 0
			while k >= span and n > 0:
				k -= span
				n -= 1
				steps += reach + span
			if n == 0:
				steps += k * 2
				print(steps)
				continue
			else:
				print(min(steps + reach + k, steps + k * 2))
				continue
	else:
		if r2 >= r1:
			covered = r1 - l2
		else:
			covered = r2 - l2
		inhand = covered * n
		if inhand >= k:
			print(0)
			continue
		k -= inhand
		if r2 >= r1:
			addspan = (r2 - r1 + l2 - l1) * n
		else:
			addspan = (r1 - r2 + l2 - l1) * n
		if addspan >= k:
			print(k)
			continue
		else:
			k -= addspan
			steps = addspan + k * 2
			print(steps)
			continue


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	ans = 0
	if max(l1, l2) <= min(r1, r2):
		k -= (min(r1, r2) - max(l1, l2)) * n
		if k <= 0:
			print(0)
		else:
			add = max(r1, r2) - min(l1, l2) - (min(r1, r2) - max(l1, l2))
			if k - add * n <= 0:
				print(k)
			else:
				print(add * n + (k - add * n) * 2)
	else:
		ni = max(l1, l2) - min(r1, r2)
		bg = max(r1, r2) - min(l1, l2)
		for i in range(n):
			ans += ni
			ans += min(k, bg)
			k -= bg
			if k <= 0:
				break
			if k <= ni:
				ans += k * 2
				k = -1
				break
		if k <= 0:
			print(ans)
		else:
			print(ans + k * 2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l2 > r1:
		ans = float('inf')
		for i in range(1, n + 1):
			temp = i * (l2 - r1)
			limit = (r2 - l1) * i
			if k <= limit:
				temp += k
				ans = min(ans, temp)
			else:
				temp += limit
				K = k - limit
				temp += 2 * K
				ans = min(ans, temp)
		print(ans)
	else:
		count = n * (min(r1, r2) - l2)
		res = 0
		if count > k:
			print(res)
			continue
		b = max(r2, r1) - min(l2, l1) - (min(r1, r2) - l2)
		for i in range(n):
			if k - count <= b:
				res += k - count
				break
			else:
				res += b
				count += b
		else:
			res += (k - count) * 2
		print(res)


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter
from pprint import pprint

def main():
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l2 < l1:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	ans = 0
	al = 0
	if l2 < r1:
		al = n * (r1 - l2)
		if r2 < r1:
			al -= n * (r1 - r2)
	if al >= k:
		return 0
	k -= al
	d = max(0, l2 - r1)
	ans += d
	reg = max(r1, r2) - min(l1, l2) - al // n
	if reg >= k:
		return ans + k
	n -= 1
	ans += reg
	k -= reg
	a = n
	if reg > 0:
		a = min(n, k // reg)
	b = k - reg * a
	ans += min(a * (d + reg), 2 * a * reg)
	if a < n:
		ans += min(2 * b, d + b)
	else:
		ans += 2 * b
	return ans
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	print(main())


import math

def main():
	t = int(input())
	while t > 0:
		(n, k) = map(int, input().split())
		(l1, r1) = map(int, input().split())
		(l2, r2) = map(int, input().split())
		if l1 > l2:
			(l1, r1, l2, r2) = (l2, r2, l1, r1)
		distance = max(0, l2 - r1)
		inter = max(0, min(r1, r2) - l2)
		move_one = max(r1, r2) - l1 - inter
		k -= n * inter
		if k < 0:
			print(0)
		else:
			min_cost = 1e309
			for i in range(1, n + 1, 1):
				connection = distance * i
				if i * move_one > k:
					min_cost = min(min_cost, k + connection)
				else:
					min_cost = min(min_cost, connection + 2 * (k - move_one * i) + move_one * i)
			print(min_cost)
		t -= 1
main()


def get_value(l, r, x, y):
	return abs(l - x) + abs(r - y)
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	(lmin, rmax) = (min(l1, l2), max(r1, r2))
	ma = rmax - lmin
	if ma * n <= k:
		res = n * (get_value(l1, r1, lmin, rmax) + get_value(l2, r2, lmin, rmax)) + (k - ma * n) * 2
	else:
		(lmax, rmin) = (max(l1, l2), min(r1, r2))
		if lmax <= rmin:
			tmp = rmin - lmax
			res = k - tmp * n if k > tmp * n else 0
		else:
			distance = lmax - rmin
			val = distance + ma
			res = val + 2 * (k - ma) if k > ma else distance + k
			num = (k + ma - 1) // ma
			res = min(res, distance * num + k)
			num = k // ma
			if num > 0:
				res = min(res, distance * num + k + (k - ma * num))
	print(res)


import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(1, T + 1):
	(n, k) = list(map(int, input().split()))
	(a, b) = list(map(int, input().split()))
	(s, t) = list(map(int, input().split()))
	if a > s:
		(a, s) = (s, a)
		(b, t) = (t, b)
	if s <= b and n * (min(b, t) - s) >= k:
		print(0)
	elif s <= b:
		now = n * (min(b, t) - s)
		w = n * (s - a + abs(t - b))
		if now + w >= k:
			print(k - now)
		else:
			now += w
			print(w + (k - now) * 2)
	elif t - a >= k:
		print(s - b + k)
	else:
		res = 10 ** 20
		for i in range(1, n + 1):
			tmp = i * (s - b)
			if i * (t - a) >= k:
				tmp += k
			else:
				tmp += i * (t - a) + (k - i * (t - a)) * 2
			res = min(res, tmp)
		print(res)


ans = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(L1, R1) = map(int, input().split())
	(L2, R2) = map(int, input().split())
	mr = max(R1, R2) - min(L1, L2)
	step = 2 * mr - (R1 - L1) - (R2 - L2)
	over = max(0, min(R1, R2) - max(L1, L2))
	dis = max(L1, L2) - min(R1, R2) if over == 0 else 0
	k = k - over * n
	if k <= 0:
		ans.append(0)
		continue
	mr -= over
	res = 0
	if k <= mr:
		res = dis + k
	elif mr * n >= k:
		cnt = k // mr
		r = k % mr
		res += cnt * step
		if r > dis:
			res += dis + r
		else:
			res += 2 * r
	else:
		res += n * step + (k - mr * n) * 2
	ans.append(res)
print('\n'.join(map(str, ans)))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if max(l1, l2) <= min(r1, r2):
		rn = n * (min(r1, r2) - max(l1, l2))
		can = n * (max(r1, r2) - min(l1, l2))
		if k <= rn:
			print(0)
		elif k <= can:
			print(k - rn)
		else:
			print(can - rn + 2 * (k - can))
	else:
		ans = 10 ** 10
		for ct in range(1, n + 1):
			cur = ct * (max(l1, l2) - min(r1, r2))
			can = ct * (max(r1, r2) - min(l1, l2))
			if k <= can:
				cur += k
			else:
				cur += can + 2 * (k - can)
			ans = min(ans, cur)
		print(ans)


import math

def main():
	for _ in range(int(input())):
		(n, k) = list(map(int, input().split()))
		(l1, r1) = list(map(int, input().split()))
		(l2, r2) = list(map(int, input().split()))
		((l1, r1), (l2, r2)) = sorted(((l1, r1), (l2, r2)))
		overhead = max(0, l2 - r1)
		already_done = max(0, min(r1, r2) - l2)
		cheap = max(r1, r2) - l1 - already_done
		ans = math.inf
		if k <= n * already_done:
			ans = 0
		for i in range(1, n + 1):
			todo = max(0, k - n * already_done)
			if todo == 0:
				value = 0
			else:
				value = i * overhead
			value += min(todo, i * cheap)
			todo -= min(todo, i * cheap)
			value += 2 * todo
			ans = min(ans, value)
		print(ans)
main()


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
con = 10 ** 9 + 7
INF = float('inf')

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

def main():
	T = int(input())
	for _ in range(T):
		(N, K) = getlist()
		L1 = getlist()
		L2 = getlist()
		(L1, L2) = list(sorted([L1, L2]))
		(l1, r1) = L1
		(l2, r2) = L2
		if r2 <= r1:
			val = (r2 - l2) * N
			space = (r1 - l1 - (r2 - l2)) * N
			if val >= K:
				print(0)
			elif K <= val + space:
				print(K - val)
			else:
				ans = space
				ans += (K - val - space) * 2
				print(ans)
		elif l2 <= r1:
			val = (r1 - l2) * N
			space = (l2 - l1 + (r2 - r1)) * N
			if val >= K:
				print(0)
			elif K <= val + space:
				print(K - val)
			else:
				ans = space
				ans += (K - val - space) * 2
				print(ans)
		else:
			ans = 0
			val = 0
			ans += l2 - r1
			if K <= r2 - l1:
				ans += K
				print(ans)
			else:
				ans += r2 - l1
				val = r2 - l1
				if (r2 - r1 + l2 - l1) * 2 <= r2 - l1:
					ans += (K - val) * 2
					print(ans)
				else:
					x = l2 - l1 + r2 - r1
					y = r2 - l1
					jud = 'No'
					for i in range(N - 1):
						if val + y < K:
							ans += x
							val += y
						else:
							ans += min(l2 - r1 + (K - val), (K - val) * 2)
							print(ans)
							jud = 'Yes'
							break
					if jud == 'No':
						ans += (K - val) * 2
						print(ans)
main()


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, k) = value()
	a = []
	(l1, r1) = value()
	(l2, r2) = value()
	if l1 > l2:
		(l2, l1) = (l1, l2)
		(r2, r1) = (r1, r2)
	extra = 0
	if r1 < l2:
		extra = -r1 + l2
	k -= max(0, min(r1, r2) - l2) * n
	MAX = max(r2, r1) - l1 - max(0, min(r1, r2) - l2)
	ans = 0
	ans2 = inf
	for i in range(n):
		if k > 0:
			ans += extra
		else:
			break
		here = min(k, MAX)
		k -= here
		ans += here
		if k > 0:
			ans2 = min(ans2, ans + 2 * k)
	if k > 0:
		ans += 2 * k
	print(min(ans, ans2))


import os
import 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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

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

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import defaultdict as dc
for _ in range(N()):
	(n, k) = RL()
	(l1, r1) = RL()
	(l2, r2) = RL()
	(l1, l2, r1, r2) = (min(l1, l2), max(l1, l2), min(r1, r2), max(r1, r2))
	dic = max(0, l2 - r1)
	inc = max(0, r1 - l2)
	now = inc * n
	if now >= k:
		print(0)
	else:
		target = k - now
		delta = r2 - l1 - inc
		if target <= delta:
			print(dic + target)
		else:
			r1 = dic + delta + (target - delta) * 2
			if n * delta < target:
				r2 = n * (dic + delta) + 2 * (target - n * delta)
			else:
				t = target // delta
				r2 = t * (dic + delta)
				if target > t * delta:
					r2 += min(dic + target % delta, 2 * (target % delta))
			print(min(r1, r2))


import math
import sys
input = sys.stdin.readline
T = int(input())
for t in range(T):
	(N, K) = [int(_) for _ in input().split()]
	(l1, r1) = [int(_) for _ in input().split()]
	(l2, r2) = [int(_) for _ in input().split()]
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if r1 <= l2:
		nb_to_touch = l2 - r1
		score_for_seg = r2 - l1
		if K <= score_for_seg:
			print(nb_to_touch + K)
			continue
		else:
			possible_answer = math.inf
			for nb_seg_to_use in range(1, N + 1):
				current_answer = nb_seg_to_use * nb_to_touch
				if K <= nb_seg_to_use * score_for_seg:
					current_answer += K
				else:
					current_answer += nb_seg_to_use * score_for_seg + 2 * (K - nb_seg_to_use * score_for_seg)
				possible_answer = min(possible_answer, current_answer)
			print(possible_answer)
			continue
	else:
		if r1 < r2:
			K -= N * (r1 - l2)
			score_for_seg = r2 - r1 + (l2 - l1)
		else:
			K -= N * (r2 - l2)
			score_for_seg = r1 - r2 + (l2 - l1)
		if K <= 0:
			print(0)
			continue
		if K <= score_for_seg * N:
			print(K)
			continue
		else:
			print(score_for_seg * N + 2 * (K - score_for_seg * N))
			continue


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	x = max(0, min(r1, r2) - max(l1, l2))
	a = max(r1, r2) - min(l1, l2)
	cnt = x * n
	if cnt >= k:
		print(0)
		continue
	if a == x:
		print(2 * (k - cnt))
		continue
	i = min(n, (k - cnt) // (a - x))
	d = k - cnt - i * (a - x)
	p = max(0, max(l1, l2) - min(r1, r2))
	if k - cnt - (a - x) <= 0:
		print(p + k - cnt)
		continue
	if p < d and i < n:
		print(i * (p + a - x) + p + d)
	else:
		print(i * (p + a - x) + 2 * d)


from sys import stdin, stdout

def segment_intersections(n, k, l1, r1, l2, r2):
	lmx = max(l1, l2)
	rmn = min(r1, r2)
	I = max(rmn - lmx, 0) * n
	if I >= k:
		return 0
	k -= I
	lmn = min(l1, l2)
	rmx = max(r1, r2)
	cov = rmx - lmn - max(rmn - lmx, 0)
	if cov == 0:
		return k * 2
	steps = lmx - lmn + (rmx - rmn)
	if steps / cov >= 2:
		if k > cov:
			return steps + (k - cov) * 2
		else:
			return rmx - rmn + k
	d = min(k // cov, n)
	ans = steps * d
	if k // cov < n:
		subans1 = k % cov
		if rmn - lmx < 0:
			subans1 += abs(rmn - lmx)
		subans2 = 10 ** 10
		if k // cov > 0:
			subans2 = k % cov * 2
		ans += min(subans1, subans2)
	else:
		ans += (k - cov * d) * 2
	return ans
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	(l1, r1) = map(int, stdin.readline().split())
	(l2, r2) = map(int, stdin.readline().split())
	ans = segment_intersections(n, k, l1, r1, l2, r2)
	stdout.write(str(ans) + '\n')


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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))
mod = 1000000007

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

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

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

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	(n, k) = sep()
	a = lis()
	b = lis()
	(a, b) = (min(a, b), max(a, b))
	if a[1] >= b[0]:
		intersntn = True
		currans = min(a[1], b[1]) - max(a[0], b[0])
		currans *= n
	else:
		intersntn = False
		currans = 0
	if currans >= k:
		print(0)
		return
	if intersntn:
		temp = max(b[1], a[1]) - min(b[1], a[1])
		temp += max(a[0], b[0]) - min(a[0], b[0])
		if n * temp >= k - currans:
			print(k - currans)
		else:
			ans = n * temp
			print(ans + (k - currans - ans) * 2)
	else:
		tofill = b[0] - a[1]
		temp = b[1] - b[0] + (b[0] - a[0])
		if temp >= k - currans:
			print(tofill + (k - currans))
			return
		if temp + tofill > 2 * temp:
			print((k - currans - temp) * 2 + tofill + temp)
		elif n * temp >= k - currans:
			if (k - currans) % temp == 0:
				print((k - currans) // temp * (tofill + temp))
			else:
				print((k - currans) // temp * (tofill + temp) + min((k - currans) % temp + tofill, 2 * ((k - currans) % temp)))
		else:
			ans = n * temp
			print(n * (temp + tofill) + (k - currans - ans) * 2)
testcase(int(inp()))


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(n, k) = map(int, input().split())
	(l, r) = map(int, input().split())
	(x, y) = map(int, input().split())
	cost = 0
	if r < x or y < l:
		cost = max(x - r, l - y)
	if cost:
		ff = 0
		capacity = max(r, y) - min(l, x) - ff
	else:
		ff = min(r, y) - max(l, x)
		capacity = max(r, y) - min(l, x) - ff
	e = k - ff * n
	if e <= 0:
		print(0)
		continue
	if cost:
		R = 10 ** 18
		for i in range(1, n + 1):
			r = cost * i
			if e <= capacity * i:
				R = min(R, r + e)
			else:
				xx = capacity * i
				R = min(R, r + xx + (e - xx) * 2)
		print(R)
	elif e <= capacity * n:
		print(e)
	else:
		r = capacity * n
		print(r + (e - r) * 2)


import io
import os
import sys
from collections import deque, defaultdict
from itertools import accumulate

def _input():
	x = sys.stdin.readline()
	x = x.replace('\r', '')
	x = x.replace('\n', '')
	return x
if 'STDINPUT' not in os.environ:
	input = _input
else:
	print('Standard input')

def solve():
	(n, k) = [int(x) for x in input().split()]
	(l1, r1) = [int(x) for x in input().split()]
	(l2, r2) = [int(x) for x in input().split()]
	if r1 - l1 < r2 - l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	complete = max(r1, r2) - min(l1, l2)
	intersection = 0
	step = None
	if l2 > l1 and r2 < r1:
		step = l2 - l1 + r1 - r2
		intersection = r2 - l2
	elif l1 > l2 and r1 < r2:
		step = l1 - l2 + r2 - r1
		intersection = r1 - l1
	elif l1 < l2:
		step = l2 - l1 + r2 - r1
		intersection = max(0, r1 - l2)
	else:
		step = l1 - l2 + r1 - r2
		intersection = max(0, r2 - l1)
	k -= intersection * n
	complete -= intersection
	ans = 0
	done = 0
	while k >= complete and done < n:
		k -= complete
		ans += step
		done += 1
	if k <= 0:
		print(ans)
	elif done == n:
		print(ans + 2 * k)
	else:
		if l1 < l2:
			initial = r1 - l2
		else:
			initial = r2 - l1
		print(min(ans + k - min(0, initial), ans + 2 * k if done >= 1 else 10 ** 10))
test_cases = True
if test_cases:
	_q = int(input())
	for _ in range(_q):
		solve()
else:
	solve()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l2 >= r1 or l1 >= r2:
		if l1 >= r2:
			(l1, r1, l2, r2) = (l2, r2, l1, r1)
		d = l2 - r1
		ans = d
		while n > 0:
			n -= 1
			if r2 - l1 < k:
				ans += r2 - l1
				k -= r2 - l1
				if k < d:
					ans += 2 * k
					k = 0
					break
				elif n > 0:
					ans += d
			else:
				ans += k
				k = 0
				break
		if k > 0:
			ans += k * 2
		print(ans)
	elif l2 <= r1 and r2 >= r1 and (l2 >= l1):
		d = r1 - l2
		k -= n * (r1 - l2)
		if k > 0:
			ans = 0
			s = l2 - l1 + r2 - r1
			if n * s < k:
				k -= n * s
				ans += n * s
				ans += 2 * k
			else:
				ans += k
				k = 0
		else:
			ans = 0
		print(ans)
	elif l1 <= r2 and l2 <= l1 and (r2 <= r1):
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
		d = r1 - l2
		k -= n * (r1 - l2)
		if k > 0:
			ans = 0
			s = l2 - l1 + r2 - r1
			if n * s < k:
				k -= n * s
				ans += n * s
				ans += 2 * k
			else:
				ans += k
				k = 0
		else:
			ans = 0
		print(ans)
	else:
		if l2 <= l1 and r1 <= r2:
			(l1, r1, l2, r2) = (l2, r2, l1, r1)
		k -= (r2 - l2) * n
		if k > 0:
			s = l2 - l1 + r1 - r2
			ans = 0
			if n * s < k:
				k -= n * s
				ans += n * s
				ans += 2 * k
			else:
				ans += k
				k = 0
		else:
			ans = 0
		print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

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

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(n, k) = sp()
	(al, ar) = sp()
	(bl, br) = sp()
	if max(al, bl) <= min(ar, br):
		temp = max(0, k - n * (min(ar, br) - max(al, bl)))
		maxi = n * (abs(al - bl) + abs(ar - br))
		out(min(temp, maxi) + max(0, temp - maxi) * 2)
		continue
	temp = max(al, bl) - min(ar, br)
	answer = inf
	for i in range(1, n + 1):
		new = i * temp
		maxi = (max(ar, br) - min(al, bl)) * i
		new += min(k, maxi) + max(0, k - maxi) * 2
		answer = min(answer, new)
	out(answer)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	(l_1, r1) = list(map(int, input().split()))
	(l_2, r2) = list(map(int, input().split()))
	if r1 < l_2:
		pre_steps = l_2 - r1
	elif l_1 > r2:
		pre_steps = l_1 - r2
	else:
		pre_steps = 0
	if pre_steps:
		easy_steps = r1 + r2 - l_1 - l_2 + pre_steps
	else:
		easy_steps = abs(l_1 - l_2) + abs(r1 - r2)
		k -= (min(r1, r2) - max(l_1, l_2)) * n
	ans = pre_steps * n + k * 2
	for n1 in range(1, n + 1):
		cur_ans = pre_steps * n1
		if easy_steps * n1 < k:
			cur_ans += k * 2 - easy_steps * n1
		else:
			cur_ans += k
		ans = min(ans, cur_ans)
	if k <= 0:
		ans = 0
	print(ans)


import sys
readline = sys.stdin.readline

def solve():
	(N, K) = map(int, readline().split())
	(L1, R1) = map(int, readline().split())
	(L2, R2) = map(int, readline().split())
	if L1 > L2:
		(L1, L2) = (L2, L1)
		(R1, R2) = (R2, R1)
	if R1 < L2:
		gap = L2 - R1
		union = R2 - L1
		ans = gap
		if K <= union:
			ans += K
			print(ans)
			return
		ans += union
		rem = K - union
		if N == 1:
			print(ans + 2 * rem)
			return
		t = min(rem // union, N - 1)
		ans += t * (gap + union)
		rem -= t * union
		if t == N - 1:
			print(ans + 2 * rem)
			return
		if 2 * rem < gap + rem:
			print(ans + 2 * rem)
			return
		else:
			print(ans + gap + rem)
			return
	else:
		ovl = min(R1, R2) - L2
		if ovl * N >= K:
			print(0)
			return
		union = max(R1, R2) - L1
		if union * N >= K:
			print(K - ovl * N)
			return
		else:
			ans = union * N - ovl * N
			ans += 2 * (K - union * N)
			print(ans)
			return
T = int(readline())
for i in range(T):
	solve()


T = int(input())
for _ in range(0, T):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	L1 = 0
	R1 = 0
	L2 = 0
	R2 = 0
	if l1 <= l2:
		L1 = l1
		R1 = r1
		L2 = l2
		R2 = r2
	else:
		L1 = l2
		R1 = r2
		L2 = l1
		R2 = r1
	ic = 0
	curr = 0
	if L2 > R1:
		ic = L2 - R1
		curr = 0
	else:
		ic = 0
		curr = min(R1, R2) - max(L1, L2)
	one = max(R2, R1) - min(L1, L2) - curr
	rem = k - curr * n
	if rem <= 0:
		print(0)
	else:
		ans = ic + min(rem, one)
		rem -= one
		if rem <= 0:
			print(ans)
		elif rem < one:
			if ic + rem < 2 * rem and n - 1 > 0:
				ans += ic + rem
			else:
				ans += 2 * rem
			print(ans)
		elif ic + one < 2 * one and n - 1 > 0:
			kk1 = min(n - 1, rem // one)
			rem = rem - kk1 * one
			ans += kk1 * (ic + one)
			if kk1 < n - 1 and ic + rem < 2 * rem:
				ans += ic + rem
			else:
				ans += 2 * rem
			print(ans)
		else:
			ans += 2 * rem
			print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	(l1, r1) = map(int, stdin.readline().split())
	(l2, r2) = map(int, stdin.readline().split())
	curr = max(0, min(r1, r2) - max(l1, l2)) * n
	if curr > k:
		print(0)
	else:
		sin_inc = min(abs(l2 - l1) + abs(r2 - r1), max(r2, r1) - min(l2, l1))
		waste = max(0, max(l2, l1) - min(r2, r1))
		if k - curr <= sin_inc:
			print(waste + k - curr)
		elif sin_inc < k - curr <= sin_inc * n:
			moves = waste + sin_inc
			req = k - curr - sin_inc
			if sin_inc > waste:
				moves += req // sin_inc * (waste + sin_inc)
				moves += min(waste + req % sin_inc, req % sin_inc * 2)
			else:
				moves += req * 2
			print(moves)
		else:
			moves = waste + sin_inc
			req = k - curr - sin_inc
			if sin_inc > waste:
				moves += min(req // sin_inc, n - 1) * (waste + sin_inc)
				if req // sin_inc >= n - 1:
					req -= sin_inc * (n - 1)
					moves += req * 2
				else:
					moves += min(waste + req % sin_inc, req % sin_inc * 2)
			else:
				moves += req * 2
			print(moves)


import math
import sys
import bisect
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	st = 0
	mini = 10 ** 18
	if l1 > l2:
		(templ, tempr) = (l1, r1)
		(l1, r1) = (l2, r2)
		(l2, r2) = (templ, tempr)
	elif l1 == l2:
		if r1 > r2:
			(tl, tr) = (l1, r1)
			r1 = r2
			r2 = tr
	maxi1 = max(r2, r1)
	mini1 = min(l1, l2)
	if l1 >= r2 or l2 >= r1:
		st1 = 0
		for i in range(1, n + 1):
			st += l2 - r1
			req = r1 - l1 + (r2 - r1)
			if req >= k and k > 0:
				mini = min(mini, k + st)
			else:
				k -= req
				if k > 0:
					mini = min(mini, st + (r1 - l1 + (r2 - r1)) + 2 * k)
			st += r1 - l1 + (r2 - r1)
	else:
		if r2 < r1:
			req = r2 - l2
		else:
			req = r1 - l2
		st = 0
		if n * req >= k:
			mini = 0
		else:
			k -= n * req
			st = 0
			for i in range(1, n + 1):
				if r2 < r1:
					req1 = l2 - l1 + (r1 - r2)
				else:
					req1 = r2 - r1 + (l2 - l1)
				if req1 >= k and k > 0:
					mini = min(mini, st + k)
				st += req1
				k -= req1
			if k > 0:
				st += k * 2
				mini = min(mini, st)
	print(mini)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	(l, r) = list(map(int, input().split()))
	(u, v) = list(map(int, input().split()))
	if l > u:
		(l, u) = (u, l)
		(r, v) = (v, r)
	if l == u and v == r:
		overlap = r - l
		overlap = overlap * n
		if overlap >= k:
			print(0)
		else:
			rem = k - overlap
			print(2 * rem)
	elif u > r:
		dis = u - r
		overlap = v - l
		fans = 0
		ans = 10 ** 20
		for i in range(n):
			fans += dis
			if overlap >= k:
				fans += k
				ans = min(ans, fans)
				break
			fans += overlap
			k -= overlap
			ans = min(ans, fans + 2 * k)
		print(ans)
	else:
		if u >= l and u <= r and (v >= r):
			overlap = r - u
			e = u - l + v - r
		elif l >= u and v >= l and (r >= v):
			overlap = v - l
			e = l - u + r - v
		elif u >= l and u <= r and (v >= l) and (v <= r):
			overlap = v - u
			e = u - l + r - v
		overlap *= n
		e *= n
		if k <= overlap:
			print(0)
		else:
			k -= overlap
			if k <= e:
				print(k)
			else:
				ans = e
				k -= e
				ans += 2 * k
				print(ans)


import sys
input = sys.stdin.readline
aaaa = list()
for kek in range(int(input())):
	(n, k) = map(int, input().split())
	(al, ar) = map(int, input().split())
	(bl, br) = map(int, input().split())
	L = n * max(0, min(ar, br) - max(al, bl))
	if L >= k:
		aaaa.append(0)
		continue
	if al <= bl and bl <= ar or (al <= br and br <= ar) or (bl <= ar and ar <= br) or (bl <= al and al <= br):
		x = n * (abs(al - bl) + abs(ar - br))
		if x + L >= k:
			aaaa.append(k - L)
		else:
			aaaa.append(2 * (k - L - x) + x)
		continue
	if al > bl:
		(al, bl) = (bl, al)
		(ar, br) = (br, ar)
	zero = bl - ar
	one = br - al
	ans = 0
	if n <= k // one:
		ans += n * (one + zero)
		k -= n * one
		ans += 2 * k
	else:
		ans += k // one * (one + zero)
		if k // one > 0:
			k -= k // one * one
			ans += min(zero + k, 2 * k)
		else:
			ans += zero + k
	aaaa.append(ans)
print(*aaaa, sep='\n')


import sys
input = sys.stdin.readline

def solve():
	(n, k) = map(int, input().split())
	avail = 0
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l2 < l1:
		(l1, l2) = (l2, l1)
		(r1, r2) = (r2, r1)
	avail = max(r2, r1) - min(l1, l2)
	if l2 <= r1:
		overlap = min(r1, r2) - max(l1, l2)
		k -= overlap * n
		if k <= 0:
			print(0)
			return
		dist = 0
		avail -= overlap
	else:
		dist = l2 - r1
	best = dist + 2 * k
	steps = 0
	for i in range(n):
		steps += dist
		best = min(best, steps + max(0, k - avail) * 2 + min(k, avail))
		steps += avail
		k -= avail
	print(best)
for _ in range(int(input())):
	solve()


import os
import 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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

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

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())

	def getOverlap(a, b):
		return max(0, min(a[1], b[1]) - max(a[0], b[0]))
	ov = getOverlap([l1, r1], [l2, r2])
	if ov * n >= k:
		print(0)
		continue
	ans = 0
	k -= ov * n
	for ii in range(1):
		if ov:
			inc = (max(r1, r2) - min(l1, l2) - ov) * n
			if inc >= k:
				ans += k
				k = 0
				break
			else:
				ans += inc
				k -= inc
				ans += 2 * k
		else:
			answers = []
			for i in range(n):
				diff = max(l1, l2) - min(r1, r2)
				ans += diff
				inc = max(r1, r2) - min(l1, l2)
				if inc >= k:
					ans += k
					k = 0
					break
				else:
					ans += inc
					k -= inc
					answers += [ans + 2 * k]
			if k > 0:
				ans += 2 * k
			ans = min(ans, min(answers) if answers else ans)
	print(ans)


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	intersect = False
	if l1 >= l2 and l1 <= r2:
		intersect = True
	if r1 >= l2 and r1 <= r2:
		intersect = True
	if l2 >= l1 and l2 <= r1:
		intersect = True
	if r2 >= l1 and r2 <= r1:
		intersect = True
	if intersect:
		l = min(l1, l2)
		r = max(r1, r2)
		totlen = r - l
		intlen = r2 - l2 + (r1 - l1) - totlen
		if n * intlen >= k:
			print(0)
		elif n * totlen >= k:
			print(k - n * intlen)
		else:
			s = n * (totlen - intlen)
			k -= n * totlen
			print(s + 2 * k)
	else:
		l = min(l1, l2)
		r = max(r1, r2)
		totlen = r - l
		dist = totlen - (r2 - l2) - (r1 - l1)
		s = 0
		i = 0
		while k > 0 and i < n:
			i += 1
			s += dist
			if k <= totlen:
				s += k
				k = 0
			else:
				k -= totlen
				s += totlen
				if k < dist:
					s += 2 * k
					k = 0
		if k > 0:
			s += 2 * k
		print(s)


import sys
input = sys.stdin.readline
import math

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

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

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

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

def from_file(f):
	return f.readline

def solve(n, k, l1, r1, l2, r2):
	C = min(r1, r2) - max(l1, l2)
	if C >= 0:
		D = abs(l2 - l1) + abs(r2 - r1)
		delta = k - n * C
		if delta <= 0:
			return 0
		res = min(delta, n * D)
		delta -= res
		if delta != 0:
			res += 2 * delta
		return res
	else:
		C = -C
		M = float('inf')
		for i in range(1, n + 1):
			res = C * i
			D = max(r1, r2) - min(l1, l2)
			delta = k
			if delta == 0:
				res = 0
			else:
				res += min(delta, i * D)
				delta -= min(delta, i * D)
				if delta != 0:
					res += 2 * delta
			M = min(res, M)
		return M
t = inp()
for _ in range(t):
	(n, k) = invr()
	(l1, r1) = invr()
	(l2, r2) = invr()
	print(solve(n, k, l1, r1, l2, r2))


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	(l1, r1) = [int(x) for x in input().split()]
	(l2, r2) = [int(x) for x in input().split()]
	its = min(r1, r2) - max(l1, l2)
	if its >= 0:
		k -= n * its
		if k <= 0:
			ans = 0
		else:
			aoc = max(r1, r2) - min(l1, l2)
			if n * (aoc - its) >= k:
				ans = k
			else:
				ans = n * (aoc - its)
				k -= n * (aoc - its)
				ans += k * 2
		print(ans)
	else:
		gap = -its
		aoc = max(r1, r2) - min(l1, l2)
		minn = float('inf')
		for i in range(1, n + 1):
			total = gap * i
			if aoc * i >= k:
				total += k
			else:
				total += aoc * i + (k - aoc * i) * 2
			minn = min(minn, total)
		print(minn)


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	(l1, r1) = [int(x) for x in input().split()]
	(l2, r2) = [int(x) for x in input().split()]
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l2 < r1:
		start = (min(r1, r2) - max(l1, l2)) * n
		if start >= k:
			print(0)
			continue
		cheap = n * (max(r1, r2) - min(l1, l2)) - start
		if start + cheap >= k:
			print(k - start)
			continue
		else:
			print(cheap + (k - start - cheap) * 2)
			continue
	best = 10 ** 100
	cost_sf = 0
	intersection_sf = 0
	for j in range(n):
		cost_sf += l2 - r1
		cheap = r2 - l1
		if intersection_sf + cheap >= k:
			best = min(best, cost_sf + max(k - intersection_sf, 0))
		intersection_sf += cheap
		cost_sf += cheap
		best = min(best, cost_sf + max((k - intersection_sf) * 2, 0))
	print(best)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	mr = max(r1, r2) - min(l1, l2)
	step = 2 * mr - (r1 - l1) - (r2 - l2)
	over = max(0, min(r1, r2) - max(l1, l2))
	dis = max(l1, l2) - min(r1, r2) if over == 0 else 0
	k = k - over * n
	if k <= 0:
		print(0)
		continue
	mr -= over
	res = 0
	if k <= mr:
		res = dis + k
	elif mr * n >= k:
		cnt = k // mr
		r = k % mr
		res += cnt * step
		if r > dis:
			res += dis + r
		else:
			res += 2 * r
	else:
		res += n * step + (k - mr * n) * 2
	print(res)


import sys
input = sys.stdin.readline
t = int(input())
for ii in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	(inter, diff) = (0, 0)
	(left, right) = (max(l1, l2), min(r1, r2))
	(left1, right1) = (min(l1, l2), max(r1, r2))
	maxpos = right1 - left1
	inter = max(0, right - left)
	intertotal = max(0, right - left) * n
	maxpostotal = (right1 - left1) * n
	(pos, ans) = (intertotal, 0)
	if k > maxpostotal:
		ans += (k - maxpostotal) * 2
		pos = k - maxpostotal
	if intertotal >= k:
		print(0)
	else:
		for i in range(n):
			if inter != 0:
				if pos + (maxpos - inter) <= k:
					ans += maxpos - inter
					pos += maxpos - inter
				else:
					ans += k - pos
					break
			elif pos + maxpos <= k:
				ans += maxpos + (left - right)
				pos += maxpos
			else:
				op1 = 100000000000000
				if i > 0:
					op1 = (k - pos) * 2
				op2 = k - pos + left - right
				ans += min(op1, op2)
				break
		print(ans)


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	(l1, r1) = list(map(int, input().split()))
	(l2, r2) = list(map(int, input().split()))
	if r1 > r2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	overlap = max(0, r1 - max(l1, l2))
	k -= overlap * n
	if k <= 0:
		print(0)
		continue
	touch_cost = max(0, max(l1, l2) - min(r1, r2))
	overlap_cost = max(r2, r1) - min(l1, l2) - overlap
	overlap_gain = max(r1, r2) - min(l1, l2)
	ans = 2 * k + touch_cost
	for i in range(1, n + 1):
		poss = touch_cost * i
		if i * overlap_cost >= k:
			poss += k
		else:
			poss += 2 * k - overlap_cost * i
		ans = min(poss, ans)
	print(ans)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

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

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

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

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

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

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		[n, k] = inlt()
		[a1, a2] = inlt()
		[b1, b2] = inlt()

		def swap(a, b):
			temp = a
			a = b
			b = temp
			return [a, b]
		if a2 < b1 or b2 < a1:
			if a2 > b1:
				[a1, b1] = swap(a1, b1)
				[a2, b2] = swap(a2, b2)
			mini = b1 - a2
			maxi = b2 - a1
			if k <= maxi:
				print(mini + k)
			else:
				case1 = (k - maxi) * 2 + mini + maxi
				a = min(k // maxi, n)
				if k // maxi >= n:
					a = n
					case2 = a * (mini + maxi) + 2 * (k - a * maxi)
				else:
					a = k // maxi
					case2 = a * (mini + maxi) + min(k - a * maxi + mini, 2 * (k - a * maxi))
				print(min(case1, case2))
		else:
			if a2 > b2:
				[a1, b1] = swap(a1, b1)
				[a2, b2] = swap(a2, b2)
			curr = (a2 - max(b1, a1)) * n
			if k <= curr:
				print(0)
			else:
				maxi = (b2 - min(a1, b1)) * n
				if k <= maxi:
					print(k - curr)
				else:
					print(2 * (k - maxi) + maxi - curr)
main()


import math
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	(a1, b1) = map(int, input().split())
	(a2, b2) = map(int, input().split())
	val = min(b1, b2) - max(a1, a2)
	if val > 0:
		val = val * n
		if val >= k:
			print(0)
		else:
			num1 = max(b1, b2) - min(a1, a2)
			if k > num1 * n:
				print(num1 * n - val + (k - num1 * n) * 2)
			else:
				print(k - val)
	else:
		steps = max(a1, a2) - min(b1, b2)
		num1 = max(b1, b2) - min(a1, a2)
		if k > num1 * n:
			print(num1 * n + steps * n + (k - num1 * n) * 2)
		else:
			v = min(num1, k)
			if math.ceil(k / num1) - 2 < 0:
				p = 0
			else:
				p = math.ceil(k / num1) - 2
			aj = k - v - num1 * p
			aj = min(2 * aj, aj + steps)
			ak = num1 * p
			aw = steps * p
			print(v + steps + min(2 * (k - v), aw + ak + aj))


t = int(input())

def solve3(n, k, l1, r1, l2, r2):
	(A, B) = (l2 - r1, r2 - l1)
	g = min(n, k // B)
	ans = g * (A + B)
	k -= g * B
	if g == n:
		ans = ans + 2 * k
	else:
		ans = min(ans + A + k, ans + 2 * k)
	print(ans)

def solve1(n, k, l1, r1, l2, r2):
	(A, B) = (l2 - r1, r2 - l1)
	ans = 100000000000000
	for i in range(1, n + 1):
		ans_ = i * A
		if k <= i * B:
			ans = min(ans, ans_ + k)
		else:
			ans = min(ans, ans_ + i * B + (k - i * B) * 2)
	print(ans)

def solve2(n, k, l1, r1, l2, r2):
	X = abs(l1 - l2) + abs(r1 - r2)
	if r2 < r1:
		k -= (r2 - l2) * n
	else:
		k -= (r1 - l2) * n
	if k <= X * n:
		print(max(k, 0))
	else:
		ans = X * n
		k -= X * n
		print(ans + 2 * k)
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if l1 > l2:
		(l1, l2) = (l2, l1)
		(r1, r2) = (r2, r1)
	if l2 >= r1:
		solve1(n, k, l1, r1, l2, r2)
	else:
		solve2(n, k, l1, r1, l2, r2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	ans = float('inf')
	if min(r1, r2) < max(l1, l2):
		i = max(l1, l2) - min(r1, r2)
		for j in range(1, n + 1):
			c = i * j
			m = (max(r1, r2) - min(l1, l2)) * j
			c += min(k, m) + max(0, k - m) * 2
			ans = min(ans, c)
	else:
		k = max(0, k - n * (min(r1, r2) - max(l1, l2)))
		m = n * (abs(l1 - l2) + abs(r1 - r2))
		ans = min(k, m) + 2 * max(0, k - m)
	print(int(ans))


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import 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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	gap = max(0, -min(r1, r2) + max(l1, l2))
	lap = max(0, min(r1, r2) - max(l1, l2))
	total = max(r1, r2) - min(l1, l2)
	if lap * n >= k:
		print(0)
		continue
	lap1 = lap
	lap *= n
	ans = 0
	t = min(k - lap, total - lap1)
	ans += gap + t
	n -= 1
	lap += t
	while lap < k:
		t = min(k - lap, total - lap1)
		if n == 0:
			break
		elif gap <= t:
			lap += t
			ans += t + gap
			n -= 1
		else:
			ans += 2 * t
			lap += t
	if lap < k:
		ans += 2 * (k - lap)
	print(ans)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

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

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

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

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

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

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

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	(n, k) = IP()
	(l1, r1) = IP()
	(l2, r2) = IP()
	a = [[l1, r1] for i in range(n)]
	b = [[l2, r2] for i in range(n)]
	(x, y) = (max(l1, l2), min(r1, r2))
	inter = max(0, y - x)
	if y > x:
		k -= (y - x) * n
	steps = 0
	for i in range(n):
		if k <= 0:
			break
		reqd = min(k, max(r1, r2) - min(l1, l2) - inter)
		cost = max(0, x - y) + reqd
		if i >= 1:
			cost = min(cost, reqd * 2)
		k -= reqd
		steps += cost
	if k > 0:
		steps += k * 2
	P(steps)
	return
t = II()
for i in range(t):
	solve()


t = int(input())
for _ in range(t):
	(n, k) = [int(s) for s in input().split()]
	ans = 0
	(l1, r1) = [int(s) for s in input().split()]
	(l2, r2) = [int(s) for s in input().split()]
	if r1 > r2:
		(l1, l2, r1, r2) = (l2, l1, r2, r1)
	initial_k = max(0, r1 - max(l1, l2))
	k -= initial_k * n
	if k < 0:
		print(0)
		continue
	in_cost = max(0, max(l1, l2) - r1)
	one_cost = r2 - min(l1, l2) - initial_k
	ans = 2 * k + in_cost
	for i in range(1, n + 1):
		cand = in_cost * i
		if one_cost * i >= k:
			cand += k
		else:
			cand += 2 * k - one_cost * i
		ans = min(ans, cand)
	print(ans)


def solve(i):
	a = i * u
	b = i * v
	if k <= -a:
		return 0
	if k <= b:
		return a + k
	return a + b + 2 * (k - b)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	u = max(l1, l2) - min(r1, r2)
	v = max(r1, r2) - min(l1, l2)
	if u <= 0:
		print(solve(n))
	else:
		print(min((solve(i) for i in range(1, n + 1))))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	intersect_length = min(r1, r2) - max(l1, l2)
	length = max(r1, r2) - min(l1, l2)
	ans = 0
	cost = 0
	if intersect_length > 0:
		ans += intersect_length * n
		if ans >= k:
			print(0)
			continue
		nokori = length - intersect_length
		for _ in range(n):
			if nokori + ans <= k:
				cost += nokori
				ans += nokori
			else:
				diff = k - ans
				cost += diff
				ans += diff
		print(cost + (k - ans) * 2)
		continue
	dist = -intersect_length
	cost += dist
	nokori = length
	if nokori + ans <= k:
		ans += nokori
		cost += nokori
	else:
		print(cost + k)
		continue
	to_same_val = max(r1, r2) - min(l1, l2)
	to_same_cost = 2 * to_same_val - (r1 - l1) - (r2 - l2)
	if to_same_val * 2 <= to_same_cost:
		print(cost + (k - ans) * 2)
		continue
	for _ in range(1, n):
		if to_same_val + ans <= k:
			ans += to_same_val
			cost += to_same_cost
		else:
			need_val = k - ans
			need_cost = dist + need_val
			if need_val * 2 >= need_cost:
				ans += need_val
				cost += need_cost
	print(cost + (k - ans) * 2)


for _ in range(int(input())):
	ans = 1e+18
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if min(r1, r2) >= max(l1, l2):
		rem = max(0, k - n * (min(r1, r2) - max(l1, l2)))
		poss = n * (abs(l1 - l2) + abs(r1 - r2))
		ans = min(rem, poss) + 2 * max(0, rem - poss)
	else:
		invest = max(l1, l2) - min(r1, r2)
		for i in range(1, n + 1):
			curans = invest * i
			poss = i * (max(r1, r2) - min(l1, l2))
			curans += min(k, poss) + 2 * max(0, k - poss)
			ans = min(curans, ans)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(n, k) = map(int, input().split())
	(l1, r1) = map(int, input().split())
	(l2, r2) = map(int, input().split())
	if r2 < r1:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if l1 <= l2 <= r1 <= r2:
		start = (r1 - l2) * n
		plusone = (r2 - l1 - (r1 - l2)) * n
		if k <= start:
			print(0)
		else:
			k -= start
			if k <= plusone:
				print(k)
			else:
				print(plusone + (k - plusone) * 2)
	elif l2 <= l1 <= r1 <= r2:
		start = (r1 - l1) * n
		plusone = (r2 - l2 - (r1 - l1)) * n
		if k <= start:
			print(0)
		else:
			k -= start
			if k <= plusone:
				print(k)
			else:
				print(plusone + (k - plusone) * 2)
	else:
		zero = l2 - r1
		plusone = r2 - l1
		ANS = 1 << 60
		for use in range(1, n + 1):
			if k <= plusone * use:
				(q, r) = divmod(k, plusone)
				ANS1 = use * zero
				ANS1 += q * plusone
				if r > 0:
					ANS1 += r
				ANS = min(ANS, ANS1)
			else:
				ANS1 = (zero + plusone) * use
				k2 = k - plusone * use
				ANS1 += k2 * 2
				ANS = min(ANS, ANS1)
		print(ANS)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	(a, b) = map(int, input().split())
	(c, d) = map(int, input().split())
	if c < a:
		(a, b, c, d) = (c, d, a, b)
	elif c == a and b < d:
		(a, b, c, d) = (c, d, a, b)
	count = 0
	if b > c:
		if d > b:
			k -= (b - c) * n
		else:
			k -= (d - c) * n
	if k <= 0:
		print(0)
		continue
	if b > c:
		if d > b:
			cal = (c + d - a - b) * n
			if cal >= k:
				print(k)
			else:
				print(cal + (k - cal) * 2)
		else:
			cal = (c + b - a - d) * n
			if cal >= k:
				print(k)
			else:
				print(cal + (k - cal) * 2)
	else:
		cost = c - a + d - b
		get = d - a
		if k <= get:
			print(c - b + k)
		else:
			count = cost
			k -= get
			if get * 2 <= cost:
				print(count + k * 2)
			else:
				ma = k // get
				if ma >= n - 1:
					print(cost * n + (k - get * (n - 1)) * 2)
				else:
					count += ma * cost
					k -= ma * get
					print(count + min(k * 2, c - b + k))


from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
srl(int(1000000000.0) + 7)
stack_size(int(100000000.0))

def solve():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		(l1, r1) = map(int, input().split())
		(l2, r2) = map(int, input().split())
		d = min(r1, r2) - max(l1, l2)
		steps = 0
		I = n * (d * (d > 0))
		if n * d * (d > 0) >= k:
			print(0)
			continue
		if d >= 0:
			length = max(r2, r1) - min(l1, l2) - d
			maxpos = length * n
			if maxpos > k - I:
				print(k - I)
			else:
				print(maxpos + (k - I - maxpos) * 2)
		else:
			steps += -d + min(k - I, max(r1, r2) - min(l1, l2))
			I += min(k - I, max(r1, r2) - min(l1, l2))
			n -= 1
			if I < k and n > 0:
				p = min((k - I) // (max(r1, r2) - min(l1, l2)), n)
				steps += p * (max(r1, r2) - min(l1, l2) + abs(d))
				I += p * (max(r1, r2) - min(l1, l2))
				if I < k and p == n:
					steps += (k - I) * 2
				elif I < k:
					method1 = abs(d) + k - I
					method2 = 2 * (k - I)
					steps += min(method1, method2)
			elif n == 0:
				steps += 2 * (k - I)
			print(steps)
Thread(target=solve).start()


import sys
q = int(input())
for i in range(q):
	(n, k) = [int(j) for j in sys.stdin.readline().split()]
	(l1, r1) = [int(j) for j in sys.stdin.readline().split()]
	(l2, r2) = [int(j) for j in sys.stdin.readline().split()]
	if l2 < l1:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if r1 >= r2:
		kol = k - (r2 - l2) * n
		if kol <= 0:
			print(0)
		elif kol <= (r1 - l1 - (r2 - l2)) * n:
			print(kol)
		else:
			print((r1 - l1 - (r2 - l2)) * n + (kol - (r1 - l1 - (r2 - l2)) * n) * 2)
	elif r1 <= l2:
		kol = k
		sep = min(kol // (r2 - l1), n)
		rez = sep * (r2 - l1 + l2 - r1)
		if kol // (r2 - l1) == 0:
			rez += kol % (r2 - l1) + l2 - r1
		elif kol - sep * (r2 - l1) <= l2 - r1:
			rez += (kol - sep * (r2 - l1)) * 2
		elif sep == n:
			rez += (kol - sep * (r2 - l1)) * 2
		else:
			rez += min(kol % (r2 - l1) * 2, kol % (r2 - l1) + l2 - r1)
		print(rez)
	else:
		kol = k - (r1 - l2) * n
		if kol <= 0:
			print(0)
		elif kol <= (r2 - l1 - (r1 - l2)) * n:
			print(kol)
		else:
			print((r2 - l1 - (r1 - l2)) * n + (kol - (r2 - l1 - (r1 - l2)) * n) * 2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = sorted([[int(i) for i in input().split()] for i in range(2)])
	g = s[1][0] - s[0][1]
	ans = int(1e+20)
	if s[1][0] < s[0][1] and s[0][1] > s[1][1]:
		k -= (s[1][1] - s[1][0]) * n
		mX = s[0][1] - s[0][0] - (s[1][1] - s[1][0])
		ans = min(k, mX * n) + max(0, 2 * (k - mX * n))
		print(max(0, ans))
		continue
	if -g * n >= k:
		print(0)
		continue
	mX = s[1][0] - s[0][0] + abs(s[1][1] - s[1][0])
	for i in range(1, n + 1):
		ans = min(ans, g * i + min(mX * i, k) + max(0, 2 * (k - mX * i)))
	assert ans > 0
	print(ans)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import 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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

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

def N():
	return int(input())
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
farr = [1]
ifa = []

def fact(x, mod=0):
	if mod:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr) % mod)
	else:
		while x >= len(farr):
			farr.append(farr[-1] * len(farr))
	return farr[x]

def ifact(x, mod):
	global ifa
	ifa.append(pow(farr[-1], mod - 2, mod))
	for i in range(x, 0, -1):
		ifa.append(ifa[-1] * i % mod)
	ifa = ifa[::-1]

def per(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return fact(i) // fact(i - j)
	return farr[i] * ifa[i - j] % mod

def com(i, j, mod=0):
	if i < j:
		return 0
	if not mod:
		return per(i, j) // fact(j)
	return per(i, j, mod) * ifa[j] % mod

def catalan(n):
	return com(2 * n, n) // (n + 1)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

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

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def GP(it):
	return [(ch, len(list(g))) for (ch, g) in groupby(it)]

class DLN:

	def __init__(self, val):
		self.val = val
		self.pre = None
		self.next = None
t = N()
for i in range(t):
	(n, k) = RL()
	(l1, r1) = RL()
	(l2, r2) = RL()
	if l1 > l2:
		(l1, r1, l2, r2) = (l2, r2, l1, r1)
	if r1 < l2:
		if k <= r2 - l1:
			ans = l2 - r1 + k
		else:
			k -= r2 - l1
			n -= 1
			ans = l2 - l1 + r2 - r1
			(d, m) = divmod(k, r2 - l1)
			if d >= n:
				ans += n * (r2 - r1 + l2 - l1) + (k - n * (r2 - l1)) * 2
			else:
				ans += d * (r2 - r1 + l2 - l1) + min(m * 2, l2 - r1 + m)
	else:
		a = min(r1, r2) - l2
		p = max(r1, r2) - l1
		if n * a >= k:
			ans = 0
		elif n * p >= k:
			ans = k - n * a
		else:
			ans = n * (p - a) + (k - n * p) * 2
	print(ans)