type
 niz=array[1..100000] of integer;


var
 n,m,aa,i,bb,z,j,o,suma:integer;
 a,b,t:niz;
 f,g:text;

procedure qsort(l,r:integer;var a:niz);
 var
  i,j,ya,yb,yt,k:integer;
    begin
    i:=l;
    j:=r;
    k:=a[(l+r) div 2];
     repeat
      while a[i]<k do
       i:=i+1;
      while k<a[j] do
       j:=j-1;
      if not(i>j) then
       begin

          ya:=a[i];
          a[i]:=a[j];
          a[j]:=ya;
          yb:=b[i];
          b[i]:=b[j];
          b[j]:=yb;
          yt:=t[i];
          t[i]:=t[j];
          t[j]:=yt;
          i:=i+1;
          j:=j-1;
       end;
    until i>j;
     if l<j then qsort(l,j,a);
     if i<r then qsort(i,r,a);
   end;

begin
suma:=0;
assign(f,'nishtel.in');
reset(f);
assign(g,'nishtel.out');
rewrite(g);
 readln(f,n,m,aa,bb);
  for i:=1 to m do
   read(f,a[i],b[i],t[i]);
   qsort(1,m,a);
   i:=1;
   j:=1;
   while j<=m do begin
    while a[i]=a[j] do
      j:=j+1;
      if j>i then
       for o:=i to j do
        if ((b[o]=b[o+1])and(a[o]=a[o+1])) or ((b[o]=b[o+2])and(a[o]=a[o+2])) then  begin
         suma:=suma+1;

         end;
      i:=j;

   end;


     writeln(g,(suma+1) div 2);



close(f);
close(g);

end.