import sys

sys.setrecursionlimit(10 ** 7)



def dfs(s):

    if visited[s]:

        return

    visited[s] = 1

    for to in t[s]:

        if visited[to]:

            continue

        dfs(to)

    

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

t = [[] for _ in range(N)]

visited = [0] * N

for _ in range(M):

    X, Y, Z = list(map(int,input().split()))

    X -= 1

    Y -= 1

    t[X].append(Y)

    t[Y].append(X)

ans = 0

for i in range(0, N):

    if visited[i]:

        continue

    ans += 1

    dfs(i)

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

t = [[] for _ in range(N)]

visited = [0] * N

for _ in range(M):

    X, Y, Z = list(map(int,input().split()))

    X -= 1

    Y -= 1

    t[X].append(Y)

    t[Y].append(X)

ans = 0

for i in range(N):

    if visited[i]:

        continue

    ans += 1

    stack = [i]

    while stack:

        s = stack.pop()

        for to in t[s]:

            if visited[to]:

                continue

            visited[to] = 1

            stack.append(to)

print(ans)
import sys

sys.setrecursionlimit(10**7)


def dfs(s):
    if visited[s]:
        return
    visited[s] = 1
    for to in t[s]:
        if visited[to]:
            continue
        dfs(to)


N, M = list(map(int, input().split()))
t = [[] for _ in range(N)]
visited = [0] * N
for _ in range(M):
    X, Y, Z = list(map(int, input().split()))
    X -= 1
    Y -= 1
    t[X].append(Y)
    t[Y].append(X)
ans = 0
for i in range(0, N):
    if visited[i]:
        continue
    ans += 1
    dfs(i)
print(ans)

N, M = list(map(int, input().split()))
t = [[] for _ in range(N)]
visited = [0] * N
for _ in range(M):
    X, Y, Z = list(map(int, input().split()))
    X -= 1
    Y -= 1
    t[X].append(Y)
    t[Y].append(X)
ans = 0
for i in range(N):
    if visited[i]:
        continue
    ans += 1
    stack = [i]
    while stack:
        s = stack.pop()
        for to in t[s]:
            if visited[to]:
                continue
            visited[to] = 1
            stack.append(to)
print(ans)

-import sys - -sys.setrecursionlimit(10**7) - - -def dfs(s): -    if visited[s]: -        return -    visited[s] = 1 -    for to in t[s]: -        if visited[to]: -            continue -        dfs(to) - - -for i in range(0, N): +for i in range(N): -    dfs(i) +    stack = [i] +    while stack: +        s = stack.pop() +        for to in t[s]: +            if visited[to]: +                continue +            visited[to] = 1 +            stack.append(to)