CodeEff/ECCO huggingface dataset:
# Original solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
d = [0 for _ in range(n)]
S = [0 for _ in range(n)]
for i in range(1, n):
    d[i] = x[i] - x[i - 1]
    S[i] = S[i - 1] + d[i]
tmp = [0 for _ in range(n - k + 1)]
for i in range(n - k + 1):
    tmp[i] = S[i + k - 1] - S[i] + min(abs(x[i]), abs(x[i + k - 1]))
print((min(tmp)))


# Optimized solution
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = abs(x[-1] - x[0]) * 2
for i in range(n - k + 1):
    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))
print(ans)
# Original solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))
right, left = [], []
	
for i in range(N):
	if X[i] >= 0:
		right.append(X[i])
	else:
		left.insert(0, -X[i])
		
right.append(0)
left.append(0)
ans = float("inf")
		
for i in range(K + 1):
	if len(right) < K - i + 1 or len(left) < i + 1: continue
	tmp = min(2 * left[i - 1] + right[(K - i) - 1], left[i - 1] + 2 * right[(K - i) - 1])
	ans = min(ans, tmp)
	
print(ans)

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

for i in range(0, N - K + 1):
	ans = min(ans, min(abs(X[i]) + abs(X[i] - X[i + K - 1]), abs(X[i + K - 1]) + abs(X[i] - X[i + K - 1])))
	
print(ans)
# Original solution
"""
0からLまで行き、LからRまで行く = abs(L) + (R-L)
0からRまで行き、RからLまで行く = abs(R) + (R-L)
"""

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 1<<60

for i in range(0, N-K+1):
  left = A[i]
  right = A[i+K-1]
  ans = min(ans, min(abs(left), abs(right)) + right - left)
print(ans)

# Optimized solution

def resolve():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    ans = 1 << 60

    for i in range(N - K + 1):
        l = A[i]
        r = A[i + K - 1]
        l_to_r = abs(l) + abs(l - r)
        r_to_l = abs(r) + abs(r - l)
        res = min(l_to_r, r_to_l)
        ans = min(ans, res)

    print(ans)


if __name__ == "__main__":
    resolve()
# Original solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

ans = INF
for i in range(K):
    if i+K > N:
        break
    A2 = A[i:i+K]
    l = A2[0]
    r = A2[-1]
    # 左だけ行く
    if r <= 0:
        ans = min(ans, -l)
    # 右だけ行く
    elif l >= 0:
        ans = min(ans, r)
    # 両方行く
    else:
        # 短い方を先に行って往復
        ans = min(ans, min(-l, r) * 2 + max(-l, r))
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(l, r):
    # 左だけ行く
    if r <= 0:
        return -l
    # 右だけ行く
    elif l >= 0:
        return r
    # 両方行く
    else:
        # 短い方を先に行って往復
        return min(-l, r) * 2 + max(-l, r)

N, K = MAP()
A = LIST()

ans = INF
for i in range(N):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    ans = min(ans, check(l, r))
print(ans)
# Original solution
N, K = list(map(int, input().split()))
X = list(map(int, input().split()))

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, max(abs(b), abs(c)))
    else:
        d, e = min(abs(b), abs(c)), max(abs(b), abs(c))
        answer = min(answer, d*2+e)
print(answer)


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

answer = []
for i in range(N-K+1):
    a, b = X[i], X[i+K-1]
    answer.append(min(abs(a-b) + abs(b), abs(b-a) + abs(a)))
print((min(answer)))
# Original solution
# coding: utf-8
import sys
from bisect import bisect_left, bisect_right

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
X = lr()
if 0 in X:
    K -= 1

i = bisect_left(X, 0)
ne = X[:i]
i = bisect_right(X, 0)
po = X[i:]
INF = 10 ** 10
neg = [INF] * (K+1)  # 左からK本選ぶ場合、K-1本選ぶ場合・・・0本選ぶ場合
neg[-1] = 0
pos = [INF] * (K+1)
pos[0] = 0
for j in range(len(ne)):
    if -j-2 < -K-1:
        break
    neg[-j-2] = ne[-j-1]

for j in range(len(po)):
    if j+1 > K:
        break
    pos[j+1] = po[j]

answer = INF
for j in range(K+1):
    x = abs(neg[j]); y = abs(pos[j])
    mi = min(x, y)
    ma = max(x, y)
    temp = mi * 2 + ma
    if temp < answer:
        answer = temp
print(answer)
# 52

# Optimized solution
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
X = lr()
answer = []
for i in range(N-K+1):
    l = X[i]; r = X[i+K-1]
    a = abs(l) + abs(r-l)
    b = abs(r) + abs(l-r)
    answer.append(min(a, b))

print((min(answer)))
# 03
# Original solution
N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]

mi = 1e10
for l in range(N - K + 1):
    r = l + K - 1
    mi = min(mi, abs(X[l]) + abs(X[r] - X[l]), abs(X[r]) + abs(X[r] - X[l]))
print(mi)

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

print((min(r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1:]))))
# Original solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
for i in range(k+1):
    mainasu = 0
    purasu = 0
    flag = True
    for j in range(k):
        a = zahyou-k+i+j
        if  a >= 0 and a < n:
            if x[a] < 0:
                mainasu += x[a+1]-x[a]
            else:
                purasu += x[a+1]-x[a]
        else:
            flag = False
    if mainasu != 0 and purasu != 0:
        purasu += min(mainasu,purasu)
    if flag:
        ans = min(ans,mainasu+purasu)
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
for i in range(k+1):
    flag = True
    if zahyou + i < n+1 and zahyou -k+i >= 0:
        mainasu = x[zahyou]-x[zahyou-k+i]
        purasu = x[zahyou+i]-x[zahyou]
        if mainasu != 0 and purasu != 0:
            purasu += min(mainasu,purasu)
        ans = min(ans,mainasu+purasu)
print(ans)
# Original solution
N,K = list(map(int,input().split()))
x = list(map(int,input().split()))
cand = []
for i in range(N-K+1):
  cand.append(x[i:i+K])

ans = float('inf')
for i in range(len(cand)):
  l = cand[i][0]
  r = cand[i][-1]
  temp = min(abs(l)+abs(r-l),abs(r)+abs(l-r))
  ans = min(temp,ans)

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):
  l = x[i]
  r = x[i+K-1]
  temp = min(abs(l)+abs(r-l),abs(r)+abs(l-r))
  ans = min(temp,ans)

print(ans)
# Original solution
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**9
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        MIN =min(MIN,ans)

print(MIN)

# Optimized solution
def main():
    n,k = list(map(int,input().split()))
    X = list(map(int,input().split()))

    ans = 10**9

    for i in range(n-k+1):
        t1 = abs(X[i]) + abs(X[i] - X[i+k-1])
        t2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
        t = min(t1,t2)

        ans = min(ans,t)

    print(ans)



main()
# Original solution
import bisect
N, K = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = 10**9
zero = bisect.bisect_left(x, 0)

if zero == 0:
    print((x[K-1]))
    exit(0)

if zero+K <= N:
    ans = min(ans, x[zero+K-1])
if zero >= K:
    ans = min(ans, -x[zero-K])

if zero != N:
    for k in range(K-1):
        if 0 <= K+zero-k-2 < N and 0 <= zero-k-1 < N: 
            ans = min(ans,2*x[K+zero-k-2]-x[zero-k-1],x[K+zero-k-2]-2*x[zero-k-1])

print(ans)


# Optimized solution
import bisect
N, K = list(map(int,input().split()))
x = list(map(int,input().split()))
ans = float("inf")
if x[0]*x[-1] >= 0:
    if x[0] >= 0:
        ans = x[K-1]
    else:
        ans = -x[-K]
    print(ans)
else:
    M = []
    P = []
    for e in x:
        if e > 0:
            P.append(e)
        else:
            M.append(-e)
    M.sort()
    M = [0] + M
    P = [0] + P
    for k in range(max(len(P),len(M))):
        if k < len(P) and 0 <= K-k < len(M):
#            print(M[K-k],P[k])
            ans = min(ans,2*M[K-k]+P[k],M[K-k]+2*P[k])
    print(ans)
# Original solution
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return eval(input())


if __name__ == "__main__":
    n, k = LI()
    X = LI()
    l_count = sum((x <= 0 for x in X))
    r_count = n - l_count

    if l_count == 0:
        print((X[k-1]))
    elif r_count == 0:
        print((abs(X[-k])))
    else:
        times = []
        for i in range(l_count):
            try:
                left = X[l_count - i - 1]
                right = X[l_count + k - i - 2]
                times.append(abs(2 * left) + abs(right))
                times.append(abs(left) + abs(2 * right))
            except:
                continue
        print((min(times)))


# Optimized solution
n, k, *x = list(map(int, open(0).read().split()))

print((min([min(abs(x[i]), abs(x[i+k-1])) + (x[i+k-1] - x[i]) for i in range(n-k+1)])))
# Original solution
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        C = X[n:n + K]
        ret = min(ret, abs(X[n]) + abs(X[n] - X[n+K-1]), abs(X[n+K-1]) + abs(X[n] - X[n+K-1]))
    print(ret)


if __name__ == '__main__':
    main()


# Optimized solution
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        ret = min(ret, abs(X[n]) + abs(X[n] - X[n+K-1]), abs(X[n+K-1]) + abs(X[n] - X[n+K-1]))
    print(ret)


if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search_naive(cur, k, x):
        if k == K:
            return 0
        costs = []
        for i in range(len(x)):
            xi = x[i]
            dist = abs(xi-cur)
            costs.append(dist + search_naive(xi, k+1, x[:i]+x[i+1:]))
        return min(costs)
    print((search_naive(0, 0, x))) 
    return


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)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search_naive(cur, k, x):
        if k == K:
            return 0
        costs = []
        for i in range(len(x)):
            xi = x[i]
            dist = abs(xi-cur)
            costs.append(dist + search_naive(xi, k+1, x[:i]+x[i+1:]))
        return min(costs)
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            conds = x[i:i+window_size]
            right_end = max([x for x in conds if x>=0], default=0)
            left_end = abs(min([x for x in conds if x<0], default=0))
            if right_end > left_end:
                costs.append(right_end + 2*left_end)
            else:
                costs.append(2*right_end + left_end)
        return min(costs)
    #print(search_naive(0, 0, x)) 
    print((search(N, K, x)))
    return


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)

if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search_naive(cur, k, x):
        if k == K:
            return 0
        costs = []
        for i in range(len(x)):
            xi = x[i]
            dist = abs(xi-cur)
            costs.append(dist + search_naive(xi, k+1, x[:i]+x[i+1:]))
        return min(costs)
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            conds = x[i:i+window_size]
            right_end = max([x for x in conds if x>=0], default=0)
            left_end = abs(min([x for x in conds if x<0], default=0))
            if right_end > left_end:
                costs.append(right_end + 2*left_end)
            else:
                costs.append(2*right_end + left_end)
        return min(costs)
    #print(search_naive(0, 0, x)) 
    print((search(N, K, x)))
    return


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)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    def search(N, window_size, x):
        costs = []
        for i in range(N-window_size+1):
            right_end = x[i+window_size-1]
            left_end = x[i]
            dist = abs(right_end-left_end)
            costs.append(min(abs(right_end)+dist, abs(left_end)+dist))
        return min(costs)
    print((search(N, K, x)))
    return


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)

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

for i in range(N-K+1):
	length=0
	for j in range(K-1):
		length+=abs(x[i+j+1]-x[i+j])
	cost.append(length+abs(x[i]))
	cost.append(length+abs(x[i+K-1]))
print((min(cost)))

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

for i in range(N-K+1):
	length=abs(x[i]-x[i+K-1])
	cost.append(length+abs(x[i]))
	cost.append(length+abs(x[i+K-1]))
print((min(cost)))
# Original solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
res = 10**10
for i in range(n-k+1):
    kx = x[i:i+k]
    l=kx[0]
    r = kx[-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)

# Optimized solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
kx = x[0:k]
l=kx[0]
r = kx[-1]
lrsmall = min(abs(l),abs(r))
res = r-l+lrsmall
for i in range(1,n-k+1):
    del kx[0]
    kx.append(x[i+k-1])
    l=kx[0]
    r = kx[-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)
# Original solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
kx = x[0:k]
l=kx[0]
r = kx[-1]
lrsmall = min(abs(l),abs(r))
res = r-l+lrsmall
for i in range(1,n-k+1):
    del kx[0]
    kx.append(x[i+k-1])
    l=kx[0]
    r = kx[-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)

# Optimized solution
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
res = 10**10
for i in range(0,n-k+1):
    l = x[i]
    r = x[i+k-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)
# Original solution

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

for i in range(n-k+1):
    l = lst[i]
    r = lst[i+k-1]
    way1 = abs(l) + abs(r-l)
    way2 = abs(r) + abs(l-r)
    m = min(way1,way2)
    if i==0:
        ans = m
    elif m < ans:
        ans = m
    
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):
    l = x[i]
    r = x[i+k-1]
    w1 = abs(l)+abs(r-l)
    w2 = abs(r)+abs(l-r)
    ans = min(ans, min(w1,w2))
print(ans)
# Original solution
from itertools import combinations

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

print((min([min(abs(cmb[0]) + abs(cmb[-1]-cmb[0]), abs(cmb[-1]) + abs(cmb[0]-cmb[-1])) for cmb in combinations(X,K)])))

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

print((min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)])))
# Original solution
N,K=list(map(int,input().split()))
X = list(map(int, input().split()))

print((min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)])))

# Optimized solution
N, K = list(map(int, input().split()))
posX = []
negX = []
for x in map(int, input().split()):
    if 0 <= x:
        posX.append(x)
    else:
        negX.append(-x)

negX.sort()

len_posX = len(posX)
len_negX = len(negX)

ans = float('inf')
for pos_k in range(len_posX + 1):
    neg_k = K - pos_k
    if len_negX <= neg_k - 1:
        continue

    if neg_k == 0 and len_posX != 0:
        ans = min(ans, posX[pos_k - 1])
    elif pos_k == 0 and len_negX != 0:
        ans = min(ans, negX[neg_k - 1])
    elif len_posX != 0 and len_negX != 0:
        ans = min(ans, posX[pos_k - 1] + 2 * negX[neg_k - 1], 2 * posX[pos_k - 1] + negX[neg_k - 1])

print(ans)
# Original solution
# -*- coding:utf-8 -*-
import copy

INF = 2*(10**8)+1
memo = INF  # メモ化用変数

def solve():
    N, K = list(map(int, input().split(" ")))
    X = [0]
    X.extend(list(map(int, input().split(" "))))
    X.sort()

    # 幅優先探索すればいいんじゃね？

    def dfs(depth, cost, i, Xs):
        global memo
        if depth == K:
            memo = min(cost, memo)
            return cost
        
        if cost >= memo:
            # 探索を打ち切る
            return INF
        
        ret1, ret2 = INF, INF
        if i+1 < len(Xs):
            # 右に移動
            tmpCost = abs(Xs[i] - Xs[i+1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret1 = dfs(depth+1, cost+tmpCost, i, tmpXs)
        if i-1 >= 0:
            # 左に移動
            tmpCost = abs(Xs[i] - Xs[i-1])
            tmpXs = copy.deepcopy(Xs)
            del tmpXs[i]
            ret2 = dfs(depth+1, cost+tmpCost, i-1, tmpXs)
        
        return min(ret1, ret2)

    # 0座標のインデックスは？
    for i in range(len(X)):
        if X[i] == 0:
            break
    ans = dfs(0, 0, i, X)
    print(ans)


if __name__ == "__main__":
    solve()

# Optimized solution
# -*- coding:utf-8 -*-

"""解説
https://www.youtube.com/watch?v=5O4k1n5Ejzw

区間を考える問題

-30 -10 0 10 20 50

・K = 3のとき、

0座標を必ず含める、ある連続するK個のロウソクについて考える

・-30 -10 0 10
・-10 0 10 20
・0 10 20 50

左端と右端に到達する最小値を求めていけばよくて、間のロウソクは無視してよい（必ず移動中に火をつけるので）

すべての場合について求めて、その中の最小値が答え
"""
def solve():
    N, K = list(map(int, input().split()))
    X = list(map(int, input().split()))
    if 0 in X:
        # 0座標にロウソクがあるなら、火をつけるロウソクをひとつ減らしてよい
        K -= 1
    else:
        X.append(0)
    X.sort()

    origin_i = X.index(0)
    left_i = max(origin_i-K, 0)

    INF = 10**12
    ans = INF
    while left_i <= origin_i:
        if left_i+K < len(X):
            tmp1 = abs(X[left_i]-0)*2 + abs(X[left_i+K]-0)
            tmp2 = abs(X[left_i]-0) + abs(X[left_i+K]-0)*2
        else:
            break
        ans = min(ans, tmp1, tmp2)
        left_i += 1
    print(ans)

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

for i in range(n):
    if x[i] > 0:
        neg_num = i
        break
pos_num = n - neg_num

l = min(k, neg_num)
res = 10 ** 13
for i in range(l, -1, -1):
    r = k - i
    if r <= pos_num:
        l_val = min(0, x[neg_num - i])
        r_val = max(0, x[neg_num + k - i - 1])
        tmp = min( -l_val * 2 + r_val, -l_val + r_val * 2)
        res = min(tmp, res)
print(res)

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

neg_to_pos = n
for i in range(n):
    if x[i] > 0:
        neg_to_pos = i
        break

res = 10 ** 14
for i in range(n - k + 1):
    l = min(x[i], 0)
    r = max(0, x[i + k - 1])
    if i <= neg_to_pos and neg_to_pos - 1 <= i + k - 1:
        tmp = min(-2 * l + r, -l + 2 * r)
        res = min(res, tmp) 
print(res)