#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
struct cy
{
int sj,jz;
};cy a[105];
bool compare(cy a,cy b)
{
return a.jz>b.jz;
}
int main()
{
int t,m,d=0;
cin>>t>>m;
for(int i=0;i<m;i++)
{
cin>>a[i].sj>>a[i].jz;
}
sort(a,a+m,compare);
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
if(a[j].sj<a[i].sj&&a[i].jz==a[j].jz)
{
swap(a[i].sj,a[j].sj);
}
}
}
for(int i=0;i<m;i++)
{
if(a[i].sj<=t)
{
t-=a[i].sj;
d+=a[i].jz;
}
}
cout<<d;
return 0;
}