import sys

sys.setrecursionlimit(10 ** 5)



class UniteFind:

    def __init__(self, N):

        self.par = [i for i in range(N)]



    def root(self, x):

        if self.par[x] == x:

            return x

        else:

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

            return self.par[x]



    def unite(self, x, y):

        rx = self.root(x)

        ry = self.root(y)

        if rx == ry:

            return

        self.par[ry] = rx



    def same(self, x, y):

        if (self.root(x) == self.root(y)):

            return True

        return False





N, M = list(map(int, input().split()))

rf = UniteFind(N)



for _ in range(M):

    x, y, _ = list(map(int, input().split()))

    rf.unite(x-1, y-1)



m = {i: -1 for i in range(N)}

ans = 0



for i in range(N):

    if m[rf.root(i)] == -1:

        m[rf.root(i)] = 1

        ans += 1



print(ans)

import sys

sys.setrecursionlimit(10 ** 5)



input = sys.stdin.readline



N, M = list(map(int, input().split()))



ans = list(range(N))

def root(a):

    if a==ans[a]:

        return a

    else:

        ans[a] = root(ans[a]) 

        return ans[a]



for _ in range(M):

    x, y, _ = [int(a)-1 for a in input().split()]

    rx, ry = root(x), root(y)

    if rx != ry:

        ans[ry] = rx



count = 0

for i,a in enumerate(ans):

    count += i==a





print(count)

import sys

sys.setrecursionlimit(10**5)


class UniteFind:
    def __init__(self, N):
        self.par = [i for i in range(N)]

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

    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return
        self.par[ry] = rx

    def same(self, x, y):
        if self.root(x) == self.root(y):
            return True
        return False


N, M = list(map(int, input().split()))
rf = UniteFind(N)
for _ in range(M):
    x, y, _ = list(map(int, input().split()))
    rf.unite(x - 1, y - 1)
m = {i: -1 for i in range(N)}
ans = 0
for i in range(N):
    if m[rf.root(i)] == -1:
        m[rf.root(i)] = 1
        ans += 1
print(ans)

import sys

sys.setrecursionlimit(10**5)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
ans = list(range(N))


def root(a):
    if a == ans[a]:
        return a
    else:
        ans[a] = root(ans[a])
        return ans[a]


for _ in range(M):
    x, y, _ = [int(a) - 1 for a in input().split()]
    rx, ry = root(x), root(y)
    if rx != ry:
        ans[ry] = rx
count = 0
for i, a in enumerate(ans):
    count += i == a
print(count)

+input = sys.stdin.readline +N, M = list(map(int, input().split())) +ans = list(range(N)) -class UniteFind: -    def __init__(self, N): -        self.par = [i for i in range(N)] - -    def root(self, x): -        if self.par[x] == x: -            return x -        else: -            self.par[x] = self.root(self.par[x]) -            return self.par[x] - -    def unite(self, x, y): -        rx = self.root(x) -        ry = self.root(y) -        if rx == ry: -            return -        self.par[ry] = rx - -    def same(self, x, y): -        if self.root(x) == self.root(y): -            return True -        return False +def root(a): +    if a == ans[a]: +        return a +    else: +        ans[a] = root(ans[a]) +        return ans[a] -N, M = list(map(int, input().split())) -rf = UniteFind(N) -    x, y, _ = list(map(int, input().split())) -    rf.unite(x - 1, y - 1) -m = {i: -1 for i in range(N)} -ans = 0 -for i in range(N): -    if m[rf.root(i)] == -1: -        m[rf.root(i)] = 1 -        ans += 1 -print(ans) +    x, y, _ = [int(a) - 1 for a in input().split()] +    rx, ry = root(x), root(y) +    if rx != ry: +        ans[ry] = rx +count = 0 +for i, a in enumerate(ans): +    count += i == a +print(count)