def main(n, d):
if n in d: #тут
return n
n, m = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in a:
s.append(i)
s.append(i)
s.sort()
def main(n, d):
if n in d:
return n
if d[0] > n:
return -1
for i in d:
f = []
if i > n:
break
else:
k = d
u = main(n-d[i],k.remove(d[i]))
if u != -1:
return [u].append(d[i])
return -1
if s[0] > n:
print(-1)
else:
print(main(n,s))