from scipy.sparse import csr_matrix as csr

from scipy.sparse.csgraph import connected_components as cc





def solve(string):

    n, m, *xyz = list(map(int, string.split()))

    return str(cc(csr(([1] * m, (xyz[::3], xyz[1::3])), shape=(n + 1, n + 1)))[0] - 1)





if __name__ == '__main__':

    n, m = list(map(int, input().split()))

    print((solve('{} {}\n'.format(n, m) + '\n'.join([eval(input()) for _ in range(m)]))))
def solve(string):

    n, m, *xyz = list(map(int, string.split()))

    t = [-1] * (n + 1)

    for x, y, z in zip(*[iter(xyz)] * 3):



        def union(r1, r2):

            new_root = min(r1, r2)

            if r1 != r2:

                t[max(r1, r2)] = new_root



        def find(x):

            if t[x] < 0:

                return x

            t[x] = find(t[x])

            return t[x]



        union(find(x), find(y))

    return str(t[1:].count(-1))





if __name__ == '__main__':

    n, m = list(map(int, input().split()))

    print((solve('{} {}\n'.format(n, m) + '\n'.join([eval(input()) for _ in range(m)]))))

from scipy.sparse import csr_matrix as csr
from scipy.sparse.csgraph import connected_components as cc


def solve(string):
    n, m, *xyz = list(map(int, string.split()))
    return str(cc(csr(([1] * m, (xyz[::3], xyz[1::3])), shape=(n + 1, n + 1)))[0] - 1)


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    print(
        (solve("{} {}\n".format(n, m) + "\n".join([eval(input()) for _ in range(m)])))
    )

def solve(string):
    n, m, *xyz = list(map(int, string.split()))
    t = [-1] * (n + 1)
    for x, y, z in zip(*[iter(xyz)] * 3):

        def union(r1, r2):
            new_root = min(r1, r2)
            if r1 != r2:
                t[max(r1, r2)] = new_root

        def find(x):
            if t[x] < 0:
                return x
            t[x] = find(t[x])
            return t[x]

        union(find(x), find(y))
    return str(t[1:].count(-1))


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    print(
        (solve("{} {}\n".format(n, m) + "\n".join([eval(input()) for _ in range(m)])))
    )

-from scipy.sparse import csr_matrix as csr -from scipy.sparse.csgraph import connected_components as cc - - -    return str(cc(csr(([1] * m, (xyz[::3], xyz[1::3])), shape=(n + 1, n + 1)))[0] - 1) +    t = [-1] * (n + 1) +    for x, y, z in zip(*[iter(xyz)] * 3): + +        def union(r1, r2): +            new_root = min(r1, r2) +            if r1 != r2: +                t[max(r1, r2)] = new_root + +        def find(x): +            if t[x] < 0: +                return x +            t[x] = find(t[x]) +            return t[x] + +        union(find(x), find(y)) +    return str(t[1:].count(-1))