#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
double sum;
bool mark;
}order[35];
double limit;
int n;
double res;
void dfs(int i,double s)
{
if(i==n)
{
if(s<=limit)
res=max(res,s);
return ;
}
if(order[i].mark)
{
dfs(i+1,s+order[i].sum);
dfs(i+1,s);
}
else
{
dfs(i+1,s);
}
}
int main()
{
while(scanf("%lf%d",&limit,&n)!=EOF&&n!=0)
{
res=0;
for(int i=0;i<35;i++)
{
order[i].sum=0.0;
order[i].mark=true;
}
for(int i=0;i<n;i++)
{
int m;
scanf("%d",&m);
char op;
double price;
for(int j=0;j<m;j++)
{
scanf("%*c%c%*c%lf",&op,&price);
if(op!='A'&&op!='B'&&op!='C')
{
order[i].mark=false;
}
else
{
if(order[i].mark)
{
order[i].sum+=price;
}
}
}
}
dfs(0,0);
printf("%.2lf
",res);
}
return 0;
}