program bandymas;
  const
    MAX = 20;
  type
    Tmas = array [0 .. MAX] of integer;
  var
    f1, f2: text;
    yra, kas, kur: Tmas;
    temp1, temp2, temp3, temp4, z, p, i, j, viso: integer;
    N, K: integer;
begin
  assign (f1, 'BAND.IN');
  assign (f2, 'BAND.OUT');
  reset (f1);
  rewrite (f2);
  readln (f1, N, K);

  { Initializacija }

  viso := 0;
  for z := 1 to N do begin
    kas [z] := z;
    kur [z] := z;
    yra [z] := 1;
  end;

  for z := 1 to K do begin
    readln (f1, p, i, j);

    if yra [kas[p]] = 1 then begin
      yra [kas[p]] := 0;
      inc (viso);
    end;

    temp1 := kur [i];
    temp2 := kur [j];
    kur [i] := temp2;
    kur [j] := temp1;

    temp3 := kas [temp1];
    temp4 := kas [temp2];
    kas [temp1] := temp4;
    kas [temp2] := temp3;
  end;
  writeln (f2, viso);
  close (f1);
  close (f2);
end.
