#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct node{
int a;
int b;
int c;
};
node no[200];
int cmp(int a,int b){
return no[a].b-no[a].a>no[b].b-no[b].a;
}
int main()
{
int n;
int m;
int l=0;
int p=0;
int aa[105];
int shifou_aa[105]={0};
int bb[105];
int shifou_bb[105]={0};
int k=1;
int sum1=0;
int sum2=0;
int sum=0;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d %d",&no[i].a,&no[i].b);
if(no[i].a<no[i].b){
no[i].c=1;
}else{
no[i].c=2;
}
}
for(int i=0;i<m;i++){
if(no[i].c==1){
for(int j=0;j<n/no[i].a;j++){
aa[k++]=i;
}
}
}
sort(aa,aa+k-1,cmp);
for(int i=1;i<k;i++){
l+=no[aa[i]].a;
shifou_aa[i]=1;
if(l<=n){
p=i;
}else{
l-=no[aa[i]].a;
shifou_aa[i]=0;
continue;
}
}
for(int i=1;i<=p;i++){
if(shifou_aa[i]==1){
sum1+=no[aa[i]].b-no[aa[i]].a;
}
}
sum=n;
n+=sum1;
for(int i=0;i<m;i++){
int temp=no[i].a;
no[i].a=no[i].b;
no[i].b=temp;
}
////////////////
l=0;
for(int i=0;i<m;i++){
if(no[i].c==2){
for(int j=0;j<n/no[i].a;j++){
bb[k++]=i;
}
}
}
sort(aa,aa+k-1,cmp);
for(int i=1;i<k;i++){
l+=no[bb[i]].a;
shifou_bb[i]=1;
if(l<=n){
p=i;
}else{
l-=no[bb[i]].a;
shifou_bb[i]=0;
continue;
}
}
for(int i=1;i<=p;i++){
if(shifou_bb[i]==1){
sum2+=no[bb[i]].b-no[bb[i]].a;
}
}
int sss=sum1+sum2;
printf("%d %d %d",sum1,sum2,sss);
return 0;
}