import java.io.*;
import java.util.*;
import static java.lang.Math.*;

public class diversion_as implements Runnable {
    ArrayList<Edge>[] edges;

    int answer;

    int[] enter;
    int[] phi;
    int[] phi2;
    int time;

    boolean[] u;

    class Edge {
        int s;
        int d;
        boolean main;
        boolean alive;
        Edge r;

        public Edge(int s, int d, boolean main) {
            this.s = s;
            this.d = d;
            this.main = main;
            alive = true;
        }

        public String toString() {
            return s + "->" + d;
        }

        public int hashCode() {
            return 29 * s + d;
        }
    }

    void dfs1(int x) {
        u[x] = true;
        for (Edge e : edges[x]) {
            if (e.alive) {
                if (e.main) {
                    e.r.alive = false;
                    dfs1(e.d);
                } else {
                    if (u[e.d]) {
                        e.r.alive = false;
                    } else {
                        e.alive = false;
                    }
                }
            }
        }
    }

    void dfs(int x) {
        u[x] = true;
        enter[x] = time++;
        phi[x] = enter[x];
        phi2[x] = enter[x];

        for (Edge e : edges[x]) {
            if (e.alive) {
                if (!u[e.d]) {
                    dfs(e.d);

                    if (phi[e.d] < phi[x]) {
                        phi2[x] = phi[x];
                        phi[x] = phi[e.d];
                        phi2[x] = min(phi2[x], phi2[e.d]);
                    } else {
                        phi2[x] = min(phi2[x], phi[e.d]);
                    }
                } else {
                    if (enter[e.d] < phi[x]) {
                        phi2[x] = phi[x];
                        phi[x] = enter[e.d];
                    } else {
                        phi2[x] = min(phi2[x], enter[e.d]);
                    }
                }
            }
        }

        assert x == 0 || phi[x] < enter[x];
        if (x != 0 && phi2[x] >= enter[x]) {
            answer++;
        }
    }


    @SuppressWarnings("unchecked")
    public void solve() throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("diversion.in"));
        PrintWriter out = new PrintWriter("diversion.out");

        StringTokenizer st = new StringTokenizer(in.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        edges = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            edges[i] = new ArrayList<Edge>();
        }

        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(in.readLine());
            int x = Integer.parseInt(st.nextToken()) - 1;
            int y = Integer.parseInt(st.nextToken()) - 1;
            boolean mn = Integer.parseInt(st.nextToken()) == 1;
            Edge e1 = new Edge(x, y, mn);
            Edge e2 = new Edge(y, x, mn);
            e1.r = e2;
            e2.r = e1;
            edges[x].add(e1);
            edges[y].add(e2);
        }

        u = new boolean[n];
        dfs1(0);

        enter = new int[n];
        u = new boolean[n];
        phi = new int[n];
        phi2 = new int[n];

        dfs(0);

        for (int i = 0; i < n; i++) {
            assert u[i];
        }

        out.println(answer);

        in.close();
        out.close();
    }
    public void run() {
        try {
            solve();
        } catch (IOException e) {
            System.exit(64);
        }
    }

    public static void main(String[] arg) throws IOException {
        new Thread(new diversion_as()).start();
    }
}
