import sys

sys.setrecursionlimit(10**5+1)



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



edge = [[] for i in range(N)]

for i in range(M):

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

    edge[x-1].append(y-1)

    edge[y-1].append(x-1)



visited = [False] * N



ans = 0

def dfs(x):

    global ans

    if not visited[x]:

        ans += 1

    visited[x] = True



    for x_next in edge[x]:

        if visited[x_next]:

           continue 

        visited[x_next] = True

        dfs(x_next)



for i in range(N):

    dfs(i)



print(ans)
from collections import deque



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



edge = [[] for i in range(N)]

for i in range(M):

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

    edge[x-1].append(y-1)

    edge[y-1].append(x-1)



visited = [False] * N



ans = 0

for i in range(N):

    if visited[i]:

        continue

    else:

        ans += 1



    q = deque([i])

    while q:

        cur = q.pop()

        visited[cur] = True



        for next in edge[cur]:

            if visited[next]:

                continue

            visited[next] = True

            q.append(next)



print(ans)
import sys

sys.setrecursionlimit(10**5 + 1)
N, M = list(map(int, input().split()))
edge = [[] for i in range(N)]
for i in range(M):
    x, y, _ = list(map(int, input().split()))
    edge[x - 1].append(y - 1)
    edge[y - 1].append(x - 1)
visited = [False] * N
ans = 0


def dfs(x):
    global ans
    if not visited[x]:
        ans += 1
    visited[x] = True
    for x_next in edge[x]:
        if visited[x_next]:
            continue
        visited[x_next] = True
        dfs(x_next)


for i in range(N):
    dfs(i)
print(ans)

from collections import deque

N, M = list(map(int, input().split()))
edge = [[] for i in range(N)]
for i in range(M):
    x, y, _ = list(map(int, input().split()))
    edge[x - 1].append(y - 1)
    edge[y - 1].append(x - 1)
visited = [False] * N
ans = 0
for i in range(N):
    if visited[i]:
        continue
    else:
        ans += 1
    q = deque([i])
    while q:
        cur = q.pop()
        visited[cur] = True
        for next in edge[cur]:
            if visited[next]:
                continue
            visited[next] = True
            q.append(next)
print(ans)

-import sys +from collections import deque -sys.setrecursionlimit(10**5 + 1) - - -def dfs(x): -    global ans -    if not visited[x]: +for i in range(N): +    if visited[i]: +        continue +    else: -    visited[x] = True -    for x_next in edge[x]: -        if visited[x_next]: -            continue -        visited[x_next] = True -        dfs(x_next) - - -for i in range(N): -    dfs(i) +    q = deque([i]) +    while q: +        cur = q.pop() +        visited[cur] = True +        for next in edge[cur]: +            if visited[next]: +                continue +            visited[next] = True +            q.append(next)