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

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

for i in range(M):

  a,b,c = list(map(int, input().split()))

  edge[a-1].append(b-1)

  edge[b-1].append(a-1)



from collections import deque



ans = 0

visited = [False]*N

d = deque()

for i in range(N):

  if visited[i]==False:

    visited[i]=True

    d.append(i)

    while len(d)>0:

      v = d.popleft()

      for w in edge[v]:

        if visited[w]==False:

          visited[w]=True

          d.append(w)

    ans += 1

print(ans)
class UnionFind():

  def __init__(self, n):

    self.n = n

    self.parents = [-1] * n



  def find(self, x):

    if self.parents[x] < 0:

      return x

    else:

      self.parents[x] = self.find(self.parents[x])

    return self.parents[x]



  def union(self, x, y):

    x = self.find(x)

    y = self.find(y)



    if x == y:

      return



    if self.parents[x] > self.parents[y]:

          x, y = y, x



    self.parents[x] += self.parents[y]

    self.parents[y] = x



  def same(self, x, y):

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



  def roots(self):

    return [i for i, x in enumerate(self.parents) if x < 0]



  def num_roots(self):

    return len([i for i, x in enumerate(self.parents) if x < 0])



  def members(self, x):

    root = self.find(x)

    return [i for i in range(self.n) if self.find(i) == root]



  def num_members(self,x):

    return abs(self.parents[self.find(x)])



  def __str__(self):

    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



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

uf = UnionFind(N)

for i in range(M):

  a,b,c = list(map(int, input().split()))

  uf.union(a-1,b-1)



print((uf.num_roots()))
N, M = list(map(int, input().split()))
edge = [[] for _ in range(N)]
for i in range(M):
    a, b, c = list(map(int, input().split()))
    edge[a - 1].append(b - 1)
    edge[b - 1].append(a - 1)
from collections import deque

ans = 0
visited = [False] * N
d = deque()
for i in range(N):
    if visited[i] == False:
        visited[i] = True
        d.append(i)
        while len(d) > 0:
            v = d.popleft()
            for w in edge[v]:
                if visited[w] == False:
                    visited[w] = True
                    d.append(w)
        ans += 1
print(ans)

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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
        return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def num_roots(self):
        return len([i for i, x in enumerate(self.parents) if x < 0])

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def num_members(self, x):
        return abs(self.parents[self.find(x)])

    def __str__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())


N, M = list(map(int, input().split()))
uf = UnionFind(N)
for i in range(M):
    a, b, c = list(map(int, input().split()))
    uf.union(a - 1, b - 1)
print((uf.num_roots()))

+class UnionFind: +    def __init__(self, n): +        self.n = n +        self.parents = [-1] * n + +    def find(self, x): +        if self.parents[x] < 0: +            return x +        else: +            self.parents[x] = self.find(self.parents[x]) +        return self.parents[x] + +    def union(self, x, y): +        x = self.find(x) +        y = self.find(y) +        if x == y: +            return +        if self.parents[x] > self.parents[y]: +            x, y = y, x +        self.parents[x] += self.parents[y] +        self.parents[y] = x + +    def same(self, x, y): +        return self.find(x) == self.find(y) + +    def roots(self): +        return [i for i, x in enumerate(self.parents) if x < 0] + +    def num_roots(self): +        return len([i for i, x in enumerate(self.parents) if x < 0]) + +    def members(self, x): +        root = self.find(x) +        return [i for i in range(self.n) if self.find(i) == root] + +    def num_members(self, x): +        return abs(self.parents[self.find(x)]) + +    def __str__(self): +        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots()) + + -edge = [[] for _ in range(N)] +uf = UnionFind(N) -    edge[a - 1].append(b - 1) -    edge[b - 1].append(a - 1) -from collections import deque - -ans = 0 -visited = [False] * N -d = deque() -for i in range(N): -    if visited[i] == False: -        visited[i] = True -        d.append(i) -        while len(d) > 0: -            v = d.popleft() -            for w in edge[v]: -                if visited[w] == False: -                    visited[w] = True -                    d.append(w) -        ans += 1 -print(ans) +    uf.union(a - 1, b - 1) +print((uf.num_roots()))