CodeEff/ECCO huggingface dataset:
# Original solution
def tosec(t):
    return int(t[:2])*60*60 + int(t[3:5])*60 + int(t[6:])

while True:
    n = int(eval(input()))
    if n == 0: break
    time = [0] * (60*60*24 + 1)

    for _ in range(n):
        s, f = input().split()
        time[tosec(s)] += 1
        time[tosec(f)] -= 1

    ans = time[0]
    for i in range(60*60*24):
        time[i+1] += time[i]
        ans = max(ans, time[i+1])

    print(ans)

# Optimized solution
while True:
    n = eval(input())
    if not n: break 
    time = [0]*86400
    for i in range(n):
        s, t = input().split()
        ssec = int(s[0:2])*3600 + int(s[3:5])*60 + int(s[6:8])
        tsec = int(t[0:2])*3600 + int(t[3:5])*60 + int(t[6:8])
        time[ssec] += 1
        time[tsec] -= 1
    ans = time[0]
    for i in range(86400-1):
        time[i+1] += time[i]
        ans = max(ans, time[i+1])
    print(ans)
# Original solution
import re

while True:
    n = int(input())
    if n == 0:
        break
    l = [0] * (24*60*60+1)
    for _ in range(n):
        h1,m1,s1,h2,m2,s2 = list(map(int, re.split(':| ', input())))
        l[h1*60*60 + m1*60 + s1] += 1
        l[h2*60*60 + m2*60 + s2] -= 1
    r = 0
    for i in range(24*60*60):
        l[i+1] += l[i]
        r = max(r, l[i+1])
    print(r)

# Optimized solution
while True:
    n = int(input())
    if n == 0:
        break
    l = []
    for _ in range(n):
        hms1, hms2 = input().split()
        l.append((hms1, True))
        l.append((hms2, False))
    l.sort()
    r, t = 0, 0
    for i in range(len(l)):
        if l[i][1]:
            t += 1
        else:
            t -= 1
        r = max(r, t)
    print(r)
# Original solution
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))

dis = 300000000

for i in range(N-K+1):
    y = list(x[i:i+K])

    disl = abs(x[i]) + abs(x[i+K-1]-x[i])
    disr = abs(x[i+K-1]) + abs(x[i+K-1]-x[i])

    dis = min(dis, disl, disr)
#    if (y[0]*y[-1]<0):
#        disy = y[-1]-y[0]+min(abs(y[-1]), abs(y[0]))
#    else:
#        disy = max(abs(y[0]), abs(y[-1]))
#    dis = min(dis, disy)

print(dis)

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

dis = 300000000

for i in range(N-K+1):
    disl = abs(x[i]) + abs(x[i+K-1]-x[i])
    disr = abs(x[i+K-1]) + abs(x[i+K-1]-x[i])

    dis = min(dis, disl, disr)
#    if (y[0]*y[-1]<0):
#        disy = y[-1]-y[0]+min(abs(y[-1]), abs(y[0]))
#    else:
#        disy = max(abs(y[0]), abs(y[-1]))
#    dis = min(dis, disy)

print(dis)
# Original solution
def main():
    # editorial読んだ．
    N, K = list(map(int, input().split(" ")))
    coordinates = list(map(int, input().split(" ")))
    result = 10 ** 9 + 7
    for i in range(N - K + 1):
        result = min(result, abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i]),
                     abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i + K - 1]))
    print(result)


if __name__ == '__main__':
    main()

# Optimized solution
def main():
  N, K = list(map(int, input().split(" ")))
  X = list(map(int, input().split(" ")))
  answer = 10 ** 9 + 7
  for i in range(0, N - K + 1):
    time = X[i + K - 1] - X[i] + min(abs(X[i]), abs(X[i + K - 1]))
    answer = min(answer, time)
  print(answer)


if __name__ == "__main__":
  main()
# Original solution
def main():
    n, k = list(map(int, input().split()))
    x = [int(x) for x in input().split()]
    answer = float("inf")
    if 1 < n:
        for i in range(n - k + 1):
            if x[i + k - 1] < 0:
                answer = min(answer, -x[i])
            elif 0 < x[i]:
                answer = min(answer, x[i + k - 1])
            else:
                answer = min(answer, min(abs(x[i]), abs(x[i + k - 1])) + x[i + k - 1] - x[i])
    else:
        answer = abs(x[0])
    print(answer)


if __name__ == '__main__':
    main()



# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    x = [int(i) for i in input().split()]
    x.sort()
    answer = float("inf")
    if n == k:
        if n == 1:
            answer = abs(x[0])
        if 0 <= x[0]:
            answer = x[-1]
        elif x[-1] <= 0:
            answer = abs(x[0])
        else:
            answer = x[-1] - x[0] + min(abs(x[-1]), abs(x[0]))
    else:
        for i in range(n - k + 1):
            left = x[i]
            right = x[i + k - 1]
            if left * right >= 0:
                answer = min(answer, max(abs(left), abs(right)))
            else:
                answer = min(answer, right - left + min(abs(left), abs(right)))
    print(answer)


if __name__ == '__main__':
    main()
# Original solution
N,K = list(map(int,input().split()))
ary = [0]+list(map(int,input().split()))
dp = [[0]*(N+1) for _ in range(N+1)]
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N+1):
    for j in range(N+1):
        dp[i][j] = abs(ary[i]-ary[j])
for i in range(N+1):
        if i == a:continue
        k = abs(i - a)
        d = abs(ary[i])
        if k >= K and ans > d:
            ans = d
        if i <= a:
            k = K - abs(i-a)
            d = abs(ary[i])
            if k > 0:
                d += dp[i][a+k]
                if ans > d:
                    ans = d
print((0 if ans == float("Inf") else ans))

# Optimized solution
N, K = list(map(int, input().split()))
ary = [0] + list(map(int, input().split()))
ary = sorted(ary)
a = ary.index(0)
ans = float("Inf")
for i in range(N + 1):
    if i == a: continue

    k = abs(i - a)
    d = abs(ary[i])
    if k >= K and ans > d:
        ans = d
    if i < a:
        k = K - abs(i - a)
        d = abs(ary[i])
        if 0 < k < len(ary[a:]):
            d += abs(ary[i] - ary[k + a])
            if ans > d:
                ans = d

print((0 if ans == float("Inf") else ans))
# Original solution
N, K = list(map(int, input().split()))
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
ary = list(map(int, input().split()))
ary2 = sorted(ary+[0])
zero = ary2.index(0)
k = [abs(i - zero) if i < zero else ((i + 1) - zero)for i in range(N)]
ans = float("Inf")
for i in range(N - K + 1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)])
for i in range(N-1, K-2, -1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i - (K-1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i - (K-1)])
print(ans)
# Original solution
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))

tmp = [0] * k
ans = float("inf")
for i in range(n-k+1):
  tmp = a[i:i+k]
  if (tmp[0] <= 0 and tmp[-1] <= 0) or (tmp[0] >= 0 and tmp[-1] >= 0):
    cost = max(abs(tmp[0]), abs(tmp[-1])) 
  else:
    cost = min(abs(tmp[0]), abs(tmp[-1]))*2 + max(abs(tmp[0]), abs(tmp[-1]))
    
  if ans > cost:
    ans = cost
    
print(ans)

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


ans = float("inf")
for i in range(n-k+1):
  #tmpの左端と右端の符号が同じなら、端から移動していく
  if (a[i] <= 0 and a[i+k-1] <= 0) or (a[i] >= 0 and a[i+k-1] >= 0):
    cost = max(abs(a[i]), abs(a[i+k-1]))
  #選んだろうそくが正負にまたがる場合、左端と右端の近いほうに移動してから  
  else:
    cost = min(abs(a[i]), abs(a[i+k-1]))*2 + max(abs(a[i]), abs(a[i+k-1]))
    
  if ans > cost:
    ans = cost
    
print(ans)
# Original solution
import sys, os, bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n , k = f()
    a = f()

    ans = int(1e10)
    for i in range(n):
        if i+k-1<n:
            cur_ans = 0
            if a[i]* a[i+k-1]>=0:
                cur_ans = max(abs(a[i+k-1]),abs(a[i]))
            else:
                cur_ans = min(2*abs(a[i])+abs(a[i+k-1]), abs(a[i]) + 2*abs(a[i+k-1]))
            ans = min(ans, cur_ans)
    
    print(ans)

solve()


# Optimized solution
import sys, os, bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n , k = f()
    a = f()

    ans = int(1e10)
    for i in range(n):
        if i+k-1<n:
            ans = min(ans,  abs(a[i]) + abs(a[i] - a[i+k-1]), abs(a[i+k-1]) + abs(a[i] - a[i+k-1]))

    print(ans)

solve()
# Original solution
def solve():

    N, K = [int(i) for i in input().split()]
    xlist = list(map(int, input().split()))

    cost = float("inf")

    for i in range(N-K+1):
        j = i+K-1
        cost = min(
            abs(xlist[i])+abs(xlist[j]-xlist[i]),
            abs(xlist[j])+abs(xlist[i]-xlist[j]),
            cost
        )

    print(cost)


if __name__ == "__main__":
    solve()


# Optimized solution
def solve():

    N, K = [int(i) for i in input().split()]
    xlist = [int(i) for i in input().split()]

    cost = float("inf")

    for i in range(N-K+1):
        j = i+K-1
        cost = min(
            cost,
            abs(xlist[i])+abs(xlist[j]-xlist[i]),
            abs(xlist[j])+abs(xlist[i]-xlist[j])
        )

    print(cost)


if __name__ == "__main__":
    solve()
# Original solution
N,k = list(map(int,input().split()))
a = list(map(int,input().split()))
i = 0
while i < N:
    if a[i] >= 0:
        break
    i += 1
x = []
y = []
for j in range(i-k,i+1):
    if j>=0 and j+k-1<N:
        x = a[j:j+k]
        if x[0] < 0 and x[k-1] > 0:
            y.append(min(-x[0],x[k-1])*2 + max(-x[0],x[k-1]))
        else:
            y.append(max(-x[0],x[k-1]))
print((min(y)))

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

if 0 in x:
    K -= 1

cand = []
hasleft = x[0] < 0
hasright = x[N-1] > 0
if K == 0:
    cand = [0]
else:
    for i in range(N-K+1):
        left = x[i]
        right = x[i+K-1]
        if left < 0 and right > 0:
            cand.append(min(abs(left)*2+right, right*2+abs(left)))
        elif right < 0:
            cand.append(abs(left))
        elif left > 0:
            cand.append(right)
print((min(cand)))
# Original solution
# ABC107C - Candles (ARC101C)
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    x = tuple(map(int, input().rstrip().split()))
    ans = float("inf")
    for i in range(n - k + 1):
        a, b = x[i], x[i + k - 1]
        if a < b < 0 or 0 < a < b:
            ans = min(ans, max(abs(a), abs(b)))
        else:
            ans = min(ans, max(abs(a), abs(b)) + min(abs(a), abs(b)) * 2)
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC107C - Candles (ARC101C)
def main():
    _, k = list(map(int, input().split()))
    x = tuple(map(int, input().split()))
    print((min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :]))))


if __name__ == "__main__":
    main()
# Original solution
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = list(map(int, open(0).read().split()))
    ans = min(r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :]))
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = list(map(int, open(0).read().split()))
    cand = [r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :])]
    ans = min(cand)
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n, k = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
INF = 10 ** 9

def sign(x):
    return int(x >= 0)

ans = INF
for i in range(n - k + 1):
    p = k + i - 1
    if sign(x[i]) == sign(x[p]):
        t = max(abs(x[i]), abs(x[p]))
    else:
        t = min(-x[i] * 2 + x[p], -x[i] + x[p] * 2)
    if t < ans:
        ans = t

print(ans)

# Optimized solution
n, k = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
INF = 10 ** 9

ans = INF
for li in range(n - k + 1):
    ri = k + li - 1
    t = min(abs(x[li]) + abs(x[ri] - x[li]), abs(x[ri]) + abs(x[li] - x[ri]))
    ans = min(t, ans)

print(ans)
# Original solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
    temp = a[i:i+k]
    l = temp[0]
    r = temp[-1]
    dist_l = abs(l) + abs(l-r)
    dist_r = abs(r) + abs(l-r)
    dist = min(dist_l, dist_r)
    b.append(dist)
print((min(b)))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
    dist = a[i+k-1] - a[i] + min(abs(a[i]), abs(a[i+k-1]))
    b.append(dist)
print((min(b)))
# Original solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
b = []
for i in range(n-k+1):
    dist = a[i+k-1] - a[i] + min(abs(a[i]), abs(a[i+k-1]))
    b.append(dist)
print((min(b)))

# Optimized solution
n, k = list(map(int, input().split()))
x = [int(i) for i in input().split()]
d = []
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    d.append(abs(l-r) + min(abs(l), abs(r)))
print((min(d)))
# 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):
  d=x[i+k-1]-x[i]
  d+=min(abs(x[i]),abs(x[i+k-1]))
  ans=min(ans,d)
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):
  x_max=x[i+k-1]
  x_min=x[i]

  t_ans=x_max-x_min
  t_ans+=min(abs(x_max),abs(x_min))

  ans=min(ans,t_ans)

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


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    plus = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            plus.append(x[i])
            j += 1

    j = 0
    minus = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            minus.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(0, K + 1):
        if len(plus) - 1 < i:
            continue

        if len(minus) - 1 < K - i:
            continue


        rsum = plus[i]
        lsum = abs(minus[K - i])

        if rsum >= lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()



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


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ans = float("inf")
    for i in range(N + 1 - K):
        l = x[i]
        r = x[i + K - 1]
        if l < 0 < r:
            ans = min(ans, abs(l) + abs(r) + min(abs(l), abs(r)))
        else:
            ans = min(ans, max(abs(l), abs(r)))

    print(ans)

    


if __name__ == '__main__':
    main()
# Original solution
n, k = list(map(int,input().split()))
x = list(map(int,input().split()))
time = 10**9

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

    time = min(t, time)
    
print(time)


# Optimized solution
n, k = list(map(int,input().split()))
x = list(map(int,input().split()))
time = 10**9
x_k = [0, 0]
for i in range(n - k + 1):
    x_k[0] =  x[i] 
    x_k[1] =  x[i+k-1] 
    #print(x_k)
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)
# Original solution
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
    tl = l[i:i + k]
    min_l = min(0, tl[0])
    max_l = max(0, tl[-1])
    min_abs = min(abs(min_l), abs(max_l))
    max_abs = max(abs(min_l), abs(max_l))
    ans = min(ans, min_abs * 2 + max_abs)
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 9999999999999
for i in range(len(l) - k + 1):
    min_l = min(0, l[i])
    max_l = max(0, l[i+k-1])
    min_abs = min(abs(min_l), abs(max_l))
    max_abs = max(abs(min_l), abs(max_l))
    ans = min(ans, min_abs * 2 + max_abs)
print(ans)
# Original solution
def main():
    n, k, *x = list(map(int, open(0).read().split()))
    f = lambda i: x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i]))
    l = f(0)

    for i in range(n - k + 1):
        y = f(i)
        if y < l:
            l = y

    print(l)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n, k, *x = list(map(int, open(0).read().split()))
    l = x[0 + k - 1] - x[0] + min(abs(x[0 + k - 1]), abs(x[0]))

    for i in range(n - k + 1):
        y = x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i]))
        if y < l:
            l = y

    print(l)


if __name__ == '__main__':
    main()
# Original solution
n,k,*x=list(map(int,open(0).read().split()))

if x[0]>=0:
  ans=x[k-1]
elif x[-1]<=0:
  ans=abs(x[~(k-1)])

elif n==k:
  l=abs(x[0])
  r=abs(x[-1])
  ans=min(l+l+r,r+r+l)
else:
  ans=10**10
  for i in range(n-k+1):
    l=x[i]
    r=x[i+k-1]
    if l<=0<=r:
      ll=abs(l)
      rr=abs(r)
      tmp=min(ll+2*rr,2*ll+rr)
      ans=min(ans,tmp)
      
print(ans)
  

# Optimized solution
n,k,*x=list(map(int,open(0).read().split()))
if x[0]>=0:
  ans=x[k-1]
elif x[-1]<=0:
  ans=abs(x[~(k-1)])
else:
  ans=10**10
  for i in range(n-k+1):
    l=x[i]
    r=x[i+k-1]
    if l<=0<=r:
      ll=abs(l)
      rr=abs(r)
      tmp=min(ll+2*rr,2*ll+rr)
      ans=min(ans,tmp)
      
print(ans)
# Original solution
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = list(map(int, input().split()))
x = list(map(int, input().split()))
dp = [float("inf")] * (K + 2)
s = bl(x, 0)
l = 0
r = K - 1
if r + 1 < s:
  l = s - 1 - r
  r = s - 1
def calc(l, r):
  if x[l] * x[r] < 0:
    return min(abs(x[l]), abs(x[r])) * 2 + max(abs(x[l]), abs(x[r]))
  else:
    return max(abs(x[l]), abs(x[r]))
dp[0] = calc(l, r)
for i in range(1, K + 2):
  l += 1
  r += 1
  if l > s + 1 or r >= N:
    break
  dp[i] = calc(l, r)
print((min(dp)))

# Optimized solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
  if a[i] * a[i + K - 1] < 0:
    mn = min(abs(a[i]), abs(a[i + K - 1]))
    mx = max(abs(a[i]), abs(a[i + K - 1]))
    res = min(res, mn * 2 + mx)
  else:
    res = min(res, max(abs(a[i]), abs(a[i + K - 1])))
print(res)
# Original solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
res = float("inf")
for i in range(N - K + 1):
  if a[i] * a[i + K - 1] < 0:
    mn = min(abs(a[i]), abs(a[i + K - 1]))
    mx = max(abs(a[i]), abs(a[i + K - 1]))
    res = min(res, mn * 2 + mx)
  else:
    res = min(res, max(abs(a[i]), abs(a[i + K - 1])))
print(res)

# Optimized solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
z = -1
res = float("inf")
for r in range(N):
  if a[r] < 0:
    z = r
    continue
  if r < K - 1: continue
  x = r - z
  y = a[z - (K - x) + 1]
  if K == x: y = 0
  t = min(abs(a[r]), abs(y))
  if r - z >= K: t = 0
  res = min(res, abs(a[r]) + abs(y) + t)
for l in range(N):
  if z - l + 1 == K:
    res = min(res, abs(a[l]))
    break
print(res)
# Original solution
n,k=list(map(int,input().split()))
candles=list(map(int,input().split()))
rows=[]
minimum=[]
for i in range(n-k+1):
        new=candles[i:i+k]
        l_r=abs(new[0])+abs(new[-1]-new[0])
        r_l=abs(new[-1])+abs(new[-1]-new[0])
        minimum.append(min(l_r,r_l))

print((min(minimum)))

# Optimized solution
N, K, *x = list(map(int, open(0).read().split()))
ans = 1e9
for i in range(N - K + 1):
    l = x[i]
    r = x[i + K - 1]
    ans = min(ans, abs(l) + (r - l), abs(r) + (r - l))
    
print(ans)
# Original solution
import sys
input = sys.stdin.readline
N,K = list(map(int,input().split()))
X = list(map(int,input().split()))
ans = 10**9
for i in range(N-K+1):
    #print(X[i:i+K])
    if min(X[i:i+K]) >= 0:
        #print('a')
        ans = min(ans,max(X[i:i+K]))
    elif max(X[i:i+K]) <= 0:
        #print('b')
        ans = min(ans,abs(min(X[i:i+K])))
    else:
        #print('c')
        ans = min(ans,abs(min(X[i:i+K])*2)+max(X[i:i+K]))
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):
    ans = min(ans,X[i+K-1]-X[i]+min(abs(X[i]),abs(X[i+K-1])))
print(ans)
# Original solution
n,k = list(map(int,input().split()))
x = [int(x) for x in input().split()]

mini = 10**9
for i in range(n-k+1):
    ls = x[i:i+k]
    flg1 = abs(ls[0])+abs(ls[0]-ls[k-1])
    flg2 = abs(ls[k-1])+abs(ls[0]-ls[k-1])
    flg = min(flg1,flg2)
    mini = min(flg,mini)
print(mini)

# Optimized solution
n,k = list(map(int,input().split()))
ls = [int(x) for x in input().split()]

mini = 10**9
for i in range(n-k+1):
    flg1 = abs(ls[i])+abs(ls[i]-ls[i+k-1])
    flg2 = abs(ls[i+k-1])+abs(ls[i]-ls[i+k-1])
    mini = min(flg1,flg2,mini)
print(mini)
# Original solution
def solve():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    min_dist = 10 ** 9
    for i in range(n - k + 1):
        part = xs[i:i + k]
        length = abs(part[0] - part[-1])
        dist = min(abs(part[0]) + length, abs(part[-1]) + length)
        if dist < min_dist:
            min_dist = dist
    return min_dist


if __name__ == "__main__":
    print((solve()))


# Optimized solution
def solve():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    min_dist = 10 ** 9
    for i in range(n - k + 1):
        length = abs(xs[i] - xs[i + k - 1])
        dist = min(abs(xs[i]) + length, abs(xs[i + k - 1]) + length)
        if dist < min_dist:
            min_dist = dist
    return min_dist


if __name__ == "__main__":
    print((solve()))
# Original solution
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        vs = xs[i:i+k]
        if vs[0] * vs[-1] < 0:
            l = abs(vs[0]) + abs(vs[-1]) + min(abs(vs[0]), abs(vs[-1]))
        else:
            l = max(abs(vs[0]), abs(vs[-1]))

        m = min(m, l)

    print(m)
    
main()


# Optimized solution
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        if xs[i] * xs[i+k-1] < 0:
            l = abs(xs[i]) + abs(xs[i+k-1]) + min(abs(xs[i]), abs(xs[i+k-1]))
        else:
            l = max(abs(xs[i]), abs(xs[i+k-1]))

        m = min(m, l)

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

ans = float("inf")
for i in range(n-k+1):
  # 0の位置から移動し、i〜i+kまでのろうそくに火をつけた場合の移動時間について左右で確認し、最小を求める
  l = abs(x[i]) + abs(x[i]-x[i+k-1])
  r = abs(x[i+k-1]) + abs(x[i] - x[i+k-1])
  ans = min(ans, l, r)
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 = abs(x[i]) + abs(x[i]-x[i+k-1])
  r = abs(x[n-1-i]) + (x[n-1-i]-x[n-k-i])
  ans = min(ans,l,r)
print(ans)