//参考:http://www.cnblogs.com/asuran/archive/2009/10/13/1582167.html
#include <stdio.h>
#include <string.h>
#define nMAX (100)
#define mMAX (10050)
int main (){
int cash, t, i, k, n, v, val[nMAX], dp[mMAX], count, ans;
while (EOF != scanf ("%d%d", &cash, &t)){
count = 0;
while (t--){
scanf ("%d%d", &n, &v);
k = 1;
while (n-k >= 0){
val[count++] = v*k;
n -= k;
k *= 2;
}
if (n) val[count++] = v*k;
}
memset (dp, 0, (cash+1)*sizeof(int));
dp[0] = 1;
for (i=0; i<count; i++)
for (k=cash; k>=val[i]; k--)
if (dp[k-val[i]]) dp[k] = 1;
ans = cash;
while (!dp[ans]) ans--;
printf ("%d\n", ans);
}
return 0;
}