#include <bits/stdc++.h>
using namespace std;
unsigned long long n,a[100005],minn=99999,m;
bool c[100005]={};
void tryy(int s)
{
for(int i=1;i<=n;i++)
{
if(c[i]==0)
{
if(s>=a[i])
{
s-=a[i];
c[i]=1;
tryy(s);
s+=a[i];
c[i]=0;
}
else
{
if(s<=minn)
minn=s;
}
}
}
}
int main()
{
cin>>m>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a,a+n+1);
tryy(m);
cout<<minn;
return 0;
}