Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x_1, x_2, ..., x_{n}. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.


-----Input-----

The first line of the input contains two integers n and a (1 ≤ n ≤ 100 000,  - 1 000 000 ≤ a ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively.

The second line contains n integers x_1, x_2, ..., x_{n} ( - 1 000 000 ≤ x_{i} ≤ 1 000 000) — coordinates of the checkpoints.


-----Output-----

Print one integer — the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.


-----Examples-----
Input
3 10
1 7 12

Output
7

Input
2 0
11 -10

Output
10

Input
5 0
0 0 1000 0 0

Output
0



-----Note-----

In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.

In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10.

Source: codeforces | Difficulty: MEDIUM
Tags: Sorting

Implementation

Greedy algorithms


Solutions:
(n, a) = map(int, input().split())
L = list(map(int, input().split()))
L.sort()
if n == 1:
	print(0)
elif a >= L[n - 1]:
	print(a - L[1])
elif a <= L[0]:
	print(L[n - 2] - a)
else:
	gauche = min(abs(a - L[0]), abs(L[n - 2] - a)) + L[n - 2] - L[0]
	droite = min(abs(a - L[1]), abs(L[n - 1] - a)) + L[n - 1] - L[1]
	if gauche < droite:
		print(gauche)
	else:
		print(droite)


(n, a) = [int(i) for i in input().split()]
coords = [int(i) for i in input().split()]
coords = sorted(coords)
if n == 1:
	print(0)
else:
	var1 = abs(a - coords[0]) + coords[-2] - coords[0]
	var2 = abs(a - coords[1]) + coords[-1] - coords[1]
	var3 = abs(a - coords[-2]) + coords[-2] - coords[0]
	var4 = abs(a - coords[-1]) + coords[-1] - coords[1]
	print(min(var1, var2, var3, var4))


(n, start) = map(int, input().split())
x = list(map(int, input().split()))
x = sorted(x)
if n == 1:
	print(0)
else:
	print(min(abs(start - x[0]) + abs(x[len(x) - 2] - x[0]), abs(start - x[1]) + abs(x[len(x) - 1] - x[1]), abs(x[len(x) - 1] - start) + abs(x[len(x) - 1] - x[1]), abs(x[len(x) - 2] - start) + abs(x[len(x) - 2] - x[0])))


(n, a) = list(map(int, input().split()))
x = list(map(int, input().split()))
x.sort()
res = 2 ** 30

def solve(a, i, j):
	if a >= x[j]:
		return a - x[i]
	elif a <= x[i]:
		return x[j] - a
	else:
		sumLeft = (a - x[i]) * 2 + x[j] - a
		sumRight = (x[j] - a) * 2 + a - x[i]
		return min(sumLeft, sumRight)
if n <= 1:
	res = 0
elif n == 2:
	for value in x:
		res = min(res, abs(value - a))
elif a <= x[0]:
	res = x[len(x) - 2] - a
elif a >= x[len(x) - 1]:
	res = a - x[1]
else:
	leftValue = solve(a, 1, len(x) - 1)
	rightValue = solve(a, 0, len(x) - 2)
	res = min(leftValue, rightValue)
print(res)


inp1 = input()
inp11 = inp1.split(' ')
n = int(inp11[0])
a = int(inp11[1])
inp2 = input()
inp21 = inp2.split(' ')
x0 = []
for i in inp21:
	j = int(i)
	x0.append(j)
x = sorted(x0)

def f(x):
	if x > 0:
		return x
	else:
		return 0
if n > 1:
	len1 = 2 * f(a - x[0]) + f(x[n - 2] - a)
	len2 = 2 * f(x[n - 2] - a) + f(a - x[0])
	len3 = 2 * f(a - x[1]) + f(x[n - 1] - a)
	len4 = 2 * f(x[n - 1] - a) + f(a - x[1])
	print(min(len1, len2, len3, len4))
else:
	print(0)


(n, a) = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort()
if len(b) == 1:
	print(0)
elif len(b) == 2:
	print(min(abs(a - b[0]), abs(a - b[1])))
elif a < b[0]:
	print(b[-2] - a)
elif a > b[-1]:
	print(a - b[1])
elif a == b[0]:
	print(b[-2] - a)
elif a == b[-1]:
	print(a - b[1])
elif len(b) == 3:
	if a < b[1]:
		print(min(a - b[0] - b[0] + b[1], b[1] - a + b[1] - a + a - b[0], b[2] - a))
	elif a > b[1]:
		print(min(a - b[0], a - b[1] + a - b[1] + b[2] - a, b[2] - a + b[2] - a + a - b[1]))
	else:
		print(min(a - b[0], b[2] - a))
elif a < b[1]:
	print(min(a - b[0] + a - b[0] + b[-2] - a, b[-2] - a + b[-2] - a + a - b[0], b[-1] - a))
elif a > b[-2]:
	print(min(a - b[0], b[-1] - a + b[-1] - a + a - b[1], a - b[1] + a - b[1] + b[-1] - a))
else:
	print(min(a - b[1] + a - b[1] + b[-1] - a, b[-1] - a + b[-1] - a + a - b[1], a - b[0] + a - b[0] + b[-2] - a, b[-2] - a + b[-2] - a + a - b[0]))


I = lambda : map(int, input().split())
(n, a) = I()
x = sorted(I())
s = abs
print(0 if n == 1 else min(min(s(a - x[1]), s(a - x[-1])) + x[-1] - x[1], min(s(a - x[-2]), s(a - x[0])) + x[-2] - x[0]))


class CodeforcesTask709BSolution:

	def __init__(self):
		self.result = ''
		self.n_a = []
		self.points = []

	def read_input(self):
		self.n_a = [int(x) for x in input().split(' ')]
		self.points = [int(x) for x in input().split(' ')]

	def process_task(self):
		right_points = [x - self.n_a[1] for x in self.points if x >= self.n_a[1]]
		left_points = [self.n_a[1] - x for x in self.points if x < self.n_a[1]]
		right_points.sort()
		left_points.sort()
		if len(right_points) > 0:
			full_right = right_points[-1]
		else:
			full_right = 0
		if len(left_points) > 0:
			full_left = left_points[-1]
		else:
			full_left = 0
		if len(right_points) > 1:
			semi_right = right_points[-2]
		else:
			semi_right = 0
		if len(left_points) > 1:
			semi_left = left_points[-2]
		else:
			semi_left = 0
		variant1 = semi_left * 2 + full_right
		variant2 = semi_right * 2 + full_left
		variant3 = semi_left + full_right * 2
		variant4 = semi_right + full_left * 2
		self.result = str(min(variant1, variant2, variant3, variant4))

	def get_result(self):
		return self.result
Solution = CodeforcesTask709BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, a) = map(int, input().split())
l = input().split()
l = [int(i) for i in l]
if n == 1:
	print(0)
elif n == 2:
	if abs(a - l[0]) < abs(a - l[1]):
		print(abs(a - l[0]))
	else:
		print(abs(a - l[1]))
else:
	ma = max(l)
	mi = min(l)
	l.remove(ma)
	max2 = max(l)
	l.append(ma)
	l.remove(mi)
	min2 = min(l)
	l.append(mi)
	if a > ma:
		print(a - min2)
	elif a < mi:
		print(max2 - a)
	else:
		ans = list()
		ans.append(abs(max2 - a) + max2 - mi)
		ans.append(abs(a - mi) + max2 - mi)
		ans.append(ma - min2 + abs(ma - a))
		ans.append(ma - min2 + abs(a - min2))
		while min(ans) < 0:
			ans.remove(min(ans))
		print(min(ans))


from sys import stdin

def main():
	(n, a) = stdin_get_ints_from_line()
	x = stdin_get_ints_list_from_line()
	x.sort()
	if n == 1:
		print(0)
		exit()
	print(min(get_result(a, x[1], x[-1]), get_result(a, x[0], x[-2])))

def get_result(a, left, right):
	if left < a < right:
		if abs(a - left) < abs(a - right):
			return abs(a - left) * 2 + abs(a - right)
		else:
			return abs(a - left) + abs(a - right) * 2
	if a <= left:
		return abs(a - right)
	if a >= right:
		return abs(a - left)

def stdin_get_ints_from_line():
	return (int(x) for x in stdin.readline().strip().split(' '))

def stdin_get_ints_list_from_line():
	return list((int(x) for x in stdin.readline().strip().split(' ')))

def stdin_get_string_from_line():
	return stdin.readline().strip()
main()


(n, a) = map(int, input().split())
x = list(map(int, input().split()))
if n == 1:
	print(0)
elif n == 2:
	if abs(x[0] - a) < abs(x[1] - a):
		print(abs(x[0] - a))
	else:
		print(abs(x[1] - a))
elif n == 3:
	x = sorted(x)
	_min = x[0]
	_mid = x[1]
	_max = x[2]
	ans = abs(_max - a) * 2 + abs(_mid - a)
	if abs(_max - a) + abs(_mid - a) * 2 < ans:
		ans = abs(_max - a) + abs(_mid - a) * 2
	if abs(_min - a) * 2 + abs(_mid - a) < ans:
		ans = abs(_min - a) * 2 + abs(_mid - a)
	if abs(_min - a) + abs(_mid - a) * 2 < ans:
		ans = abs(_min - a) + abs(_mid - a) * 2
	print(ans)
else:
	_max = -10 ** 6 + 1
	_nmax = -10 ** 6 + 1
	_min = 10 ** 6 + 1
	_nmin = 10 ** 6 + 1
	for i in range(n):
		if x[i] >= _max:
			_nmax = _max
			_max = x[i]
		elif x[i] > _nmax:
			_nmax = x[i]
		if x[i] <= _min:
			_nmin = _min
			_min = x[i]
		elif x[i] < _nmin:
			_nmin = x[i]
	if _nmax > a > _nmin:
		ans = abs(_max - a) * 2 + abs(_nmin - a)
		if abs(_max - a) + abs(_nmin - a) * 2 < ans:
			ans = abs(_max - a) + abs(_nmin - a) * 2
		if abs(_nmax - a) * 2 + abs(_min - a) < ans:
			ans = abs(_nmax - a) * 2 + abs(_min - a)
		if abs(_nmax - a) + abs(_min - a) * 2 < ans:
			ans = abs(_nmax - a) + abs(_min - a) * 2
		print(ans)
	elif _max > a >= _nmax:
		ans = abs(_max - a) * 2 + abs(_nmin - a)
		if abs(_min - a) < ans:
			ans = abs(_min - a)
		if abs(_nmin - a) * 2 + abs(_max - a) < ans:
			ans = abs(_nmin - a) * 2 + abs(_max - a)
		print(ans)
	elif a >= _max:
		print(abs(a - _nmin))
	elif a <= _min:
		print(abs(a - _nmax))
	else:
		ans = abs(_min - a) * 2 + abs(_nmax - a)
		if abs(_max - a) < ans:
			ans = abs(_max - a)
		if abs(_nmax - a) * 2 + abs(_min - a) < ans:
			ans = abs(_nmax - a) * 2 + abs(_min - a)
		print(ans)


(n, a) = [int(i) for i in input().split()]
points = [int(i) for i in input().split()]
right = None
right2 = None
left = None
left2 = None
for point in points:
	if right is None:
		right = point
	elif point > right:
		right2 = right
		right = point
	elif right2 is None or point > right2:
		right2 = point
	if left is None:
		left = point
	elif point < left:
		left2 = left
		left = point
	elif left2 is None or point < left2:
		left2 = point
if n == 1:
	print(0)
elif a >= right:
	print(a - left2)
elif a <= left:
	print(right2 - a)
else:
	x1 = abs(a - left) + abs(right2 - left)
	x2 = abs(a - left2) + abs(right - left2)
	x3 = abs(right - a) + abs(right - left2)
	x4 = abs(right2 - a) + abs(right2 - left)
	print(min((x1, x2, x3, x4)))


(n, a) = map(int, input().split())
l = sorted(list(map(int, input().split())))
if len(l) == 1:
	print(0)
	exit()
m = float('inf')
if len(l) == 2:
	print(min(abs(a - l[0]), abs(a - l[1])))
	exit()
for i in [0, n - 1]:
	if i == 0:
		x = abs(l[-1] - l[1]) + min(abs(a - l[1]), abs(a - l[-1]))
	else:
		x = abs(l[-2] - l[0]) + min(abs(a - l[0]), abs(a - l[-2]))
	if x < m:
		m = x
print(m)


(n, d) = map(int, input().split())
a = list(map(int, input().strip().split()))
a.sort()
ans = 0
if n == 1:
	ans = 0
elif n <= 2:
	ans = min(abs(a[0] - d), abs(a[n - 1] - d))
else:
	ans = a[n - 2] - a[0] + min(abs(a[0] - d), abs(a[n - 2] - d))
	ans = min(ans, a[n - 1] - a[1] + min(abs(a[1] - d), abs(a[n - 1] - d)))
print(ans)


(n, a) = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
s.sort()
t = len(s) - 1
for i in range(n):
	if s[i] > a:
		t = i - 1
		break
if n == 1:
	print(0)
elif t == -1:
	print(abs(s[-2] - a))
elif t == n - 1:
	print(abs(a - s[1]))
elif t == 0:
	print(min(abs(s[-1] - a), abs(s[-2] - s[0] + a - s[0]), abs(s[-2] - s[0] + s[-2] - a)))
elif t == n - 2:
	print(min(abs(a - s[0]), abs(s[-1] - s[1] + s[-1] - a), abs(s[-1] - s[1] + a - s[1])))
else:
	ans = min(abs(s[-1] - s[1] + a - s[1]), abs(s[-1] - s[1] + s[-1] - a), abs(s[-2] - s[0] + a - s[0]), abs(s[-2] - s[0] + s[-2] - a))
	print(ans)


(n, a) = list(map(int, input().split()))
points = list(map(int, input().split()))
points.sort()
if n == 1:
	print(0)
	exit()
ans1 = abs(a - points[0]) + abs(points[0] - points[-2])
ans2 = abs(a - points[1]) + abs(points[1] - points[-1])
ans3 = abs(a - points[-1]) + abs(points[-1] - points[1])
ans4 = abs(a - points[-2]) + abs(points[-2] - points[0])
ans = min(ans1, ans2, ans3, ans4)
print(ans)


import sys

def read_data():
	(n, x0) = list(map(int, next(sys.stdin).split()))
	checkpoints = list(map(int, next(sys.stdin).split()))
	return (x0, checkpoints)

def solve(x0, checkpoints):
	n = len(checkpoints)
	checkpoints.sort()
	if n == 1:
		return 0
	case0 = abs(x0 - checkpoints[1]) + abs(checkpoints[n - 1] - checkpoints[1])
	case1 = abs(x0 - checkpoints[n - 2]) + abs(checkpoints[0] - checkpoints[n - 2])
	case2 = abs(x0 - checkpoints[0]) + abs(checkpoints[n - 2] - checkpoints[0])
	case3 = abs(x0 - checkpoints[n - 1]) + abs(checkpoints[1] - checkpoints[n - 1])
	return min(case0, case1, case2, case3)

def __starting_point():
	(x0, checkpoints) = read_data()
	length = solve(x0, checkpoints)
	print(length)
__starting_point()


import sys
(n, a) = map(int, input().split())
x = list(map(int, sys.stdin.read().split()))
x.sort()

def dist(x, a):
	assert len(x) >= 2
	if x[0] < a < x[-1]:
		return min(a - x[0], x[-1] - a) + x[-1] - x[0]
	else:
		return max(abs(x[0] - a), abs(x[-1] - a))
if n == 1:
	print(0)
elif n == 2:
	print(min(abs(a - x[0]), abs(a - x[1])))
else:
	x0 = x.pop(0)
	d0 = dist(x, a)
	x.insert(0, x0)
	x.pop(-1)
	d1 = dist(x, a)
	print(min(d0, d1))


def main():
	from itertools import permutations
	(n, a) = map(int, input().split())
	l = sorted(map(int, input().split()))
	if n > 4:
		del l[2:-2]
	res = [sum((abs(u - v) for (u, v) in zip(t, [a, *t]))) for t in permutations(l, len(l) - 1)]
	print(min(res) if res else 0)
main()


(n, a) = list(map(int, input().split()))
line = list(map(int, input().split()))
line.sort()
if len(line) == 1:
	print(0)
else:
	r1 = abs(line[-1] - line[1])
	otvet1 = min(abs(a - line[1]), abs(a - line[-1])) + r1
	r2 = abs(line[-2] - line[0])
	otvet2 = min(abs(a - line[-2]), abs(a - line[0])) + r2
	print(min(otvet1, otvet2))


def calc_dist(coords, a):
	if len(coords) == 0:
		return 0
	if a > coords[-1]:
		return a - coords[0]
	if a < coords[0]:
		return coords[-1] - a
	return coords[-1] - coords[0] + min(a - coords[0], coords[-1] - a)
(n, a) = list(map(int, input().split()))
coords = list(map(int, input().split()))
coords.sort()
print(min(calc_dist(coords[:-1], a), calc_dist(coords[1:], a)))


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 collections import defaultdict
from math import ceil, floor, sqrt, log2, gcd
from heapq import heappush, heappop
import sys
abc = 'abcdefghijklmnopqrstuvwxyz'
(n, a) = map(int, input().split())
arr = list(map(int, input().split()))
arr.append(float('inf'))
arr.append(-float('inf'))
arr.sort()
a1 = []
a2 = []
for i in arr:
	if i <= a:
		a1.append(i)
	else:
		a2.append(i)
if n == 1:
	print(0)
elif n == 2:
	print(min(abs(a - arr[1]), abs(a - arr[2])))
elif len(a1) == 1:
	print(abs(a - a2[-3]))
elif len(a2) == 1:
	print(abs(a - a1[2]))
elif len(a1) > 2 and len(a2) > 2:
	m1 = min(abs(a - a1[1]) + abs(a1[1] - a2[-3]), abs(a - a2[-3]) + abs(a2[-3] - a1[1]))
	m2 = min(abs(a - a1[2]) + abs(a1[2] - a2[-2]), abs(a - a2[-2]) + abs(a2[-2] - a1[2]))
	print(min(m1, m2))
elif len(a1) > 2 and len(a2) <= 2:
	m1 = min(abs(a - a1[1]), float('inf'))
	m2 = min(abs(a - a1[2]) + abs(a1[2] - a2[-2]), abs(a - a2[-2]) + abs(a2[-2] - a1[2]))
	print(min(m1, m2))
elif len(a1) <= 2 and len(a2) > 2:
	m1 = min(abs(a - a2[-2]), float('inf'))
	m2 = min(abs(a - a2[-3]) + abs(a1[1] - a2[-3]), abs(a - a1[1]) + abs(a2[-3] - a1[1]))
	print(min(m1, m2))


(n, a) = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
x.sort()

def f(l, r):
	d1 = abs(x[l] - a)
	d2 = abs(x[r] - a)
	if l == r:
		return min(d1, d2)
	if (x[l] <= a) == (x[r] <= a):
		return max(d1, d2)
	return min(d1, d2) * 2 + max(d1, d2)
if n == 1:
	print(0)
else:
	print(min(f(0, n - 2), f(1, n - 1)))


def not_left(a, x):
	right = abs(x[-1] - a)
	left = abs(a - min(a, x[1]))
	return min(left, right) * 2 + max(left, right)

def not_right(a, x):
	right = abs(max(a, x[-2]) - a)
	left = abs(a - x[0])
	return min(left, right) * 2 + max(left, right)
(n, a) = map(int, input().split())
x = sorted(list(map(int, input().split())))
if n == 1:
	print(0)
elif n == 2:
	if a <= x[0]:
		print(abs(x[0] - a))
	elif a >= x[1]:
		print(abs(a - x[1]))
	else:
		left = abs(a - x[0])
		right = abs(x[1] - a)
		print(min(left, right))
elif a >= x[-1]:
	print(abs(a - x[1]))
elif a <= x[0]:
	print(abs(x[-2] - a))
else:
	print(min(not_right(a, x), not_left(a, x)))


(N, A) = list(map(int, input().split()))
X = list(map(int, input().split()))
X.sort()
if N == 1:
	print(0)
	return
elif N == 2:
	print(min(abs(X[0] - A), abs(X[1] - A)))
	return
ans = int(1e+18)
ans = min(ans, abs(X[N - 2] - X[0]) + min(abs(A - X[0]), abs(A - X[N - 2])))
ans = min(ans, abs(X[N - 1] - X[1]) + min(abs(A - X[1]), abs(A - X[N - 1])))
print(ans)


(n, p) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if n == 1:
	print(0)
elif n == 2:
	print(min(abs(a[0] - p), abs(a[1] - p)))
else:
	a1 = min(abs(a[0] - p), abs(a[-2] - p)) + abs(a[0] - a[-2])
	a2 = min(abs(a[1] - p), abs(a[-1] - p)) + abs(a[1] - a[-1])
	print(min(a1, a2))


read = lambda : map(int, input().split())
(n, a) = read()
x = sorted(read())
result = 0
if n > 1:
	r1 = abs(a - x[0]) + abs(x[0] - x[n - 2])
	r2 = abs(a - x[1]) + abs(x[1] - x[n - 1])
	r3 = abs(a - x[n - 2]) + abs(x[n - 2] - x[0])
	r4 = abs(a - x[n - 1]) + abs(x[n - 1] - x[1])
	result = min(r1, r2, r3, r4)
print(result)


(n, a) = list(map(int, input().split()))
x = list(map(int, input().split()))

def visit(a, b, c):
	if a <= b:
		return c - a
	if a >= c:
		return a - b
	return c - b + min(c - a, a - b)
x.sort()
if n <= 1:
	print(0)
elif a <= x[0]:
	print(x[-2] - a)
elif a >= x[-1]:
	print(a - x[1])
else:
	print(min(visit(a, x[0], x[-2]), visit(a, x[1], x[-1])))


(n, a) = map(int, input().split())
l = [int(x) for x in input().split()]
if n == 1:
	print(0)
	exit()
l.sort()
ans1 = l[-2] - l[0] + min(abs(a - l[-2]), abs(a - l[0]))
ans2 = l[-1] - l[1] + min(abs(a - l[-1]), abs(a - l[1]))
print(min(ans1, ans2))


import sys
from decimal import *

def main():
	(n, a) = map(int, sys.stdin.readline().split())
	x = list(map(int, sys.stdin.readline().split()))
	if n == 1:
		print(0)
		return
	min1 = min(x)
	max1 = max(x)
	x.remove(min1)
	x.remove(max1)
	if n == 2:
		if abs(a - min1) > abs(a - max1):
			print(abs(a - max1))
		else:
			print(abs(a - min1))
		return
	min2 = min(x)
	max2 = max(x)
	v1 = 0
	(l1, r1) = (0, 0)
	if a > min2:
		l1 = abs(a - min2)
	if a < max1:
		r1 = abs(a - max1)
	if l1 < r1:
		v1 = 2 * l1 + r1
	else:
		v1 = 2 * r1 + l1
	v2 = 0
	(l2, r2) = (0, 0)
	if a > min1:
		l2 = abs(a - min1)
	if a < max2:
		r2 = abs(a - max2)
	if l2 < r2:
		v2 = 2 * l2 + r2
	else:
		v2 = 2 * r2 + l2
	if v1 < v2:
		print(v1)
	else:
		print(v2)
main()


from math import *
[n, a] = [int(x) for x in input().split()]
if n == 1:
	print(0)
	return
points = [int(x) for x in input().split()]
points.sort()
dis1 = abs(points[n - 2] - points[0])
dis2 = abs(points[n - 1] - points[1])
to0 = abs(a - points[0])
to1 = abs(a - points[1])
ton = abs(a - points[n - 1])
ton2 = abs(a - points[n - 2])
print(min(to0 + dis1, to1 + dis2, ton + dis2, ton2 + dis1))


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

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

def rl():
	return list(map(int, input().split()))
(n, a) = rm()
b = rl()
b.sort()
if n == 1:
	print(0)
elif n == 2:
	print(min(abs(a - b[0]), abs(a - b[1])))
elif b[0] >= a:
	print(abs(a - b[-2]))
elif b[-1] <= a:
	print(abs(a - b[1]))
else:
	cal = 0
	for i in b:
		if i <= a:
			cal += 1
		else:
			break
	s = abs(a - b[0])
	e = abs(b[-1] - a)
	if cal == 1:
		es = abs(b[-2] - a)
		print(min(e, s + es + min(s, es)))
	elif n - cal == 1:
		se = abs(a - b[1])
		print(min(s, e + se + min(e, se)))
	else:
		es = abs(b[-2] - a)
		se = abs(a - b[1])
		print(min(s + es + min(s, es), e + se + min(e, se)))


(n, a) = map(int, input().split())
x = sorted([int(i) for i in input().split()])
if len(x) == 1:
	print(0)
elif len(x) == 2:
	print(min(abs(x[0] - a), abs(x[1] - a)))
else:
	print(min(x[-2] - x[0] + min(abs(x[0] - a), abs(x[-2] - a)), x[-1] - x[1] + min(abs(x[1] - a), abs(x[-1] - a))))


(n, a) = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n == 1:
	print(0)
	exit()
if n == 2:
	print(min(abs(x[0] - a), abs(x[1] - a)))
	exit()
x1 = [[x[1], x[n - 1]], [x[0], x[n - 2]]]
ans = 10 ** 10
for i in range(2):
	if x1[i][0] <= a <= x1[i][1]:
		ans = min(ans, x1[i][1] - a + x1[i][1] - x1[i][0], a - x1[i][0] + x1[i][1] - x1[i][0])
	elif a < x1[i][0]:
		ans = min(ans, x1[i][1] - a)
	else:
		ans = min(ans, a - x1[i][0])
print(ans)


(n, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
if len(a) <= 1:
	ans = 0
else:
	a = sorted([x] + a)
	ind = a.index(x)
	if ind == 0:
		ans = a[-2] - a[0]
	elif ind == len(a) - 1:
		ans = a[-1] - a[1]
	else:
		ans = min(a[ind] + a[-2] - 2 * a[0], 2 * a[-2] - a[ind] - a[0], a[ind] + a[-1] - 2 * a[1], 2 * a[-1] - a[ind] - a[1])
print(ans)


def solve():
	(n, a) = list(map(int, input().split()))
	x = list(map(int, input().split()))
	x.append(a)
	x.sort()
	bi = 0
	for i in range(n + 1):
		if x[i] == a:
			bi = i
			break
	ans = float('inf')
	for s in range(2):
		l = 0
		r = 0
		ok = x[s] == a
		for i in range(s + 1, s + n):
			ok = ok or x[i] == a
			if i <= bi:
				l += x[i] - x[i - 1]
			else:
				r += x[i] - x[i - 1]
		if ok:
			ans = min(ans, min(l, r) * 2 + max(l, r))
	print(ans)

def main():
	solve()

def __starting_point():
	main()
__starting_point()


(n, a) = map(int, input().split())
x = sorted(map(int, input().split()))
if n == 1:
	print(0)
else:
	status1 = min(abs(a - x[0]), abs(a - x[-2])) + abs(x[-2] - x[0])
	status2 = min(abs(a - x[1]), abs(a - x[-1])) + abs(x[-1] - x[1])
	print(min(status1, status2))


import sys
input = sys.stdin.readline
(n, a) = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
if n == 1:
	print(0)
	exit()
ans = abs(a - x[0]) + abs(x[0] - x[n - 2])
ans = min(ans, abs(a - x[n - 2]) + abs(x[n - 2] - x[0]))
ans = min(ans, abs(a - x[1]) + abs(x[1] - x[-1]))
ans = min(ans, abs(a - x[-1]) + abs(x[-1] - x[1]))
print(ans)


(n, pos) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

def solve(left, right):
	if pos < a[left]:
		return a[right] - pos
	if pos > a[right]:
		return pos - a[left]
	return min(2 * (pos - a[left]) + a[right] - pos, 2 * (a[right] - pos) + pos - a[left])
if n == 1:
	print(0)
else:
	print(min(solve(0, n - 2), solve(1, n - 1)))


(n, a) = map(int, input().split())
x = list(map(int, input().split()))
if n == 1:
	print(0)
elif n == 2:
	print(min(abs(a - x[0]), abs(a - x[1])))
else:
	num = float('inf')
	x = sorted(x)
	if a <= x[0]:
		num = min(num, abs(a - x[n - 2]))
	elif x[n - 1] <= a:
		num = min(num, abs(a - x[1]))
	num = min(abs(x[1] - a) + abs(x[1] - x[n - 1]), num)
	num = min(abs(x[n - 2] - a) + abs(x[0] - x[n - 2]), num)
	num = min(abs(x[n - 1] - a) + abs(x[n - 1] - x[1]), num)
	num = min(abs(x[0] - a) + abs(x[n - 2] - x[0]), num)
	print(num)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
t = list(map(int, input().split()))
t.sort()
if n <= 1:
	print(0)
else:
	ans = 9999999999999999
	for j in range(n):
		if j + (n - 2) < n:
			ans = min(ans, t[j + n - 2] - t[j] + abs(m - t[j]))
		x = n - 1 - j
		if x - (n - 2) >= 0:
			ans = min(ans, abs(t[x] - m) + (t[x] - t[x - (n - 2)]))
	print(ans)


def f(a, l, r):
	(dl, dr) = (a - l, r - a)
	return 2 * max(0, min(dl, dr)) + max(dl, dr)
(n, a) = map(int, input().split())
if n == 1:
	print(0)
	exit()
x = sorted(map(int, input().split()))
print(min(f(a, x[0], x[-2]), f(a, x[1], x[-1])))


(n, a) = map(int, input().split())
ip = list(map(int, input().split()))
try:
	if a not in ip:
		ip.append(a)
		n += 1
	ip = sorted(ip)
	k = ip.index(a)
	l1 = 0
	l2 = 0
	for i in range(k):
		l1 += ip[i + 1] - ip[i]
	for i in range(k, n - 1):
		l2 += ip[i + 1] - ip[i]
	n1 = ip[1] - ip[0]
	n2 = ip[-1] - ip[-2]
	if l1 == 0:
		print(l2 - n2)
	elif l2 == 0:
		print(l1 - n1)
	else:
		print(min(l1 - n1 + 2 * l2, 2 * l1 - 2 * n1 + l2, l2 - n2 + 2 * l1, 2 * l2 - 2 * n2 + l1))
except:
	print(0)


(n, a) = map(int, input().split())
x = [*map(int, input().split())]
x.sort()
if n == 1:
	print(0)
	exit(0)
ind = 0
dist1 = min(abs(a - x[n - 1]), abs(a - x[1])) + abs(x[n - 1] - x[1])
dist2 = min(abs(a - x[0]), abs(a - x[n - 2])) + abs(x[0] - x[n - 2])
print(min(dist1, dist2))


import traceback
import math
from collections import defaultdict
from functools import lru_cache
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 geti():
	return int(input())

def gets():
	return input()

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

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

def get2d(nrows, ncols, n=0):
	return [[n] * ncols for r in range(nrows)]
inf = float('inf')
mod = 10 ** 9 + 7

def main():
	(N, s) = getil()
	a = getil()
	if N == 1:
		return 0
	a.sort()
	(diffl, diffr) = (a[-2] - a[0], a[-1] - a[1])
	dm = lambda x, y, z: min(abs(x - y), abs(x - z))
	return min(dm(s, a[0], a[-2]) + diffl, dm(s, a[1], a[-1]) + diffr)
try:
	ans = main()
	print(ans)
except Exception as e:
	print(e)
	traceback.print_exc()


from math import *
(n, a) = (int(z) for z in input().split())
x = [int(z) for z in input().split()]
x.sort()
if n == 1:
	print(0)
else:
	print(int(min(fabs(a - x[1]) + fabs(x[-1] - x[1]), fabs(a - x[-1]) + fabs(x[-1] - x[1]), fabs(a - x[-2]) + fabs(x[-2] - x[0]), fabs(a - x[0]) + fabs(x[-2] - x[0]))))


(n, a) = list(map(int, input().split()))
x = list(sorted(map(int, input().split())))
print(min(abs(x[n - 1] - a) + x[n - 1] - x[1], abs(x[n - 2] - a) + x[n - 2] - x[0], abs(x[0] - a) + x[n - 2] - x[0], abs(x[1] - a) + x[n - 1] - x[1]) if n > 1 else 0)


import sys, time
startTime = time.time()
(n, a) = map(int, sys.stdin.readline().split())
x = sys.stdin.readline().split()
x = [int(num) for num in x]

def minDistance(n, a, x):
	x.sort()
	ans = 0
	if n == 1:
		ans = 0
	elif n == 2:
		ans = abs(x[0] - a)
		tmpAns = abs(x[1] - a)
		if tmpAns < ans:
			ans = tmpAns
	elif a <= x[0]:
		ans = abs(a - x[-2])
	elif a >= x[-1]:
		ans = abs(a - x[1])
	else:
		tmp_dist = [abs(a - x[-2]) + abs(x[-2] - x[0]), abs(a - x[-1]) + abs(x[-1] - x[1]), abs(a - x[1]) + abs(x[1] - x[-1]), abs(a - x[0]) + abs(x[0] - x[-2])]
		ans = min(tmp_dist)
	return ans

def doTest():
	assert minDistance(3, 10, [1, 7, 12]) == 7
	assert minDistance(2, 0, [11, -10]) == 10
	assert minDistance(5, 0, [0, 0, 1000, 0, 0]) == 0
print(minDistance(n, a, x))


read = lambda : map(int, input().split())
(n, a) = read()
x = sorted(read())
if n == 1:
	print(0)
	exit()
c = [float('inf')] * 10
c[0] = abs(a - x[1]) + abs(x[-1] - x[1])
c[1] = abs(a - x[0]) + abs(x[-2] - x[0])
c[2] = abs(a - x[-1]) + abs(x[-1] - x[1])
c[3] = abs(a - x[-2]) + abs(x[-2] - x[0])
ans = min(c)
print(ans)


(n, a) = map(int, input().split())
x = sorted(list(map(int, input().split())))
l = len(x)
if n == 1:
	print(0)
	exit()
if n == 2:
	if a <= x[0]:
		print(x[0] - a)
		exit()
	if a >= x[1]:
		print(x[1] - a)
		exit()
	else:
		print(min(a - x[0], x[1] - a))
		exit()
elif x[1] <= a <= x[l - 2]:
	mi = mi2 = mi1 = 0
	mi1 = min(abs(a - x[1]), abs(x[l - 1] - a)) * 2 + max(abs(a - x[1]), abs(x[l - 1] - a))
	mi2 = min(abs(a - x[0]), abs(x[l - 2] - a)) * 2 + max(abs(a - x[0]), abs(x[l - 2] - a))
	mi = min(mi1, mi2)
elif x[0] < a < x[1]:
	mi = mi2 = mi1 = 0
	mi1 = x[l - 1] - a
	mi2 = min(abs(a - x[0]), abs(x[l - 2] - a)) * 2 + max(abs(a - x[0]), abs(x[l - 2] - a))
	mi = min(mi1, mi2)
elif x[l - 2] < a < x[l - 1]:
	mi = mi2 = mi1 = 0
	mi1 = min(abs(a - x[1]), abs(x[l - 1] - a)) * 2 + max(abs(a - x[1]), abs(x[l - 1] - a))
	mi2 = a - x[0]
	mi = min(mi1, mi2)
elif a <= x[0]:
	mi = x[l - 2] - a
else:
	mi = a - x[1]
print(mi)


(n, a) = list(map(int, input().split(' ')))
x = list(map(int, input().split(' ')))
(MAX, MIN) = (1000000, -1000000)
(r1, r2, l1, l2) = (MIN - 1, MIN - 1, MAX + 1, MAX + 1)
for xi in x:
	if xi <= l1:
		l2 = l1
		l1 = xi
	elif xi < l2:
		l2 = xi
	if xi >= r1:
		r2 = r1
		r1 = xi
	elif xi > r2:
		r2 = xi
if n == 1:
	print(0)
else:
	print(min(r1 - l2 + abs(a - r1), r1 - l2 + abs(a - l2), r2 - l1 + abs(a - r2), r2 - l1 + abs(a - l1)))


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

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 divs(n, start=2):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors
import sys
inf = float('inf')
mod = 1000000007

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

def intt():
	return map(int, sys.stdin.readline().split())
from bisect import bisect_left
import sys
(n, k) = map(int, sys.stdin.readline().split())
arr = list(map(int, sys.stdin.readline().split()))
arr.sort()
if n == 1:
	print(0)
	exit()
elif n == 2:
	if k > arr[1]:
		print(k - arr[1])
		exit()
	elif k < arr[0]:
		print(arr[0] - k)
		exit()
	else:
		print(min(k - arr[0], arr[1] - k))
		exit()
fr = arr[1] - arr[0] if k >= arr[1] else k - arr[0]
ba = arr[-1] - arr[-2] if k <= arr[-2] else arr[-1] - k
if k <= arr[0]:
	print(abs(arr[-2] - k))
elif k >= arr[-1]:
	print(abs(k - arr[1]))
else:
	d1 = k - arr[0]
	d2 = arr[-1] - k
	var = 2 * d1
	var2 = 0
	if arr[-2] > k:
		var += arr[-2] - k
		var2 = 2 * (arr[-2] - k)
	v1 = min(var, var2 + d1)
	var = 2 * d2
	var2 = 0
	if k > arr[1]:
		var += k - arr[1]
		var2 += 2 * (k - arr[1])
	v2 = min(var, var2 + d2)
	print(min(v1, v2))


(n, a) = map(int, input().split())
coords = list(map(int, input().split()))
coords.sort()

def length(a, b):
	if a >= b:
		return a - b
	else:
		return b - a
ans = 0
if n == 1:
	ans = 0
elif a == coords[0]:
	ans = length(coords[0], coords[-2])
elif a == coords[-1]:
	ans = length(coords[-1], coords[1])
else:
	sol1 = length(a, coords[0]) + length(coords[0], coords[-2])
	sol2 = length(a, coords[1]) + length(coords[1], coords[-1])
	sol3 = length(a, coords[-1]) + length(coords[-1], coords[1])
	sol4 = length(a, coords[-2]) + length(coords[-2], coords[0])
	ans = min(sol1, sol2, sol3, sol4)
print(ans)


from collections import defaultdict
import sys, os, math

def solve(arr, a):
	if a <= arr[0]:
		return arr[n - 2] - a
	elif a >= arr[n - 2]:
		return a - arr[0]
	return min(2 * (arr[n - 2] - arr[0]) - abs(a - arr[0]), 2 * (arr[n - 2] - arr[0]) - abs(arr[n - 2] - a))
(n, aa) = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
if n == 1:
	print(0)
else:
	print(min(solve(A[:n - 1], aa), solve(A[1:], aa)))


(n, a) = map(int, input().split())
checkpoints = list(map(int, input().split()))
checkpoints.append(a)
checkpoints.sort()
len_new = len(checkpoints)
dis_to_first = abs(a - checkpoints[0])
dis_to_first_after = abs(a - checkpoints[1])
dis_to_last = abs(a - checkpoints[len_new - 1])
dis_to_last_before = abs(a - checkpoints[len_new - 2])
dis = [None] * 4
dis[0] = dis_to_first * 2 + dis_to_last_before
dis[1] = dis_to_first_after * 2 + dis_to_last
dis[2] = dis_to_last * 2 + dis_to_first_after
dis[3] = dis_to_last_before * 2 + dis_to_first
print(min(dis))


(n, a) = map(int, input().split())
x = list(map(int, input().split()))
x_left = [xi for xi in x if xi <= a]
x_right = [xi for xi in x if xi > a]
x_left.sort(key=lambda el: -1 * el)
x_right.sort()
dist = 1000000000
if n == 1:
	dist = 0
for i in range(n):
	if i > len(x_left) or n - i - 2 > len(x_right) - 1 or n - i - 2 < 0:
		continue
	elif i == 0:
		dist_curr = x_right[n - i - 2] - a
		if dist_curr < dist:
			dist = dist_curr
	else:
		dist_curr = 2 * (a - x_left[i - 1]) + (x_right[n - i - 2] - a)
		if dist_curr < dist:
			dist = dist_curr
for i in range(n):
	if i > len(x_right) or n - i - 2 > len(x_left) - 1 or n - i - 2 < 0:
		continue
	elif i == 0:
		dist_curr = a - x_left[n - i - 2]
		if dist_curr < dist:
			dist = dist_curr
	else:
		dist_curr = 2 * (x_right[i - 1] - a) + (a - x_left[n - i - 2])
		if dist_curr < dist:
			dist = dist_curr
print(dist)


(n, a) = [int(i) for i in input().split()]
coords = sorted((int(i) for i in input().split()))
dist = [0] * 4
cur = [a] * 4
for i in range(0, n - 1):
	dist[0] += abs(cur[0] - coords[i])
	dist[1] += abs(cur[1] - coords[n - i - 1])
	dist[2] += abs(cur[2] - coords[i + 1])
	dist[3] += abs(cur[3] - coords[n - i - 2])
	cur[0] = coords[i]
	cur[1] = coords[n - i - 1]
	cur[2] = coords[i + 1]
	cur[3] = coords[n - i - 2]
print(min(dist))


import math
(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
if n == 1:
	print('0')
elif n == 2:
	c = k - a[0]
	c = int(math.fabs(c))
	d = k - a[1]
	d = int(math.fabs(d))
	print(min(c, d))
else:
	x = []
	a.sort()
	e = int(math.fabs(k - a[0]))
	g = int(math.fabs(k - a[n - 2]))
	e = int(min(e, g))
	f = int(math.fabs(k - a[n - 1]))
	h = int(math.fabs(k - a[1]))
	f = int(min(f, h))
	s = 0
	for i in range(0, n - 1):
		c = int(a[i + 1] - a[i])
		c = int(math.fabs(c))
		s += c
	e = e + s - (a[n - 1] - a[n - 2])
	f = f + s - (a[1] - a[0])
	print(min(e, f))


(n, a) = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
if n == 1:
	print(0)
else:
	x = min(abs(a - l[0]), abs(a - l[-2])) + l[-2] - l[0]
	y = min(abs(a - l[1]), abs(a - l[-1])) + l[-1] - l[1]
	ans = min(x, y)
	print(ans)


from math import *
(n, a) = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
if n == 1:
	print(0)
elif a > A[-1]:
	print(abs(a - A[1]))
elif a < A[0]:
	print(abs(a - A[-2]))
else:
	per1 = abs(A[0] - A[-2])
	per2 = abs(A[1] - A[-1])
	ans1 = abs(A[0] - a) + per1
	ans2 = per1 + abs(A[-2] - a)
	ans3 = per2 + abs(a - A[-1])
	ans4 = per2 + abs(a - A[1])
	print(min(ans1, ans2, ans3, ans4))


def s():
	[n, a] = list(map(int, input().split()))
	x = list(map(int, input().split()))
	x.sort()
	if n == 1:
		print(0)
		return
	print(min(x[-1] - x[1] + min(abs(x[-1] - a), abs(x[1] - a)), x[-2] - x[0] + min(abs(x[-2] - a), abs(x[0] - a))))
s()


import sys
read = lambda : sys.stdin.readline().strip()
readi = lambda : map(int, read().split())
from collections import *
from math import *
(n, s) = readi()
nums = sorted(readi())
if n == 1:
	print(0)
else:
	for i in range(n):
		nums[i] -= s
	negDist = abs(nums[0])
	posDist = abs(nums[-1])
	ans1 = abs(min(0, nums[1]))
	if ans1 == 0:
		ans1 += nums[-1]
	elif nums[-1] >= 0:
		ans1 += nums[-1] + min(nums[-1], ans1)
	ans2 = abs(min(0, nums[0]))
	if ans2 == 0:
		ans2 += nums[-2]
	elif nums[-2] >= 0:
		ans2 += nums[-2] + min(nums[-2], ans2)
	print(min(ans1, ans2))


(n, a) = map(int, input().split())
x = sorted(list(map(int, input().split())))
c = 0
if n == 1:
	c = 0
elif n == 2:
	c = min(abs(x[0] - a), abs(x[1] - a))
elif a <= x[0]:
	c = abs(x[n - 2] - a)
elif a >= x[n - 1]:
	c = abs(a - x[1])
elif a <= x[1]:
	d0 = abs(a - x[0])
	dn1 = abs(a - x[n - 1])
	dn2 = abs(a - x[n - 2])
	c = min(dn1, d0 * 2 + dn2, dn2 * 2 + d0)
elif a >= x[n - 2]:
	d0 = abs(a - x[0])
	d1 = abs(a - x[1])
	dn2 = abs(a - x[n - 1])
	c = min(d0, d1 * 2 + dn2, dn2 * 2 + d1)
else:
	d0 = abs(x[0] - a)
	d1 = abs(x[1] - a)
	dn1 = abs(x[n - 1] - a)
	dn2 = abs(x[n - 2] - a)
	c = min(d0 * 2 + dn2, d1 * 2 + dn1, dn1 * 2 + d1, dn2 * 2 + d0)
print(c)


(n, a) = map(int, input().split())
array = list(map(int, input().split()))
array.sort()
ans = 0
if n == 1:
	ans = 0
elif n == 2:
	ans = min(abs(array[0] - a), abs(array[-1] - a))
else:
	var1 = array[-2] - array[0] + min(abs(array[0] - a), abs(array[-2] - a))
	var2 = array[-1] - array[1] + min(abs(array[1] - a), abs(array[-1] - a))
	ans = min(var1, var2)
print(ans)


(n, a) = map(int, input().split())
X = list(map(int, input().split()))
X = [X[i] - a for i in range(n)]
X.sort()
L = []
R = []
for i in X:
	if i <= 0:
		L.append(i)
	else:
		R.append(i)

def minimum(A):
	if len(A) > 0:
		return min(A)
	else:
		return 0

def maximum(A):
	if len(A) > 0:
		return max(A)
	else:
		return 0
if len(L) > 1:
	t_1 = maximum(R) * 2 + abs(minimum(L[1:]))
else:
	t_1 = maximum(R) * 2
if len(R) > 1:
	t_2 = abs(minimum(L) * 2) + maximum(R[:-1])
else:
	t_2 = abs(minimum(L) * 2)
if len(R) > 1:
	t_3 = maximum(R[:-1]) * 2 + abs(minimum(L))
else:
	t_3 = abs(minimum(L))
if len(L) > 1:
	t_4 = abs(minimum(L[1:])) * 2 + maximum(R)
else:
	t_4 = maximum(R)
p = min(t_1, t_2)
q = min(t_3, t_4)
print(min(p, q))


(n, a) = input().strip().split(' ')
(n, a) = (int(n), int(a))
l = list(map(int, input().strip().split(' ')))
l.sort()
if n == 1:
	print(0)
	exit()
elif n == 2:
	print(min(abs(l[1] - a), abs(l[0] - a)))
	exit()
elif a < l[0]:
	print(abs(l[-2] - a))
	exit()
elif a > l[-1]:
	print(abs(l[1] - a))
	exit()
elif a >= l[0] and a <= l[1]:
	d1 = 2 * abs(a - l[0]) + abs(l[-2] - a)
	d3 = 2 * abs(a - l[-2]) + abs(a - l[0])
	d2 = abs(l[-1] - a)
	print(min(d1, d2, d3))
	exit()
elif a >= l[-2] and a <= l[-1]:
	d1 = 2 * abs(l[-1] - a) + abs(a - l[1])
	d2 = abs(a - l[0])
	d3 = 2 * abs(l[1] - a) + abs(a - l[-1])
	print(min(d1, d2, d3))
	exit()
else:
	d1 = 2 * abs(a - l[1]) + abs(l[-1] - a)
	d2 = 2 * abs(a - l[-1]) + abs(l[1] - a)
	d3 = 2 * abs(a - l[0]) + abs(l[-2] - a)
	d4 = 2 * abs(a - l[-2]) + abs(l[0] - a)
	print(min(d1, d2, d3, d4))
	exit()


def second_min(x):
	m = 1000001
	sm = 1000001
	for elem in x:
		if elem < m:
			sm = m
			m = elem
		elif elem < sm:
			sm = elem
	return (m, sm)

def second_max(x):
	m = -1000001
	sm = -1000001
	for elem in x:
		if elem > m:
			sm = m
			m = elem
		elif elem > sm:
			sm = elem
	return (m, sm)
(n, a) = map(int, input().split())
x = list(map(int, input().split()))
if n == 1:
	print(0)
else:
	variant = []
	(Max, smax) = second_max(x)
	(Min, smin) = second_min(x)
	variant.append(abs(Max - a) + (Max - smin))
	variant.append(abs(smax - a) + (smax - Min))
	variant.append(abs(a - Min) + (smax - Min))
	variant.append(abs(a - smin) + (Max - smin))
	print(min(variant))


def parser():
	while 1:
		data = list(input().split(' '))
		for number in data:
			if len(number) > 0:
				yield number
input_parser = parser()

def get_word():
	global input_parser
	return next(input_parser)

def get_number():
	data = get_word()
	try:
		return int(data)
	except ValueError:
		return float(data)
n = get_number()
a = get_number()
chkpnts = []
for i in range(n):
	chkpnts.append(get_number())
chkpnts.sort()
if len(chkpnts) == 1:
	print(0)
elif len(chkpnts) == 2:
	print(min(abs(a - chkpnts[0]), abs(a - chkpnts[1])))
else:
	dist1 = min(abs(a - chkpnts[0]), abs(a - chkpnts[-2])) + abs(chkpnts[0] - chkpnts[-2])
	dist2 = min(abs(a - chkpnts[1]), abs(a - chkpnts[-1])) + abs(chkpnts[1] - chkpnts[-1])
	print(min(dist1, dist2))


na = input().split()
n = int(na[0])
a = int(na[1])
checkpoints = input().split()
checkpoints = [int(check) for check in checkpoints]
checkpoints.sort()
if len(checkpoints) == 1:
	print(0)
else:
	value1 = abs(a - checkpoints[1]) + abs(checkpoints[-1] - checkpoints[1])
	value2 = abs(a - checkpoints[-1]) + abs(checkpoints[-1] - checkpoints[1])
	value3 = abs(a - checkpoints[0]) + abs(checkpoints[-2] - checkpoints[0])
	value4 = abs(a - checkpoints[-2]) + abs(checkpoints[n - 2] - checkpoints[0])
	print(min(value1, value2, value3, value4))


(n, a) = map(int, input().split())
x = sorted(map(int, input().split()))
if n == 1:
	exit(print(0))
print(min(min(abs(a - x[0]), abs(a - x[-2])) + (x[-2] - x[0]), min(abs(a - x[1]), abs(a - x[-1])) + (x[-1] - x[1])))


(n, a) = map(int, input().split())
N = sorted(list(map(int, input().split())))
dist = 0
if n == 1:
	dist = 0
elif n == 2:
	dist = min(abs(N[0] - a), abs(N[1] - a))
elif a <= N[0]:
	dist = abs(N[-2] - a)
elif a >= N[-1]:
	dist = abs(a - N[1])
elif a >= N[-2]:
	dist = min(abs(a - N[0]), 2 * abs(a - N[-1]) + abs(a - N[1]), abs(a - N[-1]) + 2 * abs(a - N[1]))
elif a <= N[1]:
	dist = min(abs(a - N[-1]), 2 * abs(a - N[0]) + abs(a - N[-2]), abs(a - N[0]) + 2 * abs(a - N[-2]))
else:
	dist = min(2 * abs(N[0] - a) + abs(N[-2] - a), 2 * abs(N[1] - a) + abs(N[-1] - a), 2 * abs(N[-1] - a) + abs(N[1] - a), 2 * abs(N[-2] - a) + abs(N[0] - a))
print(dist)


(n, a) = map(int, input().split())
p = list(map(int, input().split()))
p.sort()
if n == 1:
	print('0')
	exit()
if a < p[0]:
	print(abs(a - p[-2]))
	exit()
if a > p[-1]:
	print(abs(a - p[1]))
	exit()
d1 = abs(a - p[-1]) + abs(p[-1] - p[1])
d2 = abs(a - p[-2]) + abs(p[-2] - p[0])
d3 = abs(a - p[1]) + abs(p[1] - p[-1])
d4 = abs(a - p[0]) + abs(p[0] - p[-2])
print(min((d1, d2, d3, d4)))