CodeEff/ECCO huggingface dataset:
# Original solution
from collections import deque
n,m = list(map(int,input().split()))
graph = [[[] for i in range(3)] for j in range(n+1)]
for i in range(m):
  u,v = list(map(int,input().split()))
  graph[u][0].append(v)
s,t = list(map(int,input().split()))
for i in range(1,3):
  for j in range(1,n+1):
    l = set([])
    for k in graph[j][0]:
      l = l | set(graph[k][i-1])
    graph[j][i] = list(l)
for i in range(n+1):
  graph[i] = graph[i][2]
cnt = 0
done = [0]*(n+1)
done[s] = 1
wait = deque([s])
while wait:
  cnt += 1
  q = wait
  wait = deque([])
  for i in q:
    for j in graph[i]:
      if not done[j]:
        wait.append(j)
        done[j] = 1
      if j == t:
        print(cnt)
        quit()
print((-1))

# Optimized solution
import copy
n,m = list(map(int,input().split()))
graph = [[] for _ in range(n*3)]
for i in range(m):
  u,v = list(map(int,input().split()))
  for j in range(3):
    graph[n*j+u-1].append((n*(j+1)+v-1)%(3*n))
s,t = list(map(int,input().split()))
wait = [s-1]
cnt = 0
done = [0]*(3*n)
while wait:
  cnt += 1
  now = copy.copy(wait)
  wait = []
  for i in now:
    for j in graph[i]:
      if not done[j]:
        done[j] = 1
        wait.append(j)
      if j == t-1:
        print((cnt//3))
        quit()
print((-1))
# Original solution
from collections import deque

N,M = list(map(int,input().split()))
edges = []
ad = [[] for _ in range(N)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    u -= 1
    v -= 1
    ad[u].append(v)
    edges.append((u,v))
S,T = list(map(int,input().split()))
S -= 1
T -= 1

dq = deque()
dq.append((S,0))
while dq:
    v,d = dq.popleft()
    nd = d + 1
    for nv in ad[v]:
        if nv == T and nd % 3 == 0:
            print((nd//3))
            exit()
        if d == 3*10**5:
            print((-1))
            exit()
        dq.append((nv,nd))

print((-1))

# Optimized solution
from collections import deque

N,M = list(map(int,input().split()))
ad = [[[] for _ in range(3)] for _ in range(N)]
for _ in range(M):
    u,v = list(map(int,input().split()))
    u -= 1
    v -= 1
    for j in range(3):
        ad[u][j].append((v,(j+1)%3))
S,T = list(map(int,input().split()))
S -= 1
T -= 1

visited = [[False]*3 for _ in range(N)]
dq = deque()
dq.append((S,0,0))
visited[S][0] = True

while dq:
    v,j,d = dq.popleft()
    nd = d + 1
    for nv,nj in ad[v][j]:
        if (nv,nj) == (T,0):
            print((nd//3))
            exit()
        if visited[nv][nj]:
            continue
        visited[nv][nj] = True
        dq.append((nv,nj,nd))
        
print((-1))
# Original solution
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 30 09:04:16 2018

@author: Yamazaki Kenichi
"""
L,N = list(map(int, input().split(' ')))
X = [int(eval(input())) for i in range(N) ]

Xr = [0] + X

Yr = ["d"]
for i in range(0,N):
    Yr.append( Xr[i+1] - Xr[i])

Yl = ["d"]+[L-X[N-1]]
Yl += Yr[::-1]

S = []

answer = 0
for itr in range(1,N+1):
    sum = 0
    counter = 1
    while counter <= itr:
        sum += Yr[counter]
        counter += 1
        
    turn = counter-1
    counter2 = 1
    sum2 = sum
    while counter <= N:
        if counter2 % 2 ==1:
            sum2 += Yl[counter2//2+1]
            sum += sum2 
        else:
            sum2 += Yr[turn + counter2//2]
            sum += sum2
        counter += 1
        counter2 += 1
#    print(sum)
#    S.append(sum)
    answer = max(answer,sum)

for itr in range(1,N+1):
    sum = 0
    counter = 1
    while counter <= itr:
        sum += Yl[counter]
        counter += 1
    turn = counter-1
    counter2 = 1
    sum2 = sum
    while counter <= N:
        if counter2 % 2 ==1:
            sum2 += Yr[counter2//2+1]
            sum += sum2 
        else:
            sum2 += Yl[turn + counter2//2]
            sum += sum2
        counter += 1
        counter2 += 1
    answer = max(answer,sum)
print(answer)


# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 30 09:04:16 2018

@author: Yamazaki Kenichi
"""
L,N = list(map(int, input().split(' ')))
X = [int(eval(input())) for i in range(N) ]

#inp = input().split()
#inp = [int(i) for i in inp]
#L, N = inp[0],inp[1]
#X = inp[2:]

X = [0] + X 
temp = L - X[1]
ans =  temp

itr1 = 0
itr2 = N

a = N%2

for i in range(-(N-1),N):
    if (i+a)%2 == 1:
        itr1 += 1
    else:
        itr2 -= 1

    if X[i]-X[i-itr1] >=0:
        minus = X[i]-X[i-itr1]
    else:
        minus = L+(X[i]-X[i-itr1])

    if X[i+itr2]-X[i] >=0:
        plus = X[i+itr2]-X[i]
    else:
        plus = L+(X[i+itr2]-X[i])
    temp = temp - minus + plus
#    print(temp)
    ans = max(ans,temp)
print(ans)
# Original solution
import sys

INF = float('inf')

L, N = list(map(int, input().split()))
Xs = [0] + [int(eval(input())) for _ in range(N)] + [L]

if N == 1:
    print((max(Xs[1], L - Xs[1])))
    sys.exit()

dp = [[[-INF] * (2) for B in range(N+1)] for A in range(N+1)]
dp[1][N][0] = 0
dp[1][N][1] = 0
for A in range(1, N+1):
    for B in reversed(list(range(A, N+1))):
        # C=0 ccw
        C = 0
        if B > 1:  # ccw
            dp[A][B-1][0] = max(dp[A][B-1][0], dp[A][B][C] + Xs[B+1] - Xs[B])
        if A < N:  # cw
            dp[A+1][B][1] = max(dp[A+1][B][1], dp[A][B][C] + (L - Xs[B+1]) + Xs[A])
        # C=1 cw
        C = 1
        if B > 1:  # ccw
            dp[A][B-1][0] = max(dp[A][B-1][0], dp[A][B][C] + Xs[A-1] + (L - Xs[B]))
        if A < N:  # cw
            dp[A+1][B][1] = max(dp[A+1][B][1], dp[A][B][C] + Xs[A] - Xs[A-1])

ans = 0
for A in range(N+1):
    for B in range(N+1):
        for C in range(2):
            ans = max(ans, dp[A][B][C])
print(ans)


# Optimized solution
from itertools import accumulate

def getRangeSum(accXs, iFr, iTo):
    return accXs[iTo+1] - accXs[iFr]

def getAns(accXs, accRevXs, Xs, revXs):
    ans = 0
    for i in range(N):
        num = (N-i)//2
        dist = 2 * getRangeSum(accXs, i, i + num-1)
        dist += 2 * getRangeSum(accRevXs, 0, num-1)
        if (N-i)%2 == 0:
            dist -= revXs[num-1]
        else:
            dist += Xs[i + num]
        ans = max(ans, dist)
    return ans

L, N = list(map(int, input().split()))
Xs = [int(eval(input())) for _ in range(N)]

revXs = [L-X for X in reversed(Xs)]
accXs = [0] + list(accumulate(Xs))
accRevXs = [0] + list(accumulate(revXs))

print((max(getAns(accXs, accRevXs, Xs, revXs), getAns(accRevXs, accXs, revXs, Xs))))
# Original solution
l,n,*ki=list(map(int,open(0).read().split()))
fmax=rmax=0
fl=[]

for ii in range(n):
    f=0
    x=ki[ii:]
    lx=n-ii
    for i in x[:(lx+1)//2]:
        f+=i*2
    for i in x[(lx+1)//2:]:
        f+=(l-i)*2
    if lx%2:
        sa = x[lx//2]
    else:
        sa = l - x[lx // 2]
    f-=sa
    fl.append(f)

for ii in range(n):
    r=0
    x=ki[:n-ii]
    lx=n-ii
    for i in x[:lx//2]:
        r += i * 2
    for i in x[lx//2:]:
        r+=(l-i)*2
    if lx%2:
        sa = l - x[(lx-1) // 2]
    else:
        sa = x[(lx-1)//2]
    r-=sa
    fl.append(r)

print((max(fl)))

# Optimized solution
ln, kn, *ki = list(map(int, open(0).read().split()))
# 順回転
pa = ki[-1]
ky = [pa]
kigu = 1
for ys in range(2, kn + 1):
    # 要素数が偶数の場合
    if kigu == 1:
        pa += ki[kn - ys] * 2 + ln - ki[-(ys // 2)] * 2
    # 要素数が奇数の場合
    else:
        pa += ki[kn - ys] * 2 - ki[-(ys // 2) - 1] + ln - ki[-(ys // 2)]
    ky.append(pa)
    kigu *= -1

# 逆回転
pa = ln - ki[0]
ky.append(pa)
kigu = 1
for ys in range(2, kn + 1):
    # 要素数が偶数の場合
    if kigu == 1:
        pa += (ln-ki[ys - 1]) * 2 - ln + ki[ys // 2 - 1] * 2
    # 要素数が奇数の場合
    else:
        pa += (ln-ki[ys - 1]) * 2+ki[ys//2]-ln+ki[ys//2-1]
    ky.append(pa)
    kigu *= -1

print((max(ky)))
# Original solution
from collections import deque
import math
L, N = list(map(int, input().split()))
XAntiClock, XClock = deque(), deque()
DistAntiClock, DistClock = deque(), deque()
DistAntiClock.append(0)
DistClock.append(0)
for i in range(N):
    x = int(eval(input()))
    XAntiClock.append(x)
    XClock.appendleft(L-x)
    DistAntiClock.append(DistAntiClock[-1] + 2 * x)
for i in range(N):
    DistClock.append(DistClock[-1] + XClock[i] * 2)

maxLength = max(XAntiClock[N-1], XClock[N-1])
for i in range(1, N):
    finIndex = math.ceil((i + N)/2)
    if (i + N) % 2 == 0:
        AntiClockLength = DistAntiClock[finIndex] - DistAntiClock[i-1] - XAntiClock[finIndex-1] + DistClock[N - finIndex] 
        ClockLength = DistClock[finIndex] - DistClock[i-1] - XClock[finIndex-1] + DistAntiClock[N - finIndex]
    else:
        AntiClockLength = DistAntiClock[finIndex - 1] - DistAntiClock[i-1] + DistClock[N - finIndex + 1] - XClock[N - finIndex]
        ClockLength = DistClock[finIndex - 1] - DistClock[i-1] + DistAntiClock[N - finIndex + 1] - XAntiClock[N - finIndex]
    maxLength = max(max(AntiClockLength, ClockLength), maxLength)

print(maxLength)

# Optimized solution
import math
L, N = list(map(int, input().split()))
X = [None for i in range(N)]
DistAntiClock, DistClock = [0 for i in range(N+1)], [0 for i in range(N+1)]
for i in range(N):
    X[i] = int(eval(input()))
    DistAntiClock[i+1] = DistAntiClock[i] + 2 * X[i]
for i in range(N):
    DistClock[i+1] = DistClock[i] + (L - X[-1-i]) * 2

maxLength = max(X[N-1], L - X[0])
for i in range(1, N):
    finIndex = math.ceil((i + N)/2)
    if (i + N) % 2 == 0:
        AntiClockLength = DistAntiClock[finIndex] - DistAntiClock[i-1] - X[finIndex-1] + DistClock[N - finIndex] 
        ClockLength = DistClock[finIndex] - DistClock[i-1] - (L-X[-finIndex]) + DistAntiClock[N - finIndex]
    else:
        AntiClockLength = DistAntiClock[finIndex - 1] - DistAntiClock[i-1] + DistClock[N - finIndex + 1] - (L - X[finIndex - 1])
        ClockLength = DistClock[finIndex - 1] - DistClock[i-1] + DistAntiClock[N - finIndex + 1] - X[N - finIndex]
    maxLength = max(max(AntiClockLength, ClockLength), maxLength)

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

L,N=list(map(int,input().split()))
X=[0]+[int(eval(input())) for i in range(N)]

def LENG(a,b,start):
    if b==0:
        if start>a:
            return a
        else:
            return L-a

    if a>b:
        a,b=b,a

    return a+L-b


#CANDI=[(N+1)//2,(N+1)//2-1,(N+1)//2+1]
#(i+2)//2*(-1)**i

ANS=0
for start in range(1,N+1):
    if start==0:
        continue

    s=start
    t=(s+(-1)**0)%(N+1)
    ANS1=0


    
    for i in range(2,N+3):
        ANS1+=LENG(X[s],X[t],X[start])
        #print(s,t,i,ANS1)
        
        s,t=t,(t-i*((-1)**i))%(N+1)

        

        if s==0:
            break
    #print(ANS1)

    s=start
    t=(s-(-1)**0)%(N+1)
    ANS2=0
    for i in range(2,N+3):
        ANS2+=LENG(X[s],X[t],X[start])
        #print(s,t,i,ANS2)
        s,t=t,(t+i*((-1)**i))%(N+1)

        if s==0:
            break
    #print(ANS2)

    ANS=max(ANS,ANS1,ANS2)

print(ANS)
    


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

L,N=list(map(int,input().split()))
X=[0]+[int(eval(input())) for i in range(N)]

S=[]

for i in range(N):
    S.append(X[i+1]-X[i])
S.append(L-X[-1])

#print(S)

DIS=L-S[-1]
ANS=L-S[-1]
MINUS=0
PLUS=L-S[-1]

#print(DIS)

for i in range(N-1):
    if i%2==0:
        MINUS+=S[-i-2]
        PLUS+=S[-i//2-1]-S[-i-2]

    else:
        MINUS+=S[-i-2]-S[-i-1+i//2]
        PLUS-=S[-i-2]

    DIS+=PLUS-MINUS

    if ANS<DIS:
        ANS=DIS

    #print(DIS,PLUS,MINUS)

S=S[::-1]

DIS=L-S[-1]
MINUS=0
PLUS=L-S[-1]

#print(DIS)

for i in range(N-1):
    if i%2==0:
        MINUS+=S[-i-2]
        PLUS+=S[-i//2-1]-S[-i-2]

    else:
        MINUS+=S[-i-2]-S[-i-1+i//2]
        PLUS-=S[-i-2]

    DIS+=PLUS-MINUS

    if ANS<DIS:
        ANS=DIS

    #print(DIS,PLUS,MINUS)

print(ANS)
# Original solution
def is_even(num):
    return num % 2 == 0
def goback(M, x):
    k = M // 2
    if M == 0:
        return L - x[0]
    elif M == 1:
        return 2 * (L - x[1]) + x[0]
    elif is_even(M):
        return 2 * sum(x[0:k]) - 2 * sum(x[M - k + 1:M + 1]) - x[M - k] + L * (2 * k + 1)
    else:
        return 2 * sum(x[0:k]) + x[k] - 2 * sum(x[M - k:M + 1]) + L * 2 * (k + 1)

L, N = (int(i) for i in input().split())
points = []
ans = []
for i in range(0, N):
    points.append(int(eval(input())))

for i in range(0, N):
    ans.append((goback(i, points)))

revpoints = [L - p for p in points[::-1]]
for i in range(0, N):
    ans.append((goback(i, revpoints)))

print((max(ans)))

# Optimized solution
def is_even(num):
    return num % 2 == 0
def goback(M, x, Sx):
    k = M // 2
    if M == 0:
        return L - x[0]
    elif M == 1:
        return 2 * (L - x[1]) + x[0]
    elif is_even(M):
        return 2 * Sx[k - 1] - 2 * (Sx[M] - Sx[M - k]) - x[M - k] + L * (2 * k + 1)
    else:
        return 2 * Sx[k - 1] + x[k] - 2 * (Sx[M] - Sx[M - k - 1]) + L * 2 * (k + 1)

L, N = (int(i) for i in input().split())
ans = 0

pl = []
for i in range(N):
    pl.append(int(eval(input())))
sumpl = [pl[0]]
for i in range(1, N):
    sumpl.append(sumpl[i - 1] + pl[i])
for i in range(0, N):
    ans = max(ans, goback(i, pl, sumpl))

revpl = [L - p for p in pl[::-1]]
sumrevpl = [revpl[0]]
for i in range(1, N):
    sumrevpl.append(sumrevpl[i - 1] + revpl[i])
for i in range(0, N):
    ans = max(ans, goback(i, revpl, sumrevpl))

print(ans)
# Original solution
import sys
def 解():
    L,N=[int(_) for _ in input().split()]
    aX=[int(_) for _ in sys.stdin.readlines()]
    iMax = max(aX[0],L-aX[0],aX[-1],L-aX[-1])
    if 1 < N:
        for i in range(N-1):

            lN = min(i,N-i-1)
            rN = min(i+1,N-i-1)
            R1 = aX[i]+ 2*sum(aX[i-lN:i]) + 2*(rN*L-(sum(aX[i+1:i+rN+1])))

            i += 1
            lN = min(i,N-i)
            rN = min(i,N-i-1)
            R2 = (L- aX[i])+ 2*sum(aX[i-lN:i]) + 2*(rN*L-(sum(aX[i+1:i+rN+1])))

            iMax = max(R1,R2,iMax)
    print(iMax)
解()


# Optimized solution
import sys
def 解():
    L,N=[int(_) for _ in input().split()]
    aX=[int(_) for _ in sys.stdin.readlines()]
    iMax = max(aX[0],L-aX[0],aX[-1],L-aX[-1])
    accX = [0]*(N+1)
    accX[0]=aX[0]
    for i in range(1,N):
        accX[i]=accX[i-1]+aX[i]

    if 1 < N:
        for i in range(N-1):

            lN = min(i,N-i-1)
            rN = min(i+1,N-i-1)
            #R1 = aX[i]+ 2*sum(aX[i-lN:i]) + 2*(rN*L-(sum(aX[i+1:i+rN+1])))
            R1 = aX[i]+ 2*(accX[i-1]-accX[i-lN-1]) + 2*(rN*L-(accX[i+rN]-accX[i]))

            i += 1
            lN = min(i,N-i)
            rN = min(i,N-i-1)
            #R2 = (L- aX[i])+ 2*sum(aX[i-lN:i]) + 2*(rN*L-(sum(aX[i+1:i+rN+1])))
            R2 = (L- aX[i])+ 2*(accX[i-1]-accX[i-lN-1]) + 2*(rN*L-(accX[i+rN]-accX[i]))

            iMax = max(R1,R2,iMax)
    print(iMax)
解()
# Original solution
n,m=list(map(int,input().split(' ')))
a=list(map(int,input().split(' ')))
asum=sum(a)

if(n-asum)<0:
    print("-1")
    exit()
else:
    print((n-asum))
    exit()


# Optimized solution
n,m=list(map(int,input().split(' ')))
a=list(map(int,input().split(' ')))
asum=sum(a)

if(n-asum)<0:
    print("-1")
else:
    print((n-asum))
# Original solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
s = sum(a)
print((max(-1,n-s)))

# Optimized solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
#合計で何日かかるかを計算して
s = sum(a)
#遊べる日数はn-sで、n-s<0になったら宿題終わらないので-1を出す
print((max(-1,n-s)))
# Original solution
import sys
input = sys.stdin.readline
from collections import *
from math import *

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

if S>N:
    print((-1))
else:
    print((N-S))

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

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

if S>N:
    print((-1))
else:
    print((N-S))
# Original solution
n, m = [int(i) for i in input().split(' ')]

a_list = [int(i) for i in input().split(' ')]

result = n - sum(a_list)
if result < 0:
    print((-1))
else:
    print(result)


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

a_list = list(map(int, input().split()))

result = n - sum(a_list)
if result < 0:
    print((-1))
else:
    print(result)
# Original solution
import sys
from collections import deque
import copy
def main():
    N, M = list(map(int, input().split()))
    A = [int(i) for i in input().split()]
    count = 0
    for i in range(M):
        count += A[i]
    if N - count < 0:
        print((-1))
    else:
        print((N - count))


if __name__ == '__main__':
    main()


# Optimized solution
import sys
from collections import deque
import copy
def main():
    N, M = list(map(int, input().split()))
    A = [int(i) for i in input().split()]
    count = sum(A)
    if N - count < 0:
        print((-1))
    else:
        print((N - count))


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

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))

if n < sum(a):
    print((-1))
else:
    print((n-sum(a)))


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

print((-1 if n < sum(a) else n - sum(a)))
# Original solution
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))

A_sum = sum(A)
ans = N - A_sum

if ans>=0:
    print(ans)
else:
    print((-1))

# Optimized solution
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))

A_sum = sum(A)

if N - A_sum>=0:
    print((N - A_sum))
else:
    print((-1))
# Original solution
NM = list(map(int, input().split()))
a = list(map(int, input().split()))
x = 0
for i in range(NM[1]):
    x += a[i]

if x > NM[0]:
    print((-1))
else:
    print((NM[0] - x))

# Optimized solution
NM = list(map(int, input().split()))
N, M = NM[0],NM[1]
a = list(map(int, input().split()))

if  sum(a)> N:
    print((-1))
else:
    print((N - sum(a)))
# Original solution
NM = list(map(int, input().split()))
N, M = NM[0],NM[1]
a = list(map(int, input().split()))

if  sum(a)> N:
    print((-1))
else:
    print((N - sum(a)))

# Optimized solution
NM = list(map(int, input().split()))
N, M = NM[0],NM[1]
a = list(map(int, input().split()))
A = sum(a)
if  A> N:
    print((-1))
else:
    print((N - A))
# Original solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = n - sum(a)
print((d if d >= 0 else -1))

# Optimized solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
print((max(n - sum(a), -1)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

a = sum(A)

if a > N:
    print((-1))
else:
    print((N - a))


# Optimized solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

print((max(N - sum(A), -1)))
# Original solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
print((n-sum(a) if n-sum(a)>=0 else -1))

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
print((max(-1,n-sum(a))))
# Original solution
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))

ans = n-  sum(a)

if ans>=0:
    print(ans)
else:
    print((-1))

# Optimized solution
N, M = list(map(int,input().split()))
A =list(map(int,input().split()))

print((max(-1,N-sum(A))))
# Original solution
N, M = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort()

free = N - sum(l)
print((free if free>=0 else -1))

# Optimized solution
N, M = list(map(int, input().split()))
l = list(map(int, input().split()))


free = N - sum(l)
print((free if free>=0 else -1))
# Original solution
import math
N, M = map(int, input().split())
A = sum(map(int, input ().split()))
print(N - A) if N - A >= 0 else print(-1)


# Optimized solution
N, M = map(int, input().split())
A = sum(map(int, input ().split()))
print(N - A) if N - A >= 0 else print(-1)
# Original solution
n, m, *A = list(map(int, open(0).read().split()))
ans = n-sum(A)
print((ans if ans >= 0 else -1))

# Optimized solution
n, m, *A = list(map(int, open(0).read().split()))
print((max(n-sum(A), -1)))
# Original solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))

for i in range(m):
    n -= a[i]

if n < 0:
    print((-1))
else:
    print(n)


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

if n >= sum(a):
    print((n-sum(a)))
else:
    print((-1))
# Original solution
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    n, m = MI()
    A = LI()

    sm = sum(A)
    if n - sm >= 0:
        print((n - sm))
    else:
        print((-1))


if __name__ == '__main__':
    solve()


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(eval(input()))

def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n, m = MI()
    A = LI()

    s = sum(A)
    print((max(n - s, -1)))


if __name__ == '__main__':
    solve()
# Original solution
N, M = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
sum_a = 0
for i in a:
    sum_a += i
if sum_a > N:
    print((-1))
else:
    print((N - sum_a))

# Optimized solution
def to_do():
    N,M=list(map(int,input().split()))
    day_list=list(map(int,input().split()))
    sum=0
    for i in range(0,M):
        sum=sum+day_list[i]
    if sum>N:
        print((-1))
    if sum<=N:
        print((N-sum))
if  __name__=="__main__":
    to_do()
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
for i in range(M):
    N -= A[i]
if N>=0:
    print(N)
elif N < 0:
    print((-1))

# Optimized solution
#先にAの合計値を出してもよい
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
suma = sum(A)
if N-suma>=0:
    print((N-suma))
else:
    print((-1))
# Original solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
if sum(a)>n:
  print('-1')
else:
  print((n-sum(a)))


# Optimized solution
n,m = list(map(int,input().split()))
a = list(map(int,input().split()))
print(('-1' if sum(a)>n else n-sum(a)))
# Original solution
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))

d = sum(A)

if N < d:
  print((-1))
else:
  print((N-d))

# Optimized solution
N, M = list(map(int, input().split()))
As = list(map(int, input().split()))
d = sum(As)
if N < d:
    print((-1))
else:
    print((N-d))
# Original solution
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = 0
for i in range(m):
  b +=a[i]
if n<b:
  print("-1")
else:
  print((n-b))

# Optimized solution
n,m = list(map(int,input().split()))
a = list(map(int, input().split()))
b = sum(a)
print((max(-1,n-b)))
# Original solution
n,m=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
rem = n-sum(a)
print((rem if rem>=0 else -1))


# Optimized solution
n,m=list(map(int,input().split()))
for num	in input().split():
  n-=int(num)
print((n if n>=0 else -1))
# Original solution
N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

ans = N - sum(A)
print((ans if ans >= 0 else -1))

# Optimized solution
N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
print((ans if (ans := N - sum(A)) >= 0 else -1))