#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#pragma region 입출력 속도향상
#define FAST_IO() \
{\
ios::sync_with_stdio(false);\
cin.tie(NULL); \
cout.tie(NULL); \
}\
#pragma endregion
#define MAX 100000
int main()
{
FAST_IO();
int c, n;
cin >> c >> n;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
v.push_back({ a,b });
}
int dp[MAX + 1] = { 0 };
for (int i = 0; i < v.size(); i++)
{
for (int j = 1; j <= MAX; j++)
{
int val = j - v[i].first;
if (val >= 0)
dp[j] = max(dp[j], dp[val] + v[i].second);
}
}
for (int i = 1; i <= MAX; i++)
{
if (dp[i] >= c)
{
cout << i;
break;
}
}
return 0;
}