program lygiagretus_veiksmai;
   const PR = 'PROG.DAT';
         RZ = 'PROG.REZ';
         MAX_N = 1000; { maksimalus atminties laukeli— skai‡ius }
         MAX_K = 10000; { maksimalus komand— skai‡ius }
   type atmintis = array [1..MAX_N] of integer;
        komanda = record
                    blokas, num: integer;
                  end;
        komandos = array [1..MAX_K+1] of komanda;


   { ‘emiau apra˛yti globalieji kintamieji }
   var bl_sk,       { minimalus blok— skai‡ius }
       N,           { atminties laukeli— skai‡ius }
       sk: integer; { komand— skai‡ius }
       kom: komandos; { kuriam blokui priklauso komanda }
       atm: atmintis; { kuriame bloke paskutin¨ kart… panaudotas laukelis }

  function max (x, y: integer): integer;
  begin
    if x > y
       then max := x
       else max := y;
  end; { max }

  procedure skaityti_komanda (var f: text;
                              var k: char; { komanda }
                              var lau1, lau2: integer);
    var s, pirmas, antras: string;
        kodas: integer;
  begin
    readln (f, s);
    k := s[1];
    { praleid‘iami tarpai }
    while not (s[1] in ['0'..'9']) do
     delete (s, 1, 1);
    pirmas := ''; antras := '';
    while s[1] in ['0'..'9'] do
      begin
        pirmas := pirmas + s[1];
        delete (s, 1, 1);
      end;
    { praleid‘iami tarpai }
    while not (s[1] in ['0'..'9']) do
      delete (s, 1, 1);
    antras := s;
    val (pirmas, lau1, kodas);
    if k in ['M', 'S']
       then val (antras, lau2, kodas);
  end; { skaityti_komand… }

  procedure kom_i_blokus (var f: text; var kom: komandos);
    { komandos suskirstomos ¨ blokus }
    var i, lau1, lau2: integer;
        k: char;
  begin
    { inicializuojamos lentel‚s atm ir kom }
    for i := 1 to N do
      atm[i] := 0; { kuriame bloke paskutin¨ kart… buvo pan. ˛is laukelis }
    for i := 1 to sk do
      begin
        kom[i].blokas := 0; { kuriam blokui priklauso komanda }
        kom[i].num := i;
      end;
    for i := 1 to sk do
      begin
        { perskaitome komand… }
        skaityti_komanda (f, k, lau1, lau2);
        { j… ¨ra˛ome ¨ blok… }
        if k in ['M', 'S']
           then kom[i].blokas := max(atm[lau1], atm[lau2]) + 1
           else kom[i].blokas := atm[lau1] + 1;
        atm[lau1] := kom[i].blokas;
        if k in ['M', 'S']
           then atm[lau2] := kom[i].blokas;
      end;
  end; { kom_¨_blokus }

  procedure quicksort (var kom: komandos; Lo,Hi: integer);
    procedure sort(l,r: integer);
      var i,j,bl, nr: integer;
          x, y: komanda;
    begin
      i:=l; j:=r; x:=kom[(l+r) DIV 2];
      repeat
        while (kom[i].blokas = x.blokas) and (kom[i].num < x.num) or
              (kom[i].blokas < x.blokas) do i:=i+1;
        while (x.blokas = kom[j].blokas) and (x.num < kom[j].num) or
              (x.blokas<kom[j].blokas) do j:=j-1;
        if i<=j then
        begin
          y := kom[i];
          kom[i]:=kom[j];
          kom[j] := y;
          i:=i+1; j:=j-1;
        end;
      until i>j;
      if l<j then sort(l,j);
      if i<r then sort(i,r);
    end; { sort }
    begin {quicksort};
      sort(Lo,Hi);
    end;

  procedure skaityti (var f: text;
                      var N, sk: integer);
    var i: integer;
  begin
    assign (f, PR);
    reset (f);
    readln (f, N, sk);
  end; { skaityti }

  procedure spausdinti (bl_sk: integer);
    var f: text;
        i, nuo: integer;
  begin
    assign (f, RZ);
    rewrite (f);
    kom[sk+1].blokas := sk+1;
    nuo := 1;
    for i := 1 to bl_sk do
      begin
        while kom[nuo].blokas = i do
          begin
            writeln (f, kom[nuo].num);
            nuo := nuo+1;
          end;
        writeln (f, 0);
      end;
    close (f);
  end; { spausdinti }

  function bloku_skaicius (const kom: komandos): integer;
    var min, i: integer;
  begin
    min := 1;
    for i := 1 to sk do
      if kom[i].blokas > min
         then min := kom[i].blokas;
    bloku_skaicius := min;
  end; { blok—_skai‡ius }

  var f: text;
begin
  skaityti (f, N, sk);
  kom_i_blokus (f, kom);
  close (f);
  bl_sk := bloku_skaicius (kom);
  quicksort (kom, 1, sk);
  spausdinti (bl_sk);
end.


