from collections import deque

    

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



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



for i in range(M):

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

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

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



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



def BFS(start,color,checked_list,node_list):

    Q=deque([start])

    checked_list[start]=color

    while len(Q)>0:

        v1 = Q.pop()

        for v2 in node_list[v1]:

            if checked_list[v2]==-1:

                checked_list[v2]=color

                Q.appendleft(v2)





count = 0

for i in range(N):

    if checked_list[i]==-1:

        BFS(i,count,checked_list,node_list)

        count+=1

        

print(count)
import sys

def input():

    return sys.stdin.readline()[:-1]



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



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



for i in range(M):

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

    x,y = x-1,y-1

    e_list[x].append(y)

    e_list[y].append(x)



from collections import deque



def BFS(vi,color,color_list):

    Q = deque([vi])

    color_list[vi]=color

    while len(Q)>0:

        v = Q.pop()

        for v1 in e_list[v]:

            if color_list[v1]==-1:

                color_list[v1]=color

                Q.appendleft(v1)



color_list = [-1]*N

color = 0



for i in range(N):

    if color_list[i]==-1:

        BFS(i,color,color_list)

        color+=1



print(color)
from collections import deque

N, M = list(map(int, input().split()))
node_list = [[] for i in range(N)]
for i in range(M):
    x, y, z = list(map(int, input().split()))
    node_list[x - 1].append(y - 1)
    node_list[y - 1].append(x - 1)
checked_list = [-1 for i in range(N)]


def BFS(start, color, checked_list, node_list):
    Q = deque([start])
    checked_list[start] = color
    while len(Q) > 0:
        v1 = Q.pop()
        for v2 in node_list[v1]:
            if checked_list[v2] == -1:
                checked_list[v2] = color
                Q.appendleft(v2)


count = 0
for i in range(N):
    if checked_list[i] == -1:
        BFS(i, count, checked_list, node_list)
        count += 1
print(count)

import sys


def input():
    return sys.stdin.readline()[:-1]


N, M = list(map(int, input().split()))
e_list = [[] for i in range(N)]
for i in range(M):
    x, y, z = list(map(int, input().split()))
    x, y = x - 1, y - 1
    e_list[x].append(y)
    e_list[y].append(x)
from collections import deque


def BFS(vi, color, color_list):
    Q = deque([vi])
    color_list[vi] = color
    while len(Q) > 0:
        v = Q.pop()
        for v1 in e_list[v]:
            if color_list[v1] == -1:
                color_list[v1] = color
                Q.appendleft(v1)


color_list = [-1] * N
color = 0
for i in range(N):
    if color_list[i] == -1:
        BFS(i, color, color_list)
        color += 1
print(color)

+import sys + + +def input(): +    return sys.stdin.readline()[:-1] + + +N, M = list(map(int, input().split())) +e_list = [[] for i in range(N)] +for i in range(M): +    x, y, z = list(map(int, input().split())) +    x, y = x - 1, y - 1 +    e_list[x].append(y) +    e_list[y].append(x) -N, M = list(map(int, input().split())) -node_list = [[] for i in range(N)] -for i in range(M): -    x, y, z = list(map(int, input().split())) -    node_list[x - 1].append(y - 1) -    node_list[y - 1].append(x - 1) -checked_list = [-1 for i in range(N)] + +def BFS(vi, color, color_list): +    Q = deque([vi]) +    color_list[vi] = color +    while len(Q) > 0: +        v = Q.pop() +        for v1 in e_list[v]: +            if color_list[v1] == -1: +                color_list[v1] = color +                Q.appendleft(v1) -def BFS(start, color, checked_list, node_list): -    Q = deque([start]) -    checked_list[start] = color -    while len(Q) > 0: -        v1 = Q.pop() -        for v2 in node_list[v1]: -            if checked_list[v2] == -1: -                checked_list[v2] = color -                Q.appendleft(v2) - - -count = 0 +color_list = [-1] * N +color = 0 -    if checked_list[i] == -1: -        BFS(i, count, checked_list, node_list) -        count += 1 -print(count) +    if color_list[i] == -1: +        BFS(i, color, color_list) +        color += 1 +print(color)