import sys

sys.setrecursionlimit(10**8)

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

num_edges = M

edges = [list(map(int, input().split())) for i in range(num_edges)]

adj = [[] for i in range(N+1)]

for ed in edges:

    adj[ed[0]] += [[ed[1], ed[2]]]

    adj[ed[1]] += [[ed[0], ed[2]]]

col_list = [-1 for i in range(N+1)]



def recfun(v, col):

    col_list[v] = col #色を更新

    for ed in adj[v]: #隣接する頂点をみる

        if col_list[ed[0]] == -1: #更新していない場合

            recfun(ed[0], col) #同じ色でその先をみる

count = 0

for i in range(1, N+1):

    if col_list[i] == -1:

        count +=1

        recfun(i, count)

print(count)
class UnionFind():

    def __init__(self,size):

        self.table=[-1]*size

    def root(self,x):

        while self.table[x]>=0:

            x=self.table[x]

        return x

    def unite(self,x,y):

        s1=self.root(x)

        s2=self.root(y)

        if s1!=s2:

            if self.table[s1]>self.table[s2]:

                s1,s2=s2,s1

            self.table[s1]+=self.table[s2]

            self.table[s2]=s1

        return

    def same(self,x,y):

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

    def size(self,x):

        return -self.table[self.root(x)]

    

#####

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

uf = UnionFind(N+1)

for i in range(M):

    ui, vi, wi = list(map(int, input().split()))

    uf.unite(ui, vi)

print((sum([t < 0 for t in uf.table[1:]])))
import sys

sys.setrecursionlimit(10**8)
N, M = list(map(int, input().split()))
num_edges = M
edges = [list(map(int, input().split())) for i in range(num_edges)]
adj = [[] for i in range(N + 1)]
for ed in edges:
    adj[ed[0]] += [[ed[1], ed[2]]]
    adj[ed[1]] += [[ed[0], ed[2]]]
col_list = [-1 for i in range(N + 1)]


def recfun(v, col):
    col_list[v] = col  # 色を更新
    for ed in adj[v]:  # 隣接する頂点をみる
        if col_list[ed[0]] == -1:  # 更新していない場合
            recfun(ed[0], col)  # 同じ色でその先をみる


count = 0
for i in range(1, N + 1):
    if col_list[i] == -1:
        count += 1
        recfun(i, count)
print(count)

class UnionFind:
    def __init__(self, size):
        self.table = [-1] * size

    def root(self, x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def unite(self, x, y):
        s1 = self.root(x)
        s2 = self.root(y)
        if s1 != s2:
            if self.table[s1] > self.table[s2]:
                s1, s2 = s2, s1
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        return

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

    def size(self, x):
        return -self.table[self.root(x)]


#####
N, M = list(map(int, input().split()))
uf = UnionFind(N + 1)
for i in range(M):
    ui, vi, wi = list(map(int, input().split()))
    uf.unite(ui, vi)
print((sum([t < 0 for t in uf.table[1:]])))

-import sys +class UnionFind: +    def __init__(self, size): +        self.table = [-1] * size -sys.setrecursionlimit(10**8) -N, M = list(map(int, input().split())) -num_edges = M -edges = [list(map(int, input().split())) for i in range(num_edges)] -adj = [[] for i in range(N + 1)] -for ed in edges: -    adj[ed[0]] += [[ed[1], ed[2]]] -    adj[ed[1]] += [[ed[0], ed[2]]] -col_list = [-1 for i in range(N + 1)] +    def root(self, x): +        while self.table[x] >= 0: +            x = self.table[x] +        return x + +    def unite(self, x, y): +        s1 = self.root(x) +        s2 = self.root(y) +        if s1 != s2: +            if self.table[s1] > self.table[s2]: +                s1, s2 = s2, s1 +            self.table[s1] += self.table[s2] +            self.table[s2] = s1 +        return + +    def same(self, x, y): +        return self.root(x) == self.root(y) + +    def size(self, x): +        return -self.table[self.root(x)] -def recfun(v, col): -    col_list[v] = col  # 色を更新 -    for ed in adj[v]:  # 隣接する頂点をみる -        if col_list[ed[0]] == -1:  # 更新していない場合 -            recfun(ed[0], col)  # 同じ色でその先をみる - - -count = 0 -for i in range(1, N + 1): -    if col_list[i] == -1: -        count += 1 -        recfun(i, count) -print(count) +##### +N, M = list(map(int, input().split())) +uf = UnionFind(N + 1) +for i in range(M): +    ui, vi, wi = list(map(int, input().split())) +    uf.unite(ui, vi) +print((sum([t < 0 for t in uf.table[1:]])))