Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

N,M=map(int,input().split())
first=[]
second=[]
for m in range(M):
    a,b=map(int,input().split())
    if a==1:
        first.append(b)
    elif b==N:
        second.append(a)
count=0
for n in first:
    if n in second:
        print('POSSIBLE')
        count=1
        break
if count==0:
    print('IMPOSSIBLE')


### FIXED CODE ###

N,M=map(int,input().split())
first=[]
second=[]
for m in range(M):
    a,b=map(int,input().split())
    if a==1:
        first.append(b)
    elif b==N:
        second.append(a)
first_and_second=set(first)&set(second)
if len(first_and_second)==0:
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')
### BUGGY CODE ###

import numpy as np

N, Q = map(int, input().split())
ans = np.zeros((N+1), dtype=np.int)
graph = {i:[None, []] for i in range(1, N+1)}
nums = [False] * (N)
visited = [False] * (N+1)

for i in range(N-1):
    a, b = map(int, input().split())
    graph[b][0] = a
    graph[a][1].append(b)
    nums[a-1] = True
        
q = [idx+1 for idx, _ in enumerate(nums) if not _]

while q:
    n = q.pop(0)
    visited[n] = True

    par, child = graph[n]
    if not par is None and not visited[par]:
        graph[par][1].extend(child)
        q.append(par)
        
for i in range(Q):
    p, x = map(int, input().split())
    ans[p] += x
    ans[graph[p][1]] += x
    
for n in ans[1:]:
    print(n, '', end='')

### FIXED CODE ###

from collections import deque

def main():
  N, Q = map(int, input().split())
  visited = [False] * (N+1)
  graph = {i:deque() for i in range(N+1)}

  for i in range(N-1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

  visited[1] = True

  c = [0] * (N+1)
  for i in range(Q):
    p, x = map(int, input().split())
    c[p] += x
    
  q = [1]
  while q:
    per = q.pop()
    for child in graph[per]:
      if not visited[child]:
        visited[child] = True
        c[child] += c[per]
        q.append(child)

  for i in range(1, N+1):
    print(c[i], '', end='')

if __name__ == '__main__':
  main()
### BUGGY CODE ###

# -*- 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(input())
def MAP(): return 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 = float('inf')
MOD = 10 ** 9 + 7

N, M = MAP()
nodes = tuple([] for i in range(N))
for i in range(M):
    a, b = MAP()
    a -= 1
    b -= 1
    nodes[a].append(b)
    nodes[b].append(a)

# 二部グラフ判定
color = [-1] * N
def check(u):
    stack = [(u, -1, 0)]
    while stack:
        u, prev, c = stack.pop()
        if color[u] == -1:
            color[u] = c
        elif color[u] != c:
                return False
        for v in nodes[u]:
            if v != prev:
                stack.append((v, u, 1-c))
    return True

if check(0):
    a = sum(color)
    b = N - a
    ans = a*b - M
else:
    ans = N*(N-1) // 2 - M
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-

import sys
from collections import Counter

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 ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,M=MAP()
nodes=[[] for i in range(N)]
for i in range(M):
    a,b=MAP()
    nodes[a-1].append(b-1)
    nodes[b-1].append(a-1)

visited=[False]*N
team=['']*N

def rec(u):
    if visited[u]:
        return True
    visited[u]=True
    for v in nodes[u]:
        if team[u]==team[v]:
            return False
        if team[u]=='a':
            team[v]='b'
        elif team[u]=='b':
            team[v]='a'
        if not rec(v):
            return False
    return True

# 1からnまでの等差数列の和
def get_sum(n):
    return (1+n)*n//2

team[0]='a'
if rec(0):
    C=Counter(team)
    ans=C['a']*C['b']-M
else:
    ans=get_sum(N-1)-M
print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
S = input()

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(N):
    cnt = 0
    change = 0
    if i != 0 and S[i] == "0":
      continue
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if S[i] == "0" and change < K or S[i] == "1" and change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
S = input()

nums = []
now = 1
cnt = 0
for i in range(N):
  if S[i] == str(now):
    cnt += 1
  else:
    nums.append(cnt)
    now = 1 - now
    cnt = 1
if cnt != 0:
  nums.append(cnt)

if len(nums) % 2 == 0:
  nums.append(0)

add = 2 * K + 1

sums = [0 for _ in range(len(nums) + 1)]
for i in range(len(nums)):
  sums[i + 1] = sums[i] + nums[i]

ans = 0
for i in range(len(nums)):
  if i % 2 == 1:
    continue
  
  left = i
  right = min(i + add, len(nums))
  tmp = sums[right] - sums[left]

  ans = max(tmp, ans)

print(ans)
### BUGGY CODE ###

N = int(input())


for n in range(1,N+1,1):
    cnt = 0
    for x in range(1,N+1,1):
        for y in range(1, N+1,1):
            for z in range(1, N+1,1):
                if x**2 + y**2 + z**2 + x*y + y*z + z*x == n:
                    cnt +=1
    print(cnt)

### FIXED CODE ###

n = int(input())
ans = [0 for _ in range(10050)]
for i in range(1,105):
    for j in range(1,105):
        for k in range(1,105):
            v = i*i+j*j+k*k+i*j+j*k+k*i;
            if v<10050:
                ans[v]+=1
for i in range(n):
    print(ans[i+1])
### BUGGY CODE ###

K,S=map(int,input().split())
print(len([1 for i in range(K+1) for j in range(K+1) for k in range(K+1) if i+j+k==S]))


### FIXED CODE ###

K,S=map(int,input().split()) 
print(len([True for x in range(K+1) for y in range(K+1) if (S-x-y>=0) and (S-x-y<=K)]))
### BUGGY CODE ###

from operator import itemgetter
import bisect
N,M = [int(x) for x in input().split()]

work = [[int(x) for x in input().split()]for _ in range(N)]
#%%
dp = [0]*(M+1)
work.sort(key=itemgetter(0))
work.sort(key=itemgetter(1))
#%%
days = [i[0] for i in work]
seen = set()
cnt = 0
for i in range(1,M+1):
    nonused = []
    while(len(work)>0):
        x = work.pop()
        if(x[0]>i):
            nonused.insert(0,x)
        else:
            break
    #print(i,x)
    if(x[0]<=i):
        cnt += x[1]
    work += nonused

print(cnt)


### FIXED CODE ###

from operator import itemgetter
import bisect
import heapq
import sys
input = sys.stdin.readline

def main():
    N,M = [int(x) for x in input().split()]
    work = {}
    for _ in range(N):
        a,b = [int(x) for x in input().split()]
        if(a in work):
            work[a].append(-b)
        else:
            work[a] = [-b]

    cand = []
    heapq.heapify(cand)
    cnt = 0
    for i in range(1,M+1):
        if(i in work):
            for x in work[i]:
                heapq.heappush(cand,x)
        if(len(cand)==0):
            continue
        x = -heapq.heappop(cand)
        cnt += x

    print(cnt)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

n = int(input())
s = ["" for i in range(n)]
ans = 0
for i in range(n):
  a = input()
  if a not in s:
    s[i] = a
    ans += 1
print(ans)

### FIXED CODE ###

n = int(input())
s = [input() for i in range(n)]
print(len(set(s)))
### BUGGY CODE ###

n=int(input())
list1=list(map(int,input().split()))
ln=[list1[0]]
for i in list1:
    if min(ln)>=i:
        ln.append(i)
print(len(ln)-1)

### FIXED CODE ###

n=int(input())
list1=list(map(int,input().split()))
mi=list1[0]
c=0
for i in list1:
    if mi>=i:
        mi=i
        c+=1
print(c)
### BUGGY CODE ###

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


def Cum(X):
    temp = 0
    Cum = [0]*len(X)
    for i in range(len(X)):
        temp += X[i]
        Cum[i] += temp
    return Cum

a = Cum(A)
a.insert(0,0)
b = Cum(B)
b.insert(0,0)

na = 0
for i in range(N+1):
    if i == N and a[i]<=K:
        na = N
    elif a[i]<=K and a[i+1]>K:
        na = i

ans = na

nb = 0
for i in range(na,-1,-1):
    temp = K-a[i]
    for j in range(nb,M+1):
        if j == M and b[j] <= temp:
            nb = M
        elif b[j]<=temp and b[j+1]>temp:
            nb = j
        ans = max(ans,i+nb)
        
print(ans)

### FIXED CODE ###

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


def Cum(X):
    temp = 0
    Cum = [0]*len(X)
    for i in range(len(X)):
        temp += X[i]
        Cum[i] += temp
    return Cum

a = Cum(A)
a.insert(0,0)
b = Cum(B)
b.insert(0,0)

na = 0
for i in range(N+1):
    if i == N and a[i]<=K:
        na = N
    elif a[i]<=K and a[i+1]>K:
        na = i

ans = na

nb = 0
for i in range(na,-1,-1):
    temp = K-a[i]
    for j in range(nb,M+1):
        if j == M and b[j] <= temp:
            nb = M
            ans = max(ans,i+nb)
        elif b[j]<=temp and b[j+1]>temp:
            nb = j
            ans = max(ans,i+nb)
            break
        
print(ans)
### BUGGY CODE ###

from copy import copy
from collections import deque
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
first = deque(L)
count = 0
for i in range(N-2):
    l = first.popleft()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.popleft()
        third = copy(second)
        for k in range(N-i-j-1):
            n = third.popleft()
            if n > l - m:
                count += 1
            else:
                break
print(count)

### FIXED CODE ###

from copy import copy
from collections import deque
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
first = deque(L)
count = 0
for i in range(N-2):
    l = first.pop()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.pop()
        n = len(second) - bisect.bisect_right(second,l-m)
        if n > 0:
            count += n
        else:
            break
print(count)
### BUGGY CODE ###

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


a1 = a.copy()
cnt1 = 0
sum1 = 0
if a1[0] == 0:
    a1[0] = 1
    cnt1 +=1

a2 = a.copy()
cnt2 = 0
sum2 = 0
if a2[0] == 0:
    a2[0] = -1
    cnt2 +=1
    
    
for i in range(n-1):
    sum1 = sum(a1[:i+1])
    if sum1 > 0 and (sum1+a1[i+1]) >= 0:
        cnt1 += abs(((sum1 * -1)-1) - a1[i+1])
        a1[i+1] = ((sum1 * -1)-1)
    
    elif sum1 < 0 and (sum1 + a1[i+1]) <= 0:
        cnt1 += abs(abs(sum1)+1 - a1[i+1])
        a1[i+1] = abs(sum1)+1

for i in range(n-1):
    sum2 = sum(a2[:i+1])
    if sum2 > 0 and (sum2+a2[i+1]) >= 0:
        cnt2 += abs(((sum2 * -1)-1) - a2[i+1])
        a2[i+1] = ((sum2 * -1)-1)
        
    elif sum2 < 0 and (sum2+a2[i+1]) <= 0:
        cnt2 += abs(abs(sum2)+1 - a2[i+1])
        a2[i+1] = abs(sum2)+1
        
print(min(cnt1,cnt2))
        

### FIXED CODE ###

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

gokei = 0
ans1 = 0

for i in range(N):
    gokei += A[i]
    # 偶数番目を負に
    if i % 2 == 0:
        if gokei >= 0:
            ans1 += abs(-1 - gokei)
            gokei -= abs(-1 - gokei)
    if i % 2 == 1:
        if gokei <= 0:
            ans1 += 1 - gokei
            gokei += (1 - gokei)


gokei = 0
ans2 = 0
for i in range(N):
    gokei += A[i]
    # 偶数番目を正に
    if i % 2 == 0:
        if gokei <= 0:
            ans2 += 1 - gokei
            gokei += (1 - gokei)
    else:
        if gokei >= 0:
            ans2 += abs(-1 - gokei)
            gokei -= abs(-1 - gokei)

print(min(ans1, ans2))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10000)
n = int(input())
*A, = map(int, input().split())
DP = [[None for j in range(n+1)] for i in range(n)]
def dp(i, j):
    if i == j:
        return 0
    if DP[i][j] != None:
        return DP[i][j]
    DP[i][j] = max(A[i]-dp(i+1, j), A[j-1]-dp(i, j-1))
    return DP[i][j]
print(dp(0, n))


### FIXED CODE ###

n = int(input())
*A, = map(int, input().split())
DP = [[0 for j in range(n+1)] for i in range(n+1)]
for l in range(1, n+1):
    for i in range(n-l+1):
        DP[i][i+l] = max(A[i]-DP[i+1][i+l], A[i+l-1]-DP[i][i+l-1])
print(DP[0][n])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int, input().split())))

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in dic:
        buf = dic[str(bc[i][0])]
        dic[str(bc[i][0])] = 0
        if str(bc[i][1]) in dic:
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

    for j in dic:
        ans += int(j) * dic[j]
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int, input().split())))

a_sum = sum(a)

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in dic.keys():
        buf = dic[str(bc[i][0])]
        if str(bc[i][1]) in dic.keys():
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

        a_sum += buf*(-bc[i][0] + bc[i][1])
        dic[str(bc[i][0])] = 0


    print(a_sum)
### BUGGY CODE ###

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

  def gcd(a,b):
    a,b=max(a,b),min(a,b)
    if b==0:
        return a
    else:
        return gcd(b,a%b)
  
  def gcd2(num,n):
    if n==1:
      return num[0]
    elif n==2:
      return gcd(num[0],num[1])
    else:
      return gcd2([gcd(num[i],num[n-i-1]) for i in range((n+1)//2)],(n+1)//2)

  ans=0
  for i in range(n):
    a=num.copy()
    a.pop(i)
    #print(a)
    tmp=gcd2(a,n-1)
    if tmp>ans:
      ans=tmp
  print(ans)  
  
if __name__=='__main__':
  main()

### FIXED CODE ###

def main():
  from fractions import gcd
  n=int(input())
  num=list(map(int,input().split()))
      
  gcdl=[0]*n
  gcdr=[0]*n
  gcdl[0],gcdr[0]=num[0],num[-1]
  for i in range(n-1):
    gcdl[i+1]=gcd(num[i+1],gcdl[i])
    gcdr[i+1]=gcd(num[-i-2],gcdr[i])

  ans=0
  for i in range(n):
    if i!=0 and i!=n-1:
      test=gcd(gcdl[i-1],gcdr[-i-2])
    elif i==0:
      test=gcdr[-2]
    else:
      test=gcdl[-2]

    if test>ans:
      ans=test
  print(ans)
 
if __name__=='__main__':
  main()
### BUGGY CODE ###

from copy import copy

X, Y, A, B, C = [int(n) for n in input().split(' ')]
p = [int(n) for n in input().split(' ')]
q = [int(n) for n in input().split(' ')]
r = [int(n) for n in input().split(' ')]

p.sort()
q.sort()

ans = 0
for flag in range(1<<len(r)):
    P = copy(p[-X:])
    Q = copy(q[-Y:])
    for i in range(len(r)):
        if flag & 1<<i:
            P += [r[i]]
        else:
            Q += [r[i]]
    P.sort()
    Q.sort()
    sums = sum(P[-X:]) + sum(Q[-Y:])
    if sums > ans:
        ans = sums

print(ans)


### FIXED CODE ###

from copy import copy

X, Y, A, B, C = [int(n) for n in input().split(' ')]
p = [int(n) for n in input().split(' ')]
q = [int(n) for n in input().split(' ')]
r = [int(n) for n in input().split(' ')]

p.sort()
q.sort()

apple = p[-X:] + q[-Y:] + r
apple.sort()

print(sum(apple[-X-Y:]))
### BUGGY CODE ###

# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(S, Q, Qs):
    rev_flg = False
    ans = S
    for q in Qs:
        if q[0] == 1:
            rev_flg = not rev_flg
        else:
            bf = q[1]
            if (bf == 1 and not rev_flg) or (bf == 2 and rev_flg):
                ans = q[2] + ans
            else:
                ans = ans + q[2]
    if rev_flg:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    S = input()
    Q = int(input())
    Qs = []
    for _ in range(Q):
        s = input()
        if s[0] == '1':
            q = [1]
        else:
            q = s.split()
            q[0] = int(q[0])
            q[1] = int(q[1])

        Qs.append(q)
    solve(S, Q, Qs)


### FIXED CODE ###

# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(S, Q, Qs):
    rev_flg = False
    bef = ''
    aft = ''
    for q in Qs:
        if q[0] == 1:
            rev_flg = not rev_flg
            bef, aft = aft, bef
        else:
            bf = q[1]
            if bf == 1:
                bef += q[2]
            elif bf == 2:
                aft += q[2]
    bef = bef[::-1]
    if rev_flg:
        S = S[::-1]
    print(bef + S + aft)


    # for q in Qs:
    #     if q[0] == 1:
    #         rev_flg = not rev_flg
    #     else:
    #         bf = q[1]
    #         if (bf == 1 and not rev_flg) or (bf == 2 and rev_flg):
    #             ans = q[2] + ans
    #         else:
    #             ans = ans + q[2]
    # if rev_flg:
    #     ans = ans[::-1]
    # print(ans)


if __name__ == '__main__':
    # # handmade
    # S = 'h' * 10 ** 5
    # Q = 2 * 10 ** 5
    # Qs = []
    # for i in range(Q - 1):
    #     if i % 2 == 0:
    #         Qs.append([2, 1, 'a'])
    #     else:
    #         Qs.append([2, 2, 'b'])
    # Qs.append([1])

    # input
    S = input()
    Q = int(input())
    Qs = []
    i = 0
    for _ in range(Q):
        s = input()
        i += 1
        if s[0] == '1':
            q = [1]
        else:
            q = s.split()
            q[0] = int(q[0])
            q[1] = int(q[1])

        Qs.append(q)
    solve(S, Q, Qs)
### BUGGY CODE ###

import math
n,a,b=map(int,input().split())

count=0
mod=1000000007

def C(n,a):
    x=1
    y=1
    for i in range(a):
        x*=n-i
        y*=i+1
    return x*pow(y,mod-2,mod)%mod

count+=pow(2,n,mod)
#print(count)
count-=1
count-=C(n,a)
#print(count)
count-=C(n,b)

print(max(0,(count)%mod))

### FIXED CODE ###

import math
n,a,b=map(int,input().split())

count=0
mod=1000000007

def C(n,a):
    x=1
    y=1
    for i in range(a):
        x*=n-i
        y*=i+1
        x%=mod
        y%=mod
    return x*pow(y,mod-2,mod)%mod

count+=pow(2,n,mod)
#print(count)
count-=1
count-=C(n,a)
#print(count)
count-=C(n,b)

print(max(0,(count)%mod))
### BUGGY CODE ###

from bisect import bisect_left, bisect_right

n = int(input())

l_list = [int(x) for x in input().split()]
l_list.sort()

ans = 0
for i in range(n - 2):
    continue_flag = False
    for j in range(i + 1, n - 1):
        l = bisect_right(l_list, l_list[j] - l_list[i])
        l = l if l > j else j + 1
        if l == n:
            continue_flag = True
            break

        r = bisect_left(l_list, l_list[j] + l_list[i])

        ans += r - l

    if continue_flag:
        continue
print(ans)

### FIXED CODE ###

from bisect import bisect_left

n = int(input())

l_list = [int(x) for x in input().split()]
l_list.sort()
# print(l_list)

ans = 0
for i in range(n - 2):
    continue_flag = False
    for j in range(i + 1, n - 1):
        ans += bisect_left(l_list, l_list[j] + l_list[i]) - (j + 1)
print(ans)
### BUGGY CODE ###

sankaNum, scoreMax, ansCnt = map(int, input().split())

score = []

for i in range(sankaNum):
    score.append(scoreMax)

for n in range(ansCnt):
    ansSanka = input()

    for k in range(sankaNum):
        if int(ansSanka) - 1 != k:
            score[k] -=  1
    
for n in range(sankaNum):

    if score[n]  > 0:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

sankaNum, scoreMax, ansCnt = map(int, input().split())

score = [scoreMax-ansCnt]*sankaNum

for _ in range(ansCnt):
    score[int(input()) - 1] +=  1

for m in range(sankaNum):

    if  score[m] > 0:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**7)
n,q=map(int,input().split())
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split());a-=1;s-=1
    edges[a].append(s)
    edges[s].append(a)
cnt=[-1]*n
add=[0]*n
for _ in range(q):
    a,s=map(int,input().split())
    add[a-1]+=s


def f(x):
    if cnt[x]!=-1:return 
    ret=add[x]
    fd=0
    for to in edges[x]:
        if cnt[to]!=-1:ret+=cnt[to];fd=to;break
    cnt[x]=ret
    for to in edges[x]:
        if to!=fd:f(to)
    
    
f(0)
print(*cnt,sep=" ")

### FIXED CODE ###

import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
n,q=map(int,input().split())
cnt=[0]*(n)
edges=[0]*n
for i in range(n):
    edges[i]=[]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)

from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

for i in edges[0]:
    dq.append((0,i))
while dq:
    f,t=dq.popleft()
    cnt[t]+=cnt[f]
    for to in edges[t]:
        if to!=f:dq.append((t,to))
    
print(*cnt)
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

cnt = 0

for a in A:
    for b in B:
        for c in C:
            if a < b < c:
                cnt += 1

print(cnt)


### FIXED CODE ###

import bisect


n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))


cnt = 0

for b in B:
    mid = bisect.bisect_left(A, b)
    bottom = n - bisect.bisect_right(C, b)
    cnt += mid*bottom

print(cnt)
### BUGGY CODE ###

import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

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):
        dis = X[n:n + K]
        l = abs(dis[0]) + abs(dis[0]-dis[K-1])
        r = abs(dis[K-1]) + abs(dis[K-1] - dis[0])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

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):
        l = abs(X[n]) + abs(X[n]-X[n+K-1])
        r = abs(X[n+K-1]) + abs(X[n+K-1] - X[n])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()