import sys

from collections import deque

sys.setrecursionlimit(10**9)

input = sys.stdin.readline

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

memo = set(list(range(n)))

E = [[] for i in range(n)]

for i in range(m):

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

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

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

def dfs(cur, pre):

    stack = deque([[cur, pre]])

    while stack:

        cur, pre = stack.pop()

        memo.discard(cur)

        for e in E[cur]:

            if e != pre and e in memo:

                stack.append([e, cur])

ans = 0

for i in range(n):

    if i in memo:

        dfs(i, -1)

        ans += 1

print(ans)

import sys

from collections import deque

sys.setrecursionlimit(10**9)

input = sys.stdin.readline

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

memo = [1 for _ in range(n)]

E = [[] for _ in range(n)]

for i in range(m):

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

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

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

def dfs(cur, pre):

    stack = deque([[cur, pre]])

    while stack:

        cur, pre = stack.pop()

        memo[cur] = 0

        for e in E[cur]:

            if e != pre and memo[e]:

                stack.append([e, cur])

ans = 0

for i in range(n):

    if memo[i]:

        dfs(i, -1)

        ans += 1

print(ans)

import sys
from collections import deque

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
n, m = list(map(int, input().split()))
memo = set(list(range(n)))
E = [[] for i in range(n)]
for i in range(m):
    x, y, z = list(map(int, input().split()))
    E[x - 1].append(y - 1)
    E[y - 1].append(x - 1)


def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        memo.discard(cur)
        for e in E[cur]:
            if e != pre and e in memo:
                stack.append([e, cur])


ans = 0
for i in range(n):
    if i in memo:
        dfs(i, -1)
        ans += 1
print(ans)

import sys
from collections import deque

sys.setrecursionlimit(10**9)
input = sys.stdin.readline
n, m = list(map(int, input().split()))
memo = [1 for _ in range(n)]
E = [[] for _ in range(n)]
for i in range(m):
    x, y, z = list(map(int, input().split()))
    E[x - 1].append(y - 1)
    E[y - 1].append(x - 1)


def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        memo[cur] = 0
        for e in E[cur]:
            if e != pre and memo[e]:
                stack.append([e, cur])


ans = 0
for i in range(n):
    if memo[i]:
        dfs(i, -1)
        ans += 1
print(ans)

-memo = set(list(range(n))) -E = [[] for i in range(n)] +memo = [1 for _ in range(n)] +E = [[] for _ in range(n)] -        memo.discard(cur) +        memo[cur] = 0 -            if e != pre and e in memo: +            if e != pre and memo[e]: -    if i in memo: +    if memo[i]: