#include<bits/stdc++.h>
using namespace std;
#define int long long
pair<int,int> findans(vector<pair<int,int>>& v,int i,int budget,int cost,vector<vector<pair<int,int>>>& dp,int value)
{
//cout<<"for i = "<<i<<" and cost = "<<cost<<" and value = "<<value<<endl;
if(i<0)
{
//cout<<"Returning"<<endl;
return make_pair(value,cost);
}
if(dp[i][cost].first!=-1)
{
//cout<<"Value is present as - "<<dp[i][cost].first<<endl;
return dp[i][cost];
}
if(cost+v[i].first<=budget)
{
pair<int,int> x=findans(v,i-1,budget,cost+v[i].first,dp,value+v[i].second);
pair<int,int> y=findans(v,i-1,budget,cost,dp,value);
if(x.first==y.first)
{
if(x.second<y.second)
return dp[i][cost]=x;
else
return dp[i][cost]=y;
}
else
if(x.first>y.first)
return dp[i][cost]=x;
else
return dp[i][cost]=y;
}
else
{
return dp[i][cost]=findans(v,i-1,budget,cost,dp,value);
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int budget,n;
cin>>budget>>n;
while(budget!=0 && n!=0){
vector<pair<int,int>> v;
for(int i=0;i<n;i++)
{
int cost;
int enjoy;
cin>>cost>>enjoy;
v.push_back(make_pair(cost,enjoy));
}
//sort(v.begin(),v.end(),compare);
int cost=0;
int newcost=0;
vector<vector<pair<int,int>>> dp(101,vector<pair<int,int>>(501));
for(int i=0;i<=100;i++)
{
for(int j=0;j<=500;j++)
{
dp[i][j].first=-1;
}
}
pair<int,int> ans=findans(v,n-1,budget,cost,dp,0);
cout<<ans.second<<" "<<ans.first<<endl;
//cout<<dp[9][0].first<<endl;
cin>>budget>>n;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbnQgbG9uZyBsb25nCnBhaXI8aW50LGludD4gZmluZGFucyh2ZWN0b3I8cGFpcjxpbnQsaW50Pj4mIHYsaW50IGksaW50IGJ1ZGdldCxpbnQgY29zdCx2ZWN0b3I8dmVjdG9yPHBhaXI8aW50LGludD4+PiYgZHAsaW50IHZhbHVlKQp7CiAgIAogICAgLy9jb3V0PDwiZm9yIGkgPSAiPDxpPDwiIGFuZCBjb3N0ID0gIjw8Y29zdDw8IiBhbmQgdmFsdWUgPSAiPDx2YWx1ZTw8ZW5kbDsKICAgIGlmKGk8MCkKICAgIHsKICAgICAgICAvL2NvdXQ8PCJSZXR1cm5pbmciPDxlbmRsOwogICAgICAgIHJldHVybiBtYWtlX3BhaXIodmFsdWUsY29zdCk7CiAgICB9CiAgICBpZihkcFtpXVtjb3N0XS5maXJzdCE9LTEpCiAgICB7CiAgICAgICAgLy9jb3V0PDwiVmFsdWUgaXMgcHJlc2VudCBhcyAtICI8PGRwW2ldW2Nvc3RdLmZpcnN0PDxlbmRsOwogICAgICAgIHJldHVybiBkcFtpXVtjb3N0XTsKICAgIH0KICAgIGlmKGNvc3QrdltpXS5maXJzdDw9YnVkZ2V0KQogICAgewogICAgICAgCiAgICAgICAgcGFpcjxpbnQsaW50PiB4PWZpbmRhbnModixpLTEsYnVkZ2V0LGNvc3QrdltpXS5maXJzdCxkcCx2YWx1ZSt2W2ldLnNlY29uZCk7CiAgICAgICAgcGFpcjxpbnQsaW50PiB5PWZpbmRhbnModixpLTEsYnVkZ2V0LGNvc3QsZHAsdmFsdWUpOwogICAgICAgCiAgICAgICAgaWYoeC5maXJzdD09eS5maXJzdCkKICAgICAgICB7CiAgICAgICAgICAgIGlmKHguc2Vjb25kPHkuc2Vjb25kKQogICAgICAgICAgICByZXR1cm4gZHBbaV1bY29zdF09eDsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gZHBbaV1bY29zdF09eTsKICAgICAgICAgICAKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIGlmKHguZmlyc3Q+eS5maXJzdCkKICAgICAgICByZXR1cm4gZHBbaV1bY29zdF09eDsKICAgICAgICBlbHNlCiAgICAgICAgcmV0dXJuIGRwW2ldW2Nvc3RdPXk7CiAgICB9CiAgICBlbHNlCiAg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