There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.

You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.


-----Input-----

The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 10^9) — the number of people, the number of keys and the office location.

The second line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — positions in which people are located initially. The positions are given in arbitrary order.

The third line contains k distinct integers b_1, b_2, ..., b_{k} (1 ≤ b_{j} ≤ 10^9) — positions of the keys. The positions are given in arbitrary order.

Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.


-----Output-----

Print the minimum time (in seconds) needed for all n to reach the office with keys.


-----Examples-----
Input
2 4 50
20 100
60 10 40 80

Output
50

Input
1 2 10
11
15 7

Output
7



-----Note-----

In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Sorting

Complete search

Greedy algorithms


Solutions:
(_, _, office) = [int(i) for i in input().split()]
people = sorted((int(i) for i in input().split()))
keys = sorted((int(i) for i in input().split()))
assert len(people) <= len(keys)
min_time = min((max((abs(p - keys[ki + v]) + abs(keys[ki + v] - office) for (v, p) in enumerate(people))) for ki in range(len(keys) - len(people) + 1)))
print(min_time)


(n, k, p) = (int(x) for x in input().split())
a = sorted([int(x) for x in input().split()])
b = sorted([int(x) for x in input().split()])
time = min([max([abs(a[j] - b[i + j]) + abs(b[i + j] - p) for j in range(n)]) for i in range(k - n + 1)])
print(time)


import sys
import math
input = sys.stdin.readline

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

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

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

def invr():
	return list(map(int, input().split()))
(n, k, p) = invr()
a = inara()
b = inara()
a.sort()
b.sort()
ans = int(1e+18)
for i in range(k - n + 1):
	curr = -1
	for j in range(n):
		if p <= b[i + j] and b[i + j] <= a[j]:
			curr = max(curr, a[j] - p)
		elif a[j] <= b[i + j] and b[i + j] <= p:
			curr = max(curr, p - a[j])
		else:
			curr = max(curr, abs(a[j] - b[i + j]) + abs(p - b[i + j]))
	ans = min(ans, curr)
print(ans)


def check(x):
	j = 0
	for i in range(n):
		if j >= k:
			return False
		while abs(b[j] - a[i]) + abs(b[j] - p) > x:
			j += 1
			if j >= k:
				return False
		j += 1
	return True
(n, k, p) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
l = 0
r = 10 ** 18
ans = -1
while l <= r:
	mid = l + (r - l) // 2
	if check(mid):
		r = mid - 1
		ans = mid
	else:
		l = mid + 1
print(ans)


import os, sys
from io import BytesIO, IOBase

def check(mid, a, b, n, k, p):
	z = k - 1
	for i in range(n - 1, -1, -1):
		while z != -1 and abs(b[z] - a[i]) + abs(b[z] - p) > mid:
			z -= 1
		if z == -1:
			return 0
		z -= 1
	return 1

def main():
	(n, k, p) = map(int, input().split())
	a = sorted(map(int, input().split()))
	b = sorted(map(int, input().split()))
	(hi, lo) = (2 * 10 ** 9, 0)
	while hi >= lo:
		mid = (hi + lo) // 2
		if check(mid, a, b, n, k, p):
			hi = mid - 1
			fl = 0
		else:
			lo = mid + 1
			fl = 1
	print(mid + fl)
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')
main()


read = lambda : map(int, input().split())
(n, k, p) = read()
(a, b) = (sorted(read()), sorted(read()))
print(min((max((abs(b[i + d] - a[i]) + abs(b[i + d] - p) for i in range(n))) for d in range(k - n + 1))))


read = lambda : list(map(int, input().split()))
(n, k, p) = read()
a = list(read())
b = list(read())
a.sort()
b.sort()

def cal(a, b):
	return abs(a - b) + abs(b - p)
mi = 1000000000000000.0
for _ in range(k - n + 1):
	ma = 0
	for __ in range(n):
		te = cal(a[__], b[_ + __])
		if te > ma:
			ma = te
	if ma < mi:
		mi = ma
print(mi)


import sys
from operator import itemgetter
read = lambda : map(int, input().split())
(n, k, p) = read()
(people, keys) = (sorted(read()), sorted(read()))
res = []
for i in range(k - n + 1):
	inner_res = []
	for j in range(n):
		inner_res.append(abs(keys[i + j] - people[j]) + abs(keys[i + j] - p))
	res.append(max(inner_res))
print(min(res))


from sys import stdin, stdout
(n, k, p) = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
a = sorted(a)
b = [int(x) for x in stdin.readline().rstrip().split()]
b = sorted(b)
ans = int(2000000000.0 + 1)
for i in range(k - n + 1):
	tmp = 0
	for j in range(n):
		tmp = max(tmp, abs(a[j] - b[j + i]) + abs(b[j + i] - p))
	ans = min(ans, tmp)
print(ans)


(n, k, p) = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
a.sort()
b.sort()
res = 2147483647
for i in range(k - n + 1):
	m = 0
	for j in range(n):
		k = j + i
		dis = abs(a[j] - b[k]) + abs(b[k] - p)
		m = max(m, dis)
	res = min(res, m)
print(res)


(n, k, p) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
A.sort()
B.sort()
ans = 10 ** 18
for start in range(k):
	curmax = 0
	curans = 10 ** 18
	if start + n > k:
		continue
	for i in range(n):
		curmax = max(curmax, abs(A[i] - B[start + i]) + abs(B[start + i] - p))
	ans = min(ans, curmax)
print(ans)


def dp(k, a, t):
	a = sorted(a)
	t = sorted(t)
	dp = [[float('inf')] * (len(a) + 1) for i in range(len(t) + 1)]
	dp[0][0] = 0
	for i in range(len(t)):
		for j in range(len(a) + 1):
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
			if j < len(a):
				dp[i + 1][j + 1] = min(dp[i + 1][j + 1], max(dp[i][j], abs(a[j] - t[i]) + abs(t[i] - k)))
	return dp[len(t)][len(a)]
(a, b, c) = map(int, input().strip().split())
lst = list(map(int, input().strip().split()))
lst2 = list(map(int, input().strip().split()))
print(dp(c, lst, lst2))


(n, k, p) = map(int, input().split(' '))
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
a.sort()
b.sort()
result = []
for i in range(k):
	max_time = 0
	if k - i < n:
		break
	for ind in range(n):
		tem = 0
		tem += abs(a[ind] - b[i + ind])
		tem += abs(b[i + ind] - p)
		if max_time < tem:
			max_time = tem
	result.append(max_time)
print(min(result))


(n, k, l) = map(int, input().split())
people = sorted(list(map(int, input().split())))
key = sorted(list(map(int, input().split())))
dp = [[999999999999999 for i in range(n + 1)] for j in range(k + 1)]
for i in range(k + 1):
	dp[i][0] = 0
for i in range(1, k + 1):
	for j in range(1, n + 1):
		dp[i][j] = max(dp[i - 1][j - 1], min(dp[i - 1][j], abs(key[i - 1] - people[j - 1]) + abs(key[i - 1] - l)))
print(dp[k][n])


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : list(map(int, input().split()))
I = lambda : int(input())
(n, k, p) = M()
l = L()
g = L()
l.sort()
g.sort()
ans = 10 ** 30
for i in range(k - n + 1):
	ans1 = 0
	for j in range(i, i + n):
		ans1 = max(ans1, abs(l[j - i] - g[j]) + abs(g[j] - p))
	ans = min(ans, ans1)
print(ans)


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

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

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

def invr():
	return map(int, input().split())
mod = 1000000007
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if n < 0 or r < 0 or r > n:
		return 0
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	AX()

def C():
	n = pi()
	a = ti()
	sm = [0 for i in range(n)]
	for i in range(n):
		if i == 0:
			sm[i] = a[i]
			continue
		sm[i] = sm[i - 1] + a[i]
	dp01 = [-1000000000 for i in range(n)]
	x = [[] for i in range(n)]
	for i in range(n):
		for j in range(i, -1, -1):
			if j != 0:
				if dp01[i] < sm[j - 1] - (sm[i] - sm[j - 1]):
					dp01[i] = sm[j - 1] - (sm[i] - sm[j - 1])
					x[i] = [j - 1, i + 1]
			elif dp01[i] < sm[i]:
				dp01[i] = sm[i]
				x[i] = [0, i + 1]
	dp12 = [-1000000000 for i in range(n)]
	mx = -1000000000
	mxIndex2 = 0
	mxIndex1 = 0
	for i in range(n):
		for j in range(i, -1, -1):
			if dp12[i] < dp01[j] + sm[i] - sm[j] - (sm[n - 1] - sm[i]):
				dp12[i] = dp01[j] + sm[i] - sm[j] - (sm[n - 1] - sm[i])
				if dp12[i] > mx:
					mx = dp12[i]
					mxIndex2 = i + 1
					mxIndex1 = j
	ans = [x[mxIndex1][0], x[mxIndex1][1], mxIndex2]
	print('sum', sm)
	print(dp01)

def A():
	[n, k, p] = ti()
	a = ti()
	b = ti()
	pre = [1000000000000000000 for i in range(k)]
	dp = [[1000000000000000000 for j in range(k)] for i in range(n)]
	for j in range(k):
		dp[0][j] = abs(a[0] - b[j]) + abs(p - b[j])
		pre[j] = min(pre[j - 1], dp[0][j]) if j != 0 else dp[0][j]
	for i in range(1, n):
		curr = [1000000000000000000 for i in range(k)]
		for j in range(k):
			if j >= i:
				dp[i][j] = max(pre[j - 1], abs(a[i] - b[j]) + abs(p - b[j]))
				curr[j] = min(curr[j - 1], dp[i][j]) if j != 0 else dp[i][j]
		pre = curr
	ans = 1000000000000000000
	for i in range(k):
		ans = min(ans, dp[n - 1][i])
	print(ans)

def AX():
	[n, k, p] = ti()
	a = sorted(ti())
	b = sorted(ti())
	ans = 100000000000000000
	curr = 0
	for i in range(k - n, -1, -1):
		curr = 0
		for j in range(n):
			curr = max(curr, abs(b[i + j] - a[j]) + abs(p - b[i + j]))
		ans = min(curr, ans)
	print(ans)
main()


import sys
input = sys.stdin.readline
(n, k, p) = map(int, input().split())
A = sorted(map(int, input().split()))
B = [0] + sorted(map(int, input().split()))
dp = [[0] * (k + 1) for _ in range(n + 1)]
for (i, a) in enumerate(A, 1):
	for j in range(k - n + i, i - 1, -1):
		dp[i][j] = max(dp[i - 1][j - 1], abs(a - B[j]) + abs(B[j] - p))
print(min(dp[-1][n:]))


filler = [0] * 10000
numbers = list(map(int, input().split()))
n = numbers[0]
k = numbers[1]
p = numbers[2]
rezultat = 9999999999999999
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
for i in range(k - n + 1):
	temp_max = -999999999
	for j in range(n):
		temp_max = max(abs(b[i + j] - a[j]) + abs(b[i + j] - p), temp_max)
	rezultat = min(temp_max, rezultat)
print(rezultat)


import os
import sys
import math
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 main():
	(n, k, p) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(a, b) = (sorted(a), sorted(b))
	dp = [[[-1 for i in range(2)] for i in range(k)] for i in range(n)]

	def solve(i, j, taken):
		if j < 0:
			return 10 ** 18
		if dp[i][j][taken] != -1:
			return dp[i][j][taken]
		if taken:
			dist = abs(a[i] - b[j]) + abs(b[j] - p)
			if i:
				dp[i][j][taken] = max(dist, min(solve(i - 1, j - 1, taken), solve(i - 1, j - 1, taken ^ 1)))
			else:
				dp[i][j][taken] = dist
		else:
			dp[i][j][taken] = min(solve(i, j - 1, taken), solve(i, j - 1, taken ^ 1))
		return dp[i][j][taken]
	ret = min(solve(n - 1, k - 1, 1), solve(n - 1, k - 1, 0))
	print(ret)
main()


inf = 10 ** 10
par = [int(i) for i in str(input()).split()]
(n, k, p) = (par[0], par[1], par[2])
person = [int(i) for i in str(input()).split()]
key = [int(i) for i in str(input()).split()]
person = sorted(person)
key = sorted(key)
dp = [[inf for i in range(k)] for j in range(n)]
for i in range(n):
	for j in range(k):
		if i == 0:
			if j == 0:
				dp[i][j] = min(dp[i][j], abs(person[i] - key[j]) + abs(p - key[j]))
				continue
		if i > j:
			dp[i][j] = inf
			continue
		a = dp[i - 1][j - 1] if i > 0 else -1
		dp[i][j] = min(dp[i][j - 1], max(a, abs(person[i] - key[j]) + abs(p - key[j])))
print(dp[n - 1][k - 1])


(n, k, p) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()

def cost(j):
	if j < 0 or j + n - 1 >= k:
		return 1000000000000
	maximum = 0
	for i in range(n):
		t = abs(a[i] - b[i + j]) + abs(b[i + j] - p)
		maximum = max(maximum, t)
	return maximum
l = 0
r = k - n
while l < r:
	mid = (l + r) // 2
	cost1 = cost(mid)
	cost1r = cost(mid + 1)
	if cost1 < cost1r:
		r = mid
	else:
		l = mid + 1
print(cost(l))


def good(n, k, p, a, b, x):
	j = 0
	c = 0
	for i in range(n):
		while j < k:
			if abs(a[i] - b[j]) + abs(b[j] - p) <= x:
				j += 1
				c += 1
				break
			j += 1
	return c == n
(n, k, p) = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
a.sort()
b.sort()
l = -1
r = 2 * 10 ** 9
dp = [[float('inf')] * (n + 1) for i in range(k + 1)]
dp[0][0] = 0
for i in range(k):
	for j in range(n + 1):
		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
		if j < n:
			dp[i + 1][j + 1] = min(dp[i + 1][j + 1], max(dp[i][j], abs(a[j] - b[i]) + abs(b[i] - p)))
print(dp[k][n])


import math
import sys
from bisect import bisect_right, bisect_left
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate
from sys import stdout
R = lambda : map(int, input().split())
(np, nk, z) = R()
ps = sorted(R())
ks = sorted(R())
res = math.inf
for ki in range(nk - np + 1):
	res = min(res, max((abs(x - y) + abs(y - z) for (x, y) in zip(ps, ks[ki:ki + np + 1]))))
print(res)


(na, nb, o) = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
dp = [[float('inf') for _ in range(nb + 1)] for _ in range(na + 1)]
for i in range(nb + 1):
	dp[0][i] = 0
for i in range(1, na + 1):
	for j in range(1, nb + 1):
		dp[i][j] = min(dp[i][j - 1], max(dp[i - 1][j - 1], abs(a[i - 1] - b[j - 1]) + abs(o - b[j - 1])))
print(dp[na][nb])


read = lambda : map(int, input().split())
(n, k, p) = read()
A = sorted(read())
B = sorted(read())
res = 1e+18

def fun(x, y):
	return abs(x - y) + abs(y - p)
for i in range(k - n + 1):
	now = 0
	for j in range(n):
		c = fun(A[j], B[i + j])
		if c > now:
			now = c
	if now < res:
		res = now
print(res)


s = input()
(n, k, p) = s.split(' ')
n = int(n)
k = int(k)
p = int(p)

def time(start):
	global people, keys, n, p
	res = 0
	for i in range(n):
		res = max(res, abs(people[i] - keys[i + start]) + abs(keys[i + start] - p))
	return res
s = input()
people = s.split(' ')
for i in range(n):
	people[i] = int(people[i])
s = input()
keys = s.split(' ')
for i in range(k):
	keys[i] = int(keys[i])
people.sort()
keys.sort()
res = 10 ** 10
for i in range(k - n + 1):
	res = min(res, time(i))
print(res)


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

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

def map_string():
	return input().split()
(n, k, p) = map_input()
a = list_input()
b = list_input()
a.sort()
b.sort()
ans = 1000000000000000000
for i in range(k - n + 1):
	cur = 0
	c1 = 0
	c2 = i
	while c1 < n:
		cur = max(abs(a[c1] - b[c2]) + abs(b[c2] - p), cur)
		c1 += 1
		c2 += 1
	ans = min(ans, cur)
print(ans)


(n, k, p) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
ans = 1000000000000000
ans = int(ans)
for i in range(k - n + 1):
	tmp = 0
	for j in range(n):
		tmp = max(tmp, abs(a[j] - b[i + j]) + abs(b[i + j] - p))
	ans = min(ans, tmp)
print(ans)


from math import inf
(nl, kl, p) = map(int, input().split())
n = list(map(int, input().split()))
k = list(map(int, input().split()))
n.sort()
k.sort()
d = [[inf for _ in range(kl)] for _ in range(nl)]
d[0][0] = abs(n[0] - k[0]) + abs(k[0] - p)
for i in range(1, kl):
	d[0][i] = min(d[0][i - 1], abs(n[0] - k[i]) + abs(k[i] - p))
for j in range(1, kl):
	for i in range(1, nl):
		d[i][j] = min(d[i][j - 1], max(d[i - 1][j - 1], abs(n[i] - k[j]) + abs(k[j] - p)))
print(d[-1][-1])


import sys
input = sys.stdin.readline
(n, k, p) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
ans = 10 ** 18
for i in range(k - n + 1):
	sc = 0
	for j in range(n):
		sc = max(sc, abs(a[j] - b[i + j]) + abs(b[i + j] - p))
	ans = min(ans, sc)
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
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 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 SegmentTree1:

	def __init__(self, data, default=10 ** 6, 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)
MOD = 10 ** 9 + 7

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
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(10)]
pp = [0] * 10

def SieveOfEratosthenes(n=10):
	p = 2
	c = 0
	while p * p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p, n + 1, p):
				pp[i] += 1
				prime[i] = False
		p += 1

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

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			right = mid - 1
		else:
			res = arr[mid]
			left = mid + 1
	return res
(n, k, p) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()

def find(x):
	j = 0
	for i in range(n):
		if j == k:
			return False
		t = 0
		while j < k:
			if abs(b[j] - a[i]) + abs(p - b[j]) <= x:
				j += 1
				t = 1
				break
			else:
				j += 1
		if t == 0:
			return False
	return True
st = 0
end = 10 ** 14
ans = end
while st <= end:
	mid = (st + end) // 2
	if find(mid) == True:
		ans = mid
		end = mid - 1
	else:
		st = mid + 1
print(ans)


import sys
from collections import defaultdict

def calc(a, b, p):
	return abs(a - b) + abs(p - b)

def main():
	(n, k, p) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	a = sorted(a)
	b = sorted(b)
	v = 10 ** 10
	for i in range(k - n + 1):
		t = 0
		for j in range(n):
			t = max(t, calc(a[j], b[j + i], p))
		v = min(v, t)
	print(v)
main()


(n, k, p) = map(int, input().split())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
res = 1000000000000000.0
for i in range(k - n + 1):
	ma = 0
	for j in range(n):
		te = abs(a[j] - b[i + j]) + abs(b[i + j] - p)
		ma = max(ma, te)
	res = min(res, ma)
print(res)


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

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

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

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

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	(n, k, p) = li()
	a = li()
	b = li()
	a.sort()
	b.sort()
	ans = 100000000000000000000
	for j in range(k):
		if j + n > k:
			break
		temp = 0
		for i in range(j, j + n):
			temp = max(temp, abs(a[i - j] - b[i]) + abs(p - b[i]))
		ans = min(ans, temp)
	print(ans)
t = 1
for _ in range(t):
	solve()


from sys import stdin, stdout
from itertools import accumulate
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
PI = float('inf')
for _ in range(1):
	(na, nb, o) = lst()
	a = sorted(lst())
	b = sorted(lst())
	dp = [[PI for _ in range(nb + 1)] for _ in range(na + 1)]
	for i in range(nb + 1):
		dp[0][i] = 0
	for i in range(1, na + 1):
		for j in range(1, nb + 1):
			dp[i][j] = min(dp[i][j - 1], max(dp[i - 1][j - 1], abs(a[i - 1] - b[j - 1]) + abs(o - b[j - 1])))
	print(dp[na][nb])


(n, k, p) = map(int, input().split())
a = []
b = []
temp = input().split()
for i in range(n):
	a.append(int(temp[i]))
temp = input().split()
for i in range(k):
	b.append(int(temp[i]))
list.sort(a)
list.sort(b)
mini = 0
for i in range(k - n + 1):
	maxi = 0
	cur = 0
	for j in range(n):
		if p >= a[j]:
			if a[j] > b[i + j]:
				cur = p + a[j] - 2 * b[i + j]
			elif b[i + j] > p:
				cur = 2 * b[i + j] - p - a[j]
			else:
				cur = p - a[j]
		elif b[i + j] > a[j]:
			cur = 2 * b[i + j] - p - a[j]
		elif b[i + j] < p:
			cur = p + a[j] - 2 * b[i + j]
		else:
			cur = a[j] - p
		if cur > maxi:
			maxi = cur
	if maxi < mini or i == 0:
		mini = maxi
print(mini)


def read():
	return [int(x) for x in input().split()]
(n, k, p) = read()
a = read()
b = read()
a.sort()
b.sort()
ans = 2000000000.0 + 1
ans = int(ans)
for i in range(k - n + 1):
	tmp = 0
	for j in range(n):
		tmp = max(tmp, abs(a[j] - b[i + j]) + abs(b[i + j] - p))
	ans = min(ans, tmp)
print(ans)


(N, M, P) = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
A = sorted(A)
B = sorted(B)

def check(HAHA):
	vis = [False for _ in range(N)]
	res = 0
	for i in range(M):
		for ii in range(N):
			x = abs(A[ii] - B[i])
			x += abs(P - B[i])
			if vis[ii] == True or x > HAHA:
				continue
			res += 1
			vis[ii] = True
			break
	return res == N
ret = -1
l = 0
r = int(3000000000.0)
while l <= r:
	md = l + r >> 1
	if check(md):
		ret = md
		r = md - 1
	else:
		l = md + 1
print(ret)


pp = list(map(int, input().split()))
inf = int(1e+18)
n = pp[0]
k = pp[1]
p = pp[2]
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort()
dp = [[inf for j in range(0, 2011)] for i in range(0, 1011)]
for i in range(0, k):
	dp[0][i] = abs(a[0] - b[i]) + abs(b[i] - p)
for i in range(1, n):
	for j in range(0, k):
		dp[i][j] = min(int(dp[i][j]), max(abs(a[i] - b[j]) + abs(b[j] - p), int(dp[i - 1][j - 1])))
ans = int(1e+18)
for i in range(0, k):
	ans = min(ans, int(dp[n - 1][i]))
print(ans)


def good(n, k, p, a, b, x):
	j = 0
	c = 0
	for i in range(n):
		while j < k:
			if abs(a[i] - b[j]) + abs(b[j] - p) <= x:
				j += 1
				c += 1
				break
			j += 1
	return c == n
(n, k, p) = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
a.sort()
b.sort()
l = -1
r = 2 * 10 ** 9
while r - l > 1:
	m = (l + r) // 2
	if good(n, k, p, a, b, m):
		r = m
	else:
		l = m
print(r)


o = lambda : [int(f) for f in input().split()]
(n, k, p) = o()
a = sorted(o())
b = sorted(o())
print(min((max((abs(b[i + d] - a[i]) + abs(b[i + d] - p) for i in range(n))) for d in range(k - n + 1))))