//递归实现所有情况的组合,随后比较返回最大的value
//用flag标记该物品是否被选,如果value比较结果小,则返回重新置零
#include<stdio.h>
struct a{
int cost,x;
int flag;
}a[1005];
int f(struct a ans,int i,struct a a[1005],int flag,int m,int n){
int max1=0,max2=0;
a[i].flag=0;
if(i==n) return ans.x;
if(m<a[i].cost||flag==0){
max1=f(ans,i+1,a,0,m,n)>f(ans,i+1,a,1,m,n)?f(ans,i+1,a,0,m,n):f(ans,i+1,a,1,m,n);
}
else if(m>=a[i].cost&&flag==1){
a[i].flag=1;
m-=a[i].cost;
ans.x+=a[i].x;
max2=f(ans,i+1,a,0,m,n)>f(ans,i+1,a,1,m,n)?f(ans,i+1,a,0,m,n):f(ans,i+1,a,1,m,n);
}
return max1>max2?max1:max2;
}
int main(){
int m,n,res1,res2;
while(~scanf("%d %d",&m,&n)){
struct a ans;
ans.cost=0;
ans.x=0;
for(int i=0;i<n;i++) scanf("%d %d",&a[i].cost,&a[i].x);
res1=f(ans,0,a,0,m,n);
res2=f(ans,0,a,1,m,n);
int res=res1>res2?res1:res2;
if(res==0) printf("0\n");
else{
printf("%d\n",res);
int t=0;
for(int i=0;i<n;i++){
if(a[i].flag==1&&t==0) {
printf("%d",i+1);
t=1;
}
else if(a[i].flag==1)printf(" %d",i+1);
}
printf("\n");
}
}
return 0;
}