program sandelys;

  const PRF = 'SAND.IN';
        RZF = 'SAND.OUT';
        MAX_K = 1000;
  type     Tsiunta = record
                    valst: String[2];
                    data: String[11];
                    kiekis: longint;
                 end;
       Tmasyvas = array [1..MAX_K] of Tsiunta;
        
  procedure rikiuoti (K: longint; var siunta: Tmasyvas);
    var i, j, min: longint;
        tarp: Tsiunta;
  begin
     for i := 1 to K-1 do begin
       min := i;
       for j := i + 1 to K do
         if siunta[min].data > siunta[j].data then min := j;
         tarp := siunta[i]; siunta[i] := siunta[min]; siunta[min] := tarp
     end;
  end;
  
  function spresti (N, K: longint; salis: String; var siunta: Tmasyvas): longint;
    var prekiu_sk, i, realz, real_vl: longint;
  begin
    rikiuoti (K, siunta);
    // kiek viso prekiu;
    prekiu_sk := 0;
    for i := 1 to K do
      prekiu_sk := prekiu_sk + siunta[i].kiekis;
    realz := prekiu_sk - N;
    real_vl := 0;
    i := 1;
    while realz > 0 do begin
       realz := realz - siunta[i].kiekis;
       if salis = siunta[i].valst
          then if siunta[i].kiekis > realz
                  then real_vl := real_vl + (siunta[i].kiekis - realz)
                  else real_vl := real_vl + siunta[i].kiekis;
       i := i + 1;
    end;
    spresti := real_vl;
  end;
  
  
  procedure skaityti (var N, K: longint; var salis: String; var siunta: Tmasyvas);
     var f: text;
         i, klaida: integer;
         s, galas: String;
  begin
    assign (f, PRF);
    reset (f);
    readln (f, N, K);
    readln (f, salis);
    for i := 1 to K do begin
       readln (f, s);
       siunta[i].valst := s[1] + s[2];
       siunta[i].data := copy (s, 3, 13);
       galas := copy (s, 14, length(s));
       val (galas, siunta[i].kiekis, klaida);
    end;  
  end;
  
  procedure rasyti (kiekis: longint);
    var f: text;
  begin
    assign (f, RZF);
    rewrite (f);
    writeln (f, kiekis);
    close (f);
  end;
  
  var N, K, kiekis: longint;
      salis: String[2];
      siunta: Tmasyvas;
begin
  skaityti (N, K, salis, siunta);
  kiekis := spresti (N, K, salis, siunta);
  rasyti (kiekis);
end.