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.

Difficulty: competition

Starter code:

Solutions:
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)))

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)))


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)  

#!/usr/bin/env python3
# http://codeforces.com/contest/830/problem/A
import sys
from operator import itemgetter

read = lambda: list(map(int, input().split()))
n, k, p = read() # n people, k keys, office location 
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))


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

n,k,p = read()
a = read()
b = read()
a.sort()
b.sort()
ans = 2e9+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,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)

n, k, p = (map(int, input().split()))
a = sorted(map(int, input().split()))
b = sorted(map(int, 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)

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(2e9+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 = 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))

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

def time(start):
	nonlocal 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)

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)


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 = 1e15
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)


read = lambda: list(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)))



# Made By Mostafa_Khaled


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)

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()

#a = a + filler
#b = b + filler

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 math as mt 
import sys,string
input=sys.stdin.readline
#print=sys.stdout.write
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)
    


# self.author = Fuad Ashraful Mehmet ,CSE-UAP

read=lambda:map(int,input().split())

n,k,p=read()
A=sorted(read())
B=sorted(read())

res=1e18
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)

'''
Input:
2 4 50
20 100
60 10 40 80
Output:
50

'''

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=list(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 sys
import math
#from queue import *
#import random
#sys.setrecursionlimit(int(1e6))
input = sys.stdin.readline
 
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
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())))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############

n,k,p=invr()
a=inara()
b=inara()
a.sort()
b.sort()

ans=int(1e18)
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 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)