#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
int w,n,v,r,t,i,maxn,j;
multimap<int,int> mps;
multimap<int,int>::iterator it2;
int state[100];
vector<int> vt;
void f(int num,int &W,int &t,int R)
{
it2 = mps.begin();
if(num==0)
{
if(it2->second<=W && it2->first < R)
t++;
if(t>maxn)
maxn = t;
return;
}
for(j=0;j<num;j++)
it2 ++;
int tw = it2->second;
int mw = W;
int mt = t;
int mr = it2->first;
if(tw<=W)
{
f(num-1,W,t,R);
W = mw;
t = mt;
if(tw == W && mr<R)
{
t++;
if(t>maxn)
maxn = t;
}
else if(mr<R)
{
W = W - tw;
t++;
f(num -1,W,t,mr);
}
}
else
f(num-1,W,t,R);
}
int main()
{
while(cin>>w>>n)
{
maxn =0;
t = n;
for(i=0;i<n;i++)
state[i]=0;
mps.clear();
while(t--)
{
cin>>v>>r;
mps.insert(pair<int,int>(r,v));
}
it2=mps.begin();
t = 0;
int R = 1000;
f(n-1,w,t,R);
cout<<maxn<<endl;
}
return 0;
}