class UnionFind:

    def __init__(self, n):

        self.par = [i for i in range(n+1)]

        self.rank = [0] * (n+1)



    def find(self, x):

        if self.par[x] == x:

            return x

        else:

            self.par[x] = self.find(self.par[x])

            return self.par[x]



    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if self.rank[x] < self.rank[y]:

            self.par[x] = y

        else:

            self.par[y] = x

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1



    def same_check(self, x, y):

        return self.find(x) == self.find(y)

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

uni = UnionFind(n-1)



for i in range(m):

    a,b,c=list(map(int,input().split()))

    a-=1

    b-=1

    uni.union(a,b)

t = set([])

for i in range(n):

    p = uni.find(i)

    t.add(p)

print((len(t)))
def main():

    #Union Find

    #locate x 

    def find(x):

        if par[x] < 0:

            return x

        else:

            par[x] = find(par[x])

            return par[x]

    #merge x y

    def unite(x,y):

        x = find(x)

        y = find(y)

        if x == y:

            return False

        else:

            #sizeの大きいほうがx

            if par[x] > par[y]:

                x,y = y,x

            par[x] += par[y]

            par[y] = x

            return True

    #x and y in same group

    def same(x,y):

        return find(x) == find(y)

    

    #number of factor of x group

    def size(x):

        return -par[find(x)]



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

    #initiarize

    #根なら-size,子なら親の頂点

    par = [-1]*n



    for i in range(m):

        X,Y,Z = list(map(int,input().split()))

        unite(X-1,Y-1)

    

    tank = set([])

    for i in range(n):

        tank.add(find(i))

    print((len(tank)))



if __name__ == '__main__':

    main()
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same_check(self, x, y):
        return self.find(x) == self.find(y)


n, m = list(map(int, input().split()))
uni = UnionFind(n - 1)
for i in range(m):
    a, b, c = list(map(int, input().split()))
    a -= 1
    b -= 1
    uni.union(a, b)
t = set([])
for i in range(n):
    p = uni.find(i)
    t.add(p)
print((len(t)))

def main():
    # Union Find
    # locate x
    def find(x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par[x])
            return par[x]

    # merge x y
    def unite(x, y):
        x = find(x)
        y = find(y)
        if x == y:
            return False
        else:
            # sizeの大きいほうがx
            if par[x] > par[y]:
                x, y = y, x
            par[x] += par[y]
            par[y] = x
            return True

    # x and y in same group
    def same(x, y):
        return find(x) == find(y)

    # number of factor of x group
    def size(x):
        return -par[find(x)]

    n, m = list(map(int, input().split()))
    # initiarize
    # 根なら-size,子なら親の頂点
    par = [-1] * n
    for i in range(m):
        X, Y, Z = list(map(int, input().split()))
        unite(X - 1, Y - 1)
    tank = set([])
    for i in range(n):
        tank.add(find(i))
    print((len(tank)))


if __name__ == "__main__":
    main()

-class UnionFind: -    def __init__(self, n): -        self.par = [i for i in range(n + 1)] -        self.rank = [0] * (n + 1) - -    def find(self, x): -        if self.par[x] == x: +def main(): +    # Union Find +    # locate x +    def find(x): +        if par[x] < 0: -            self.par[x] = self.find(self.par[x]) -            return self.par[x] +            par[x] = find(par[x]) +            return par[x] -    def union(self, x, y): -        x = self.find(x) -        y = self.find(y) -        if self.rank[x] < self.rank[y]: -            self.par[x] = y +    # merge x y +    def unite(x, y): +        x = find(x) +        y = find(y) +        if x == y: +            return False -            self.par[y] = x -            if self.rank[x] == self.rank[y]: -                self.rank[x] += 1 +            # sizeの大きいほうがx +            if par[x] > par[y]: +                x, y = y, x +            par[x] += par[y] +            par[y] = x +            return True -    def same_check(self, x, y): -        return self.find(x) == self.find(y) +    # x and y in same group +    def same(x, y): +        return find(x) == find(y) + +    # number of factor of x group +    def size(x): +        return -par[find(x)] + +    n, m = list(map(int, input().split())) +    # initiarize +    # 根なら-size,子なら親の頂点 +    par = [-1] * n +    for i in range(m): +        X, Y, Z = list(map(int, input().split())) +        unite(X - 1, Y - 1) +    tank = set([]) +    for i in range(n): +        tank.add(find(i)) +    print((len(tank))) -n, m = list(map(int, input().split())) -uni = UnionFind(n - 1) -for i in range(m): -    a, b, c = list(map(int, input().split())) -    a -= 1 -    b -= 1 -    uni.union(a, b) -t = set([]) -for i in range(n): -    p = uni.find(i) -    t.add(p) -print((len(t))) +if __name__ == "__main__": +    main()