CodeEff/ECCO huggingface dataset:
# Original solution
N,K=list(map(int,input().split()))
*X,=list(map(int,input().split()))
A=[]
B=[]
for x in X:
    if x>=0:
        A.append(x)
    else:
        B.append(abs(x))
B.sort()
i=min(K,len(A))
j=K-i
ans=[]
while i>=0 and j<=len(B):
    if i==0:
        ans.append(B[j-1])
    elif j==0:
        ans.append(A[i-1])
    else:
        a,b=A[i-1],B[j-1]
        ans.append(a+b+min(a,b))
    i-=1
    j+=1
print((min(ans)))

# Optimized solution
N,K=list(map(int,input().split()))
*X,=list(map(int,input().split()))
i=0
ans=[]
while i+K-1<N:
    a,b=X[i],X[i+K-1]
    if b<0:
        ans.append(abs(a))
    elif a>0:
        ans.append(b)
    else:
        a=abs(a)
        ans.append(a+b+min(a,b))
    i+=1
print((min(ans)))
# Original solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))

ans = float('inf')

for left in range(N - K + 1) :
    if X[left] < 0 and X[left + K - 1] >= 0 :
        l = -X[left] * 2 + X[left + K - 1]
        r = -X[left] + X[left + K - 1] * 2
        ans = min(ans, l, r)
    elif X[left + K - 1] < 0 :
        ans = min(ans, -X[left])
    else :
        ans = min(ans, X[left + K - 1])

print(ans)

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

ans = 10**18
for l in range(N - K + 1):
    D = X[l + K - 1] - X[l]
    if X[l] <= 0 <= X[l + K - 1]:
        D += min(-X[l], X[l + K - 1])
    elif X[l + K - 1] <= 0:
        D += -X[l + K - 1]
    elif X[l] >= 0:
        D += X[l]
    ans = min(ans, D)
print(ans)
# Original solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))

ans = 10**18
for l in range(N - K + 1):
    D = X[l + K - 1] - X[l]
    if X[l] <= 0 <= X[l + K - 1]:
        D += min(-X[l], X[l + K - 1])
    elif X[l + K - 1] <= 0:
        D += -X[l + K - 1]
    elif X[l] >= 0:
        D += X[l]
    ans = min(ans, D)
print(ans)


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

ans = 10**18
for l, r in zip(X, X[K - 1:]):
    if r <= 0:
        ans = min(ans, abs(l))
    if l >= 0:
        ans = min(ans, abs(r))
    if l < 0 < r:
        ans = min(ans, r - l + min(abs(r), abs(l)))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans=4*10**8
# zip関数は多い元は無視する.
for a, b in zip(x, x[k-1:]):
    cand = b-a + min(abs(a), abs(b))
    ans = min(cand, ans)
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans=[0]*(n-k+1)
# zip関数は多い元は無視する.
for i, a, b in zip(list(range(n)), x, x[k-1:]):
    ans[i] = b-a + min(abs(a), abs(b))
print((min(ans)))
# Original solution
x,y = list(map(int,input().split()))
t = list(map(int,input().split()))
ans = 1000000000000000000000000000000
for i in range(x-y+1):
    hani = t[i:i+y]
    if hani[0] <= 0 and 0 <= hani[-1]:
        ans = min(ans, abs(hani[-1]*2)+abs(hani[0]), abs(hani[-1])+abs(hani[0]*2))
    elif hani[-1] <= 0:
        ans = min(ans, abs(hani[0]))
    elif 0 <= hani[0]:
        ans = min(ans, abs(hani[-1]))
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
ans = float("inf")
for left in range(0, N-K+1):
    right = left+K
    minus = 0
    plus = 0
    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])
    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)
 
print(ans)
# Original solution
N, K = list(map(int, input().split()))
X = [int(x) for x in input().split()]

import bisect
zero = bisect.bisect_left(X, 0)

left = max(zero - K, 0)
ans = float('inf')
while 1:
    right = left+K-1
    if left > zero + 1 or len(X) <= right: break
    lv, rv = X[left], X[right]
    if lv * rv > 0:
        ans = min(ans, max(abs(rv), abs(lv)))
    else:
        ans = min(ans, min(rv*2 - lv, rv - lv*2))
    left += 1

print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
X = [int(x) for x in input().split()]

ans = float('inf')
for left in range(N - K + 1):
    lv, rv = X[left], X[left+K-1]
    if lv * rv > 0:
        ans = min(ans, max(abs(rv), abs(lv)))
    else:
        ans = min(ans, min(rv*2 - lv, rv - lv*2))
print(ans)
# Original solution
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))

if 0 in x:
    K -= 1
    x.remove(0)

Min = -1
for i in x:
    goal = i
    if goal > 0:
        path = [j for j in x if j < goal]
        if len(path) < K-1:
            pass
        else:
            path = path[-K+1:]
            le = min(path)
            dis = abs(0-le)+abs(goal-le)
            if Min == -1:
                Min = dis
            else:
                if dis < Min:
                    Min = dis
    else:
        path = [j for j in x if j > goal]
        if len(path) < K-1:
            pass
        else:
            path = path[:K-1]
            re = max(path)
            dis = abs(0-re)+abs(goal-re)
            if Min == -1:
                Min = dis
            else:
                if dis < Min:
                    Min = dis

if Min == -1:
    Min = 0

print(Min)


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

Min = 10**9
for i in range(N-K+1):
    x_l = x[i]
    x_r = x[i+K-1]
    ans_1 = abs(x_l-0) + abs(x_l-x_r)
    ans_2 = abs(x_r-0) + abs(x_l-x_r)
    Min = min(Min, ans_1, ans_2)

print(Min)
# Original solution
import itertools as ito
N,K=list(map(int,input().split(" ")))
X=list(map(int,input().split(" ")))

Ex=list(ito.permutations(X,K))

ans=0
for i in range(len(Ex)):
    tans=0
    tx=0
    for j in range(len(Ex[i])):
        tans+=abs(tx-Ex[i][j])
        tx=Ex[i][j]
        
    if tans<ans or i==0:
        ans=tans
    
print(ans)

# Optimized solution
N,K=list(map(int,input().split(" ")))
X=sorted(list(map(int,input().split(" "))))

ans=0
for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i])

    if i==0 or ans>tans:
        ans=tans

for i in range(N-K+1):
    tans=abs(X[i]-X[i+K-1])+abs(X[i+K-1])

    if ans>tans:
        ans=tans

print(ans)
# Original solution
import bisect
import sys

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

index = bisect.bisect_left(x,0)

if x[0] >= 0 :
  print((x[k-1]))
  sys.exit()
  
if x[-1] <= 0 :
  print((x[n-k]*(-1)))
  sys.exit()

ans = 10**9
if min(index+k,n) == index+k :
  ans = x[index+k-1]

if max(0,index-k) == index - k :
  ans = min(ans,x[index-k]*(-1))
  
c = 0
for i in range(index,min(index+k,n)) :
  c += 1
  if i <= n-1 and i-(k-1) <= n-1 and i-(k-1) >= 0 :
    ans = min(ans,x[i]*2+x[i-(k-1)]*(-1))

c = 0
for i in range(max(0,index-k),index) :
  c += 1
  if i <= n-1 and i+(k-1) <= n-1 :
    ans = min(ans,x[i]*(-1)*2+x[i+(k-1)])
  
print(ans)

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

ans = 10**9
for i in range(n-k+1) :
  xs = abs(x[i+k-1]-x[i])
  ans = min(ans,xs+abs(x[i]),xs+abs(x[i+k-1]))
  
print(ans)
# Original solution
N,K = list(map(int,input().split()))
X = [int(i) for i in input().split()]

X_sub = [[] for i in range(N-K+1)]

for i in range(N-K+1):
    X_sub[i] = X[i:i+K]

min_cost = float('inf')

for i in X_sub:
    tmp_l = abs(i[0]) + abs(i[-1]-i[0])
    tmp_r = abs(i[-1]) + abs(i[0]-i[-1])
    tmp = min(tmp_l,tmp_r)

    if min_cost > tmp:
        min_cost = tmp

print(min_cost)

# Optimized solution
N,K = list(map(int,input().split()))
X = [int(i) for i in input().split()]

min_cost = float('inf')

for i in range(N-K+1):
    sub_x = X[i:i+K]
    tmp_l = abs(sub_x[0]) + abs(sub_x[-1]-sub_x[0])
    tmp_r = abs(sub_x[-1]) + abs(sub_x[0]-sub_x[-1])
    tmp = min(tmp_l,tmp_r)

    if min_cost > tmp:
        min_cost = tmp

print(min_cost)
# Original solution
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))

ans=10**9

for i in range(n-k+1):
  if x[i+k-1]<0:
    ans=min(ans,-x[i])
  elif x[i]>0:
    ans=min(ans,x[i+k-1])
  else:
    ans=min(ans,2*x[i+k-1]-x[i],2*(-x[i])+x[i+k-1])

print(ans)

# Optimized solution
import sys
input = sys.stdin.buffer.readline
input = sys.stdin.readline


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()
# Original solution
import sys
input = sys.stdin.buffer.readline
input = sys.stdin.readline


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()


# Optimized solution
import sys
input = sys.stdin.buffer.readline
#input = sys.stdin.readline


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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

	ans=10**18

	for i in range(n):
		try:
			l=x[i]
			r=x[i+k-1]
			if r<0:
				ans=min(ans,-l)
			elif l>0:
				ans=min(ans,r)
			else:
				ans=min(ans,r-l+min(-l,r))
		except:
			pass
	print(ans)



if __name__ == "__main__":
	main()
# Original solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))

if x[0] >= 0:
    print((x[k - 1]))
    exit()
elif x[n - 1] < 0:
    print((abs(x[n - k])))
    exit()
else:
    for i in range(n):
        if x[i] >= 0:
            ori = i
            break

f_inf = float("inf")
res = f_inf
for i in range(max(ori - k, 0), min(ori + k, n) - k + 1):
    time = min(abs(x[i + k - 1] - x[i]) + abs(x[i + k - 1]), abs(x[i]) + abs(x[i + k - 1] - x[i]))
    res = min(res, time)

print(res)


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    X = list(map(int, input().split()))

    if X[0] >= 0:
        print((X[k - 1]))
        exit()
    elif X[-1] < 0:
        print((abs(X[n - k])))
        exit()
    else:
        for i in range(n):
            if X[i] >= 0:
                ori = i
                break

    res = f_inf
    left = max(ori - k, 0)
    right = min(ori + k, n)
    for i in range(left, right - k + 1):
        RL = abs(X[i + k - 1] - X[i]) + abs(X[i + k - 1])
        LR = abs(X[i]) + abs(X[i + k - 1] - X[i])
        res = min(res, min(RL, LR))
    print(res)


if __name__ == '__main__':
    resolve()
# Original solution
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    X = list(map(int, input().split()))

    if X[0] >= 0:
        print((X[k - 1]))
        exit()
    elif X[-1] < 0:
        print((abs(X[n - k])))
        exit()
    else:
        for i in range(n):
            if X[i] >= 0:
                ori = i
                break

    res = f_inf
    left = max(ori - k, 0)
    right = min(ori + k, n)
    for i in range(left, right - k + 1):
        RL = abs(X[i + k - 1] - X[i]) + abs(X[i + k - 1])
        LR = abs(X[i]) + abs(X[i + k - 1] - X[i])
        res = min(res, min(RL, LR))
    print(res)


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = list(map(int, input().split()))
    X = list(map(int, input().split()))

    for i in range(n):
        if X[i] >= 0:
            ori = i
            X.insert(ori, 0)
            break
    else:
        ori = n
        X.insert(ori, 0)

    res = f_inf
    start = max(0, ori - k)
    end = min(n - k + 1, ori + 1)
    for j in range(start, end):
        left = abs(X[j]) * 2 + abs(X[j + k])
        right = abs(X[j]) + abs(X[j + k]) * 2
        res = min(res, min(left, right))
    print(res)


if __name__ == '__main__':
    resolve()
# Original solution
N,K = list(map(int, input().split()))
X = [int(x) for x in input().split()]

ref = []
for i in range(N-K+1):
    ref.append(X[i:i+K])

List = []
for j in range(N-K+1):
    if X[j]<=0 and X[j+K-1]>=0:
        List.append(X[j+K-1]-X[j]+min(abs(X[j]),X[j+K-1]))
    else:
        List.append(max(abs(X[j]),abs(X[j+K-1])))

print((min(List)))

# Optimized solution
N,K = list(map(int, input().split()))
X = [int(x) for x in input().split()]

List = []
for j in range(N-K+1):
    if X[j]<=0 and X[j+K-1]>=0:
        List.append(X[j+K-1]-X[j]+min(abs(X[j]),X[j+K-1]))
    else:
        List.append(max(abs(X[j]),abs(X[j+K-1])))

print((min(List)))
# Original solution
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = 3*10**8

for i in range(N-K+1):
    ans1 = abs(x[i])+abs(x[i]-x[i+K-1])
    ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans = min(ans, ans1, ans2)
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
x = [int(i) for i in input().split()]
ans = x[K - 1] - x[0] + min(abs(x[0]), abs(x[K - 1]))
for i in range(N - K + 1): 
    ans = min(ans, x[K + i -1] - x[i] + min(abs(x[K + i -1]), abs(x[i])) )
print(ans)
# Original solution
n,k=list(map(int,input().split()))
x=list(map(int,input().split()))

import bisect
c=bisect.bisect_left(x,0)
mt=2*10**8

for i in range(n-k+1):
  if i+k<c:continue
  mt=min(mt,2*abs(x[i])+abs(x[i+k-1]),abs(x[i])+2*abs(x[i+k-1]))
  if i+k-1 ==c-1:mt=min(mt,abs(x[i]))
  if i==c:mt=min(mt,x[i+k-1])
if n==k:
  if c!=n and c!=0:
    mt=min(2*abs(x[0])+x[-1],2*x[-1]+abs(x[0]))
  else:
    mt=max(abs(x[0]),abs(x[-1]))
print(mt)
    
  

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

mt=5*10**8

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

if x[0]>0:mt=min(mt,x[k-1])
elif x[-1]<0:mt=min(mt,abs(x[-1-k+1]))
print(mt)
# Original solution
import bisect
n,k = list(map(int, input().split()))
x = list(map(int, input().split()))
z = bisect.bisect_left(x,0)

ans=10**12
s=z-k+1
if s < 0:
  s=0
if z == 0:
  z+=1
for i in range(s,z):
  t=x[i:i+k]
  xl=min(t)
  xr=max(t)
  ans=min(ans,abs(xr)+abs(xr-xl),abs(xl)+abs(xr-xl))
print(ans)

# Optimized solution
import bisect
n,k = list(map(int, input().split()))
x = list(map(int, input().split()))
z = bisect.bisect_left(x,0)

ans=10**12
s=z-k
if s < 0:
  s=0
g=z+1
if g+k >= n:
  g=n-k+1
for i in range(s,g):

  xl=x[i]
  xr=x[i+k-1]
  ans=min(ans,abs(xr)+abs(xr-xl),abs(xl)+abs(xr-xl))
print(ans)
# Original solution
# coding: utf-8
# Your code here!
 
import bisect
from collections import defaultdict
import sys
sys.setrecursionlimit(int(1e6))
 
N, K = list(map(int, input().rstrip().split(' ')))
X = sorted(list(map(int, input().rstrip().split(' '))))
 
i = bisect.bisect_left(X, 0)
X.insert(i, 0)
 
memo = defaultdict(lambda: defaultdict(lambda: -1))
 
def recursive(current_k, cost, i, left_i, right_i):
    if K == current_k:
        return cost
    if memo[current_k][i] != -1:
        return memo[current_k][i]
       
    left_cost = 1e10 
    if left_i >= 0:
        left_cost = recursive(current_k + 1, cost + X[i] - X[left_i], left_i, left_i - 1, right_i)
        
    right_cost = 1e10
    if right_i <= N:
        right_cost = recursive(current_k + 1, cost + X[right_i] - X[i], right_i, left_i, right_i + 1)
        
    min_cost = min(left_cost, right_cost)
    
    memo[current_k][i] = min_cost
    
    return min_cost
    
print((recursive(0, 0, i, i - 1, i + 1)))

# Optimized solution
# coding: utf-8
# Your code here!

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

min_cost = int(1e10)
for i in range(K, N+1):
    min_cost = min(min_cost, X[i-1] - X[i-K] + min(abs(X[i-1]), abs(X[i-K])))
    
print(min_cost)
# Original solution
from bisect import bisect_left

n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
if 0 in x:
    x.remove(0)
    k -= 1
    n -= 1

if n == 0:
    print((0))
else:
    si = bisect_left(x, 0)

    ans = (x[-1] - x[0]) * 2
    if si + k - 1 < len(x):
        ans = min(ans, x[si + k - 1])
    if si - k >= 0:
        ans = min(ans, abs(x[si - k]))

    for i in range(n):
        tmp_ans = 0
        if i < si and i + k -1 < len(x):
            tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
            ans = min(ans, tmp_ans)
        elif i >= si and i - k + 1 >= 0:
            tmp_ans = x[i] * 2 + abs(x[i - k + 1])
            ans = min(ans, tmp_ans)

    print(ans)


# Optimized solution
from bisect import bisect_left

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

si = bisect_left(x, 0)

ans = (x[-1] - x[0]) * 2
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

for i in range(n):
    tmp_ans = 0
    if i < si and i + k -1 < len(x):
        tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
        ans = min(ans, tmp_ans)
    elif i >= si and i - k + 1 >= 0:
        tmp_ans = x[i] * 2 + abs(x[i - k + 1])
        ans = min(ans, tmp_ans)

print(ans)
# Original solution
from bisect import bisect_left

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

si = bisect_left(x, 0)

ans = (x[-1] - x[0]) * 2
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

for i in range(n):
    tmp_ans = 0
    if i < si and i + k -1 < len(x):
        tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
        ans = min(ans, tmp_ans)
    elif i >= si and i - k + 1 >= 0:
        tmp_ans = x[i] * 2 + abs(x[i - k + 1])
        ans = min(ans, tmp_ans)

print(ans)


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

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

print(ans)
# Original solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = 10**9
for i in range(n-k+1):
    x2 = x[i:i+k]
    if x2[0]*x2[-1] < 0:
        left = 2*abs(x2[0])+abs(x2[-1])
        right = 2*abs(x2[-1])+abs(x2[0])
        ans= min(ans,min(left,right))
    else:
        ans = min(ans,max(abs(x2[0]),abs(x2[-1])))
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = 10**9
for i in range(n-k+1):
    if x[i]*x[i+k-1] < 0:
        left = 2*abs(x[i])+abs(x[i+k-1])
        right = 2*abs(x[i+k-1])+abs(x[i])
        ans= min(ans,min(left,right))
    else:
        ans = min(ans,max(abs(x[i]),abs(x[i+k-1])))
print(ans)
# Original solution
n, k = list(map(int, input().split()))
xlis = list(map(int, input().split()))
ret = 10 ** 100
for i in range(n-k+1):
    cand = xlis[i:k+i]
    min_val = cand[0]
    max_val = cand[-1]
    if min_val * max_val >= 0:
        tmp = max(abs(min_val), abs(max_val))
        ret = min(ret, tmp)
    else:
        fst = min(abs(min_val), abs(max_val))
        lst = max(abs(min_val), abs(max_val))
        tmp = fst * 2 + lst
        ret = min(ret, tmp)

print(ret)


# Optimized solution
n, k = list(map(int, input().split()))
xlis = list(map(int, input().split()))
ret = 10 ** 100
for i in range(n-k+1):
    min_val = xlis[i]
    max_val = xlis[k+i-1]
    if min_val * max_val >= 0:
        tmp = max(abs(min_val), abs(max_val))
        ret = min(ret, tmp)
    else:
        fst = min(abs(min_val), abs(max_val))
        lst = max(abs(min_val), abs(max_val))
        tmp = fst * 2 + lst
        ret = min(ret, tmp)

print(ret)
# Original solution
import sys
input = sys.stdin.readline


def main():
    _, K = list(map(int, input().split()))
    X = [int(c) for c in input().split()]

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print((min(fullsearch())))

main()

# Optimized solution
import sys
input = sys.stdin.readline


def main():
    _, K = list(map(int, input().split()))
    X = list(map(int, input().split()))

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print((min(fullsearch())))

main()
# Original solution
import sys
input = sys.stdin.readline


def main():
    _, K = list(map(int, input().split()))
    X = list(map(int, input().split()))

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print((min(fullsearch())))

main()

# Optimized solution
import sys
input = sys.stdin.readline

def fullsearch(x, k):
    for l, r in zip(x, x[k-1:]):
        distance = abs(r - l)
        yield distance + abs(r)
        yield distance + abs(l)

def main():
    _, K = list(map(int, input().split()))
    X = list(map(int, input().split()))



    print((min(fullsearch(X, K))))

main()
# Original solution
# sys.stdin.readline()
import sys
input = sys.stdin.readline

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

x_p = [0] + [p for p in x if p > 0]#正
x_z = x.count(0)#0
x_n = [(-1) * ng for ng in x if ng < 0]#負
x_n = [0] + sorted(x_n)#0から始まる絶対値

myK = k - x_z#左右に移動して点火するろうそく数

if myK <= 0:
    print((0))
else:
    ans = float('inf')
    for i in range(myK + 1):
        if 0 <= i < len(x_p) and 0 <= myK - i < len(x_n):
            ans = min(ans, x_p[i] + x_n[myK - i] + min(x_p[i], x_n[myK - i]))
    print(ans)

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

ans = float('inf')
for i in range(n - k + 1):
    ans = min(
            ans,
            abs(x[i]) + abs(x[i + k - 1] - x[i]),
            abs(x[i + k - 1]) + abs(x[i + k - 1] - x[i])
        )
print(ans)
# Original solution
inf = float('inf')

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

m, p = [], []
for xx in x:
    if xx > 0:
        p.append(xx)
    elif xx < 0:
        m.append(-xx)
    else:
        K -= 1
        if K == 0:
            print((0))
            exit()
m.sort()
lp = len(p)
lm = len(m)

ans = inf
for from_minus in range(min(lm, K) + 1):
    from_plus = K - from_minus
    if from_plus > lp:
        continue
    if from_minus == 0:
        ans = min(ans, p[K - 1])
    elif from_minus == K:
        ans = min(ans, m[K - 1])
    else:
        ans = min(ans, p[from_plus - 1] * 2 + m[from_minus - 1])
        ans = min(ans, m[from_minus - 1] * 2 + p[from_plus - 1])
print(ans)


# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    *x, = list(map(int, input().split()))

    ans = 3 * 10 ** 8
    for left, right in zip(x, x[K - 1:]):
        if left * right < 0:
            ans = min(ans, right - left + min(-left, right))
        else:
            ans = min(ans, -left if left < 0 else right)
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok
# Original solution
N,K=list(map(int,input().split()))
X=list(map(int,input().split()))+[0]
ans=10**20

for i in range(N-K+1):
    x=X[i:i+K]
    l,r=x[0],x[-1]
    if l<=0 and r<=0:
        ans=min(ans,-l)
    elif l>=0 and r>=0:
        ans=min(ans,r)
    else:
        res=min(abs(l),abs(r))*2+max(abs(l),abs(r))
        ans=min(ans,res)
print(ans)

# Optimized solution
N,K=list(map(int,input().split()))
X=list(map(int,input().split()))
ans=float('inf')
for l in range(N-K+1):
    r=l+K-1
    ans=min(ans,min(abs(X[l]),abs(X[r]))+X[r]-X[l])
print(ans)