There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.
In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.
When activated, each type of robot will operate as follows.
 - When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.
 - When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.
Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

-----Constraints-----
 - 1 \leq N \leq 100
 - 1 \leq K \leq 100
 - 0 < x_i < K
 - All input values are integers.

-----Inputs-----
Input is given from Standard Input in the following format:
N
K
x_1 x_2 ... x_N

-----Outputs-----
Print the minimum possible total distance covered by robots.

-----Sample Input-----
1
10
2

-----Sample Output-----
4

There are just one ball, one type-A robot and one type-B robot.
If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.
Similarly, if the type-B robot is used, the total distance covered will be 16.
Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Difficulty: introductory

Starter code:

Solutions:
n = int(input())
k = int(input())
x = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans += 2*min(k-x[i],x[i])
print(ans)

n = int(input())
k = int(input())
li = list(map(int,input().split()))

dis = 0
for i in range(n):
    dis += min(2*li[i],2*abs(li[i]-k))

print(dis)

n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0

for i in x:
  ans += 2*min(i,abs(k-i))

print(ans)

N=int(input())
K=int(input())
X=list(map(int,input().split()))

ans=0
for x in X:
    ans+=min(abs(x),abs(x-K))*2
print(ans)

N = int(input())
K = int(input())
X = list(map(int,input().split()))

ans = 0
for x in X:
    ans += min(abs(x),abs(x-K))

print(ans*2)

import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: list(map(int, sys.stdin.readline().split()))
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
k = ri()
ans = 0
for i in rl():
    ans += min(i, abs(k-i))
print((ans*2))












N, K, *X = map(int, open(0).read().split())

ans = 0
for y,x in enumerate(X, start=1):
    a = x * 2
    b = (K - x) * 2
    ans += min(a, b)

print(ans)

N = int(input())
K = int(input())
print((sum([min(int(a), K - int(a)) for a in input().split()]) * 2))


n = int(input())
k = int(input())
xn = list(map(int,input().split()))

class Solution:
    def __init__(self,n,k,xn):
        self.n = n
        self.k = k
        self.xn = xn
    
    def printout(self):
        print(n,k,xn)
    
    def answer(self):
        total = 0
        total = 0
        for xi in xn:
            a = abs(xi - 0)
            b = abs(xi - k)
            if a <= b:
                total += a * 2
            else:
                total += b * 2
        print(total)

conditions = Solution(n,k,xn)
conditions.answer()

def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, k = (int(input()) for _ in range(2))
    x = Input()
    ans = 0
    for i in range(n):
        ans += min(abs(0-x[i]), abs(k-x[i])) * 2
    print(ans)


main()

N = int(input())
K = int(input())
x = list(map(int,input().split()))
distance = 0

for i in range(N):
  distance += min(2*x[i],2*abs(x[i]-K))
  
print(distance)

N = input()
K = int(input())
l = input().split()
         

         
ans =0 
for i in l:
	s = int(i)
	ans += min(s,K-s)
print((ans*2))


n=int(input())
k=int(input())
x=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=min(x[i],k-x[i])
print(2*ans)

n = int(input())
k = int(input())
xn = list(map(int, input().split()))


class Solution:
    def __init__(self, n, k, xn):
        self.n = n
        self.k = k
        self.xn = xn

    @staticmethod
    def printout():
        print(n, k, xn)

    @staticmethod
    def answer():
        total = 0
        for xi in xn:
            a = abs(xi - 0)
            b = abs(xi - k)
            if a <= b:
                total += a * 2
            else:
                total += b * 2
        print(total)


conditions = Solution(n, k, xn)
conditions.answer()

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

tot_dis = 0
for i in range(n):
    min_dis = min(x[i], k-x[i])
    tot_dis += 2*min_dis
print(tot_dis)

n = int(input())
k = int(input())
points = list(map(int, input().split()))

ans = 0
for i in points:
    minimum = min(abs(k-i), abs(i))
    ans += minimum*2

print(ans)

n = int(input())
k = int(input())
li = list(map(int,input().split()))
total = 0
for i in range(n):
    total += 2*min(li[i],k-li[i])
print(total)

def int_list(sepalate=" "): return list(map(int, input().split(sepalate)))


def solve1(n, k, x):
    ans = 0

    for i in range(n):
        ans += min(abs(k-x[i]), x[i]) * 2

    return ans


n = int(input())
k = int(input())
x = int_list()
print((solve1(n, k, x)))


#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc074/tasks/abc074_b

N = int(input())
K = int(input())
X = list(map(int, input().split()))

ans = 0
for x in X:
    if x > abs(K-x):
        ans += abs(K-x)*2
    else:
        ans += x*2
print(ans)


n,k=int(input()),int(input());print(sum([min(i*2,(k-i)*2) for i in list(map(int,input().split()))]))

N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0

for i in range(N):
    if X[i] <= (K - X[i]):
        ans += (X[i] * 2)
    else:
        ans += ((K - X[i]) * 2)
print (ans)

n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(i,k-i)
print(2*ans)

n = int(input())
k = int(input())
x = [int(x) for x in input().split()]


ans = 0
for i in range(n):
    ans += 2*min(x[i], abs(x[i]-k))

print(ans)

N = int(input())
K = int(input())
X = list(map(int, input().split()))

ans = 0

for x in X:
    ans += min(x, K-x)*2
    
print(ans)

N = int(input())
K = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
  ans += min(i, K - i) * 2
print(ans)

n = int(input())
k = int(input())
x = [int(x.strip()) for x in input().split()]
ans = 0
for i in x:
  if abs(i-k) > i:
    ans = ans + i
  else:
    ans = ans + abs(i-k)
ans += ans
print(ans)

n = int(input())
k = int(input())
x = [int(s) for s in input().split()]

move = 0
for i in x:
    move += min(i, abs(i - k)) * 2
print(move)

n = int(input())
k = int(input())
x_l = list(map(int, input().split()))
th = k/2

ans = 0
for x in x_l:
  if x >= th:
    ans += abs(x-k)
  else:
    ans += x
print(ans*2)

n,k = [int(input()) for i in range(2)]
x = list(map(int,input().split()))

distance = 0

for i in range(len(x)):
    distance += 2*(min(x[i], abs(x[i]-k)))
    
print(distance)

a=int(input())
b=int(input())
c=list(map(int,input().split()))
total=0
for i in range(a):
    d=2*c[i]
    e=2*abs(b-c[i])
    total+=min(d,e)
print(total)

n = int(input())
k = int(input())
line = list(map(int, input().split()))
ans = 0
for i in range(n):
  ans += min(line[i], k-line[i]) * 2
print(ans)

N=int(input())
K=int(input())
x=list(map(int,input().split()))

ans=0
for i in range(N):
    a=abs(x[i])*2
    b=abs(K-x[i])*2
    ans+=min(a,b)
    
print(ans)

_,k=input(),int(input());print(sum([min(i*2,(k-i)*2) for i in list(map(int,input().split()))]))

n = int(input())
k = int(input())
data = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans += min(data[i]*2, (k - data[i])*2)
print(ans)

n = int(input())
k = int(input())
L = list(map(int,input().split()))
ans = 0
for l in L:
  a = l
  b = abs(k-l)
  ans += min(a,b)*2
print(ans)

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    return sum(min(xx, abs(xx-K)) for xx in x) * 2


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print((solve(N, K, x)))

def test():
    import doctest
    doctest.testmod()

def __starting_point():
    #test()
    main()

__starting_point()

import math

n = int(input())
k = int(input())
x = list(map(int,input().split()))
cnt = 0

dis = math.ceil(k/2)
for i in x:
    if i == k:
        continue
    elif i <= dis:
        cnt += i*2
    else:
        cnt += (k-i)*2
print(cnt)


import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    K = I()
    x = LI()

    ans = sum([2 * min(x[i], K - x[i]) for i in range(N)])
    print(ans)

def __starting_point():
    resolve()
__starting_point()

N = int(input())
K = int(input())
x = list(map(int, input().split()))

sum = 0
for i in range(N):
  dist = min(x[i], abs(x[i]-K))
  sum += dist
  
print(sum*2)

n,k=int(input()),int(input());print(sum([min(abs(i*2),abs((i-k)*2)) for i in list(map(int,input().split()))]))

a=int(input())
b=int(input())
c=list(map(int,input().split()))
d=0
for i in range(a):
  if c[i]<b-c[i]:
    d=d+c[i]
  else:
    d=d+b-c[i]
print(2*d)

#
# abc074 b
#
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """1
10
2"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
9
3 6"""
        output = """12"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
20
11 12 9 17 12"""
        output = """74"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    K = int(input())
    X = list(map(int, input().split()))

    ans = 0
    for x in X:
        ans += min(x*2, abs(K-x)*2)

    print(ans)


def __starting_point():
    # unittest.main()
    resolve()

__starting_point()

N = int(input())
K = int(input())
X = [int(n) for n in input().split()]

ans = 0

for x in X:
    diff = abs(K - x)
    if diff < x:
        ans += diff
    else:
        ans += x
print((2*ans))


N = int(input())
K = int(input())
x = list(map(int, input().split()))

sum = 0

for i in range(N):
  if x[i] >= K - x[i]:
    sum += 2 * (K - x[i])
  
  else:
    sum += 2 * x[i]

print(sum)

# -*- coding:utf-8 -*-
N = int(input())
K = int(input())
x_list = list(map(int,input().split()))

sum_distance = 0

for x in x_list:
    if x < K - x:
        sum_distance += 2*x
    else:
        sum_distance += 2*(K-x)

print(sum_distance)

N = int (input())
K = int (input())
x = list(map(int, input().split()))
sum_min = 0

for p in x:
  a = 2*p
  b = 2*abs(K-p)
  if a >= b:
    sum_min += b
  elif a < b:
    sum_min += a

print(sum_min)

N = int(input())
K = int(input())
x = list(map(int, input().split()))

ans = 0
for i in range(N):
    ans += min(x[i]-0, K-x[i])
print((ans*2))


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

res = 0

for i in range(n):
  res += min(2*(abs(a[i])),2*(abs(a[i] - k)))

print(res)

N = int(input())
K = int(input())

x = list(map(int, input().split()))
calc = 0

for i in range(N):
    calc += min(x[i], abs(K-x[i])) * 2

print(calc)

N = int(input(""))
K = int(input(""))
a = input("").split(" ")
a = [int(aa) for aa in a]
ans = 0
for x in a:
    if (K-x) < x:
        ans += (K-x)*2
    elif (K-x) >= x:
        ans += x*2
print(ans)


"""Boot-camp-for-Beginners_Easy009_B_Collecting-Balls-(Easy-Version)_28-August-2020.py"""

N = int(input())
K = int(input())
x = list(map(int, input().split()))

s = 0
for i in range(N):
    if x[i] < K/2:
        s += 2*x[i]
    else:
        s += 2*(K-x[i])
print(s)


N = int(input())
K = int(input())
X = list(map(int,input().split()))

ans = 0
for x in X:
  ans += 2 * (x if x <= K - x else K - x)
  
print(ans)


# ABC074

N = int(input())
K = int(input())
X = list(map(int, input().split()))

ans = 0
for x in X:
    ans += min(x, K-x)*2
print(ans)


def main():
    _ = int(input())
    k = int(input())
    x = list(map(int, input().split()))
    print((sum([min([abs(xi - point) for point in [0, k]]) * 2 for xi in x])))


def __starting_point():
    main()

__starting_point()

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

print(sum([min(position, abs(k - position)) * 2 for position in x]))

n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(i, k-i)*2
print(ans)

n = int(input())
k = int(input())

x = list(map(int, input().split()))
ans = 0
for i in x:
  ans += 2 * min(i, k - i)
print(ans)

N = int(input())
K = int(input())
x = list(map(int,input().split()))
ans = 0
for i in x:
  if i <= K - i:
    ans += i
  if i > K - i:
    ans += K - i
print((2 * ans))
    


N=int(input())
K=int(input())
x=list(map(int,input().split()))
print((sum(2*min(y,K-y) for y in x)))


N = int(input())
K = int(input())
X = list(map(int, input().split()))
cnt = 0

for x in X:
    cnt += min(x, abs(K-x))*2
print(cnt)


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

d = 0
for i in x:
    d += min(i, k-i)
print(2*d)

N=int(input())
K=int(input())
x=list(map(int,input().split()))
tot=0
for i in range(N):
    if abs(x[i]-K)<x[i]:
        tot+=2*(abs(x[i]-K))
    else:
        tot+=2*x[i]
print(tot)

N = int(input())
K = int(input())
lst = input().split()

ans = 0

for i in range(N):
   x = int(lst[i])
   if abs(x) < abs(K - x):
      ans += 2 * abs(x)
   else:
      ans += 2 * abs(K - x)

print(ans)

n = int(input())
k = int(input())
x = list(map(int,input().split()))
c= 0
for i in range(len(x)):
    c+=min(x[i],k-x[i])*2
print(c)

N = int(input())
K = int(input())
x = [int(x) for x in input().split()]
ans = 0
for i in range(N):
  ans += 2 * min(abs(x[i]-0), abs(x[i]-K))
print(ans)

N = int(input())
K = int(input())
x = list(map(int,input().split()))
ans = 0

for i in range(N):
  ans += min(abs(x[i] - 0), abs(K - x[i]))
print(ans * 2)

n = int(input())
k = int(input())

s = 0
xs = list(map(int, input().split()))
for x in xs:  
  if x < abs(k-x):
    s += 2 * x
  else:
    s += 2 * abs(k-x)
    
print(s)


n = int(input())
k = int(input())
v = list(map(int, input().split()))

ans = 0
for i in v:
    tmp = min(i, k-i)
    ans += tmp
print((ans*2))


n = int(input())
k = int(input())
l = list(map(int, input().split()))
c = 0
for x in l:
  c += min(x,k-x)
print(2*c)

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

ans=0
for i in range(n):
  ans+=min(abs(x[i]-0), abs(x[i]-k))
print(ans*2)

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    res = 0
    for x_ in x:
        res += 2*min(x_, abs(K-x_))
    print(res)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, x)

def __starting_point():
    main()

__starting_point()

N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0
for x in X:
    ans += min(x, abs(K-x))*2
print(ans)


n = int(input())
k = int(input())
x = list(map(int,input().split()))
count = 0
for i in range(n):
    count += min(x[i], k-x[i])
print(count * 2)

_,k=input(),int(input());print(sum(min(i,(k-i))*2 for i in list(map(int,input().split()))))

N = int(input())
K = int(input())
x = [int(c) for c in input().split() ]

cnt = 0
for i in range(N):
    a = 0
    b = 0
    a = abs(x[i]-0)*2
    b = abs(x[i]-K)*2
    if a <= b:
        cnt += a
    else:
        cnt += b

print(cnt)

N = int(input())
K = int(input())
A = list(map(int, input().split()))

ans = 0
for a in A:
  ans += 2*min(a, K-a)
print(ans)

N = int(input())
K = int(input())
x = [int(i) for i in input().split()]
ans = 0
for i in range(N) :
    xi = x[i]
    if xi <= (K//2) :
        ans += 2*xi
    else :
        ans += 2*(K-xi)
print(ans)

_,k=input(),int(input());print(sum([min(i,(k-i))*2 for i in list(map(int,input().split()))]))

N = int(input())
K = int(input())
x = list(map(int, input().split()))

ans = 0

for x in x:
    a = 2 * x
    b = abs(K - x) * 2
    ans += min(a, b)

print(ans)


n = int(input())
k = int(input())
nums = list(map(int, input().split()))
print(2 * sum([min(k - i, i) for i in nums]))

n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in range(n):
    if x[i] >= k - x[i]:
        ans += 2*(k - x[i])
    else:
        ans += 2*x[i]
print(ans)

n = int(input())
k = int(input())
x_list = list(map(int, input().split()))
ans = 0
for i in x_list:
    if 2*i < k:
        ans += 2*i
    else :
        ans += (k-i)*2
print(ans)


N = int(input())
K = int(input())
x = list(map(int,input().split()))
total = 0
for i in range (N):
  if x[i]<K/2:
    total += 2*x[i]
  elif x[i]>= K/2:
    total += 2*(K-x[i])
print(total)

n = int(input())
k = int(input())
x = list(map(int, input().split()))
a = 0
for i in x:
    if 2 * i < 2 * (k - i):
        a += 2 * i
    else:
        a += 2 * (k - i)
print(a)

n = int(input())
k = int(input())
x = list(map(int, input().split()))
d = 0
for i in x:
    y = min(i, abs(k-i))
    d += 2*y
print(d)

N = int(input())
K = int(input())
xs = list(map(int, input().split()))

dist_sum = 0
for i in range(N):
	y = i + 1
	x = xs[i]
	
	if x > K - x:
		dist_sum += (K - x) * 2
	else:
		dist_sum += x * 2
		
print(dist_sum)
	


N = int(input())
K = int(input())
X = list(map(int, input().split()))
Y = [i for i in range(1, N+1)]
ans = 0

for x in X:
    distance_a = x
    distance_b = abs(K-x)
    if distance_a < distance_b:
        ans += distance_a
    else:
        ans += distance_b
    
print(ans*2)

with open(0) as f:
    N, K, *X = map(int, f.read().split())
print(2*sum(min(x, K-x) for x in X))

N = int(input())
K = int(input())
X = list([int(x) for x in input().split()])

result = 0

for pos in X:
    if pos <= abs(pos - K):
        result += 2 * pos
    else:
        result += 2 * abs(pos - K)

print(result)


#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    data=[]
    n = int(input())
    k = int(input())
    data=list(map(int,input().split()))
    length=0

    for ball in data:
        if ball < k-ball:
            length+=2*ball
        else:
            length+=2*(k-ball)
    print(length)

def __starting_point():
    main()
__starting_point()

n=int(input())
k=int(input())
x=list(map(int,input().split()))
ans=0
for  i in range(n):
  m=min(abs(x[i]),abs(k-x[i]))
  ans+=m

print(ans*2)

n = int(input())
k = int(input())
x = list(map(int,input().split()))
ans = 0
for i in range(n):
  ans += 2*min(abs(x[i]-k),abs(x[i]))
print(ans)

N = int(input())
K = int(input())
dist = 0
for val in list(map(int,input().split())):
  dist += 2*min(val,abs(val-K))
  
print(dist)

N=int(input())
K=int(input())
X=list(map(int,input().split()))

ans=0

for i in X:
    Y=[]
    Y.append(2*abs(i))
    Y.append(2*abs(i-K))
    ans+=min(Y)
    
print(ans)

n,k=int(input()),int(input());x=list(map(int,input().split()))
print(sum([min(i,j) for i,j in zip([abs(i*2) for i in x],[abs((i-k)*2) for i in x])]))

n=int(input())
k=int(input())
l=list(map(int,input().split()))

ans=0
for i in l:
  if i>=k/2:
    ans+=abs(k-i)*2
  else:
    ans+=i*2

print(ans)


N = int(input())
K = int(input())
X = [int(x) for x in input().split()]

ans = 0
for x in X:
    ans += min(x, abs(x-K))
ans *= 2
print(ans)

n=int(input())
k=int(input())
x=list(map(int,input().split()))
ans=0
for xi in x:
    ans+=2*min(xi,k-xi)
print(ans)


from typing import List


def answer(n: int, k: int, xs: List[int]) -> int:
    moving_distance = 0
    reference_value = k / 2
    for x in xs:
        if x <= reference_value:
            moving_distance += x * 2
        else:
            moving_distance += (k - x) * 2

    return moving_distance


def main():
    n = int(input())
    k = int(input())
    xs = list(map(int, input().split()))
    print(answer(n, k, xs))


def __starting_point():
    main()
__starting_point()