class uf_tree:

    def __init__(self, n):

        self._size = n

        self.sizes = [1] * n

        self.par = list(range(n))

 

    def find(self, x):

        if x == self.par[x]:

            return x

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

        return self.par[x]

 

    def unite(self, x, y):

        self._size -= 1

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

        if x == y:

            return

        if self.sizes[x] < self.sizes[y]:

            x, y = y, x

        self.par[y] = x

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

 

    def same(self, x, y):

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

 

    def size(self, x):

        return self.sizes[self.find(x)]





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

uf = uf_tree(n+1)



for _ in range(m):

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

    if uf.same(x, y):

        continue

    uf.unite(x, y)



print((uf._size - 1))

class UnionFind:

    def __init__(self, n):

        self.parent = [ -1 for _ in range(n) ]

        self._size = n

    def unite(self, x, y):

        x, y = self.root(x), self.root(y)

        if x != y:

            if self.parent[y] < self.parent[x]:

                x, y = y, x

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

            self.parent[y] = x

            self._size -= 1

    def same(self, x, y):

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

    def root(self, x):

        if self.parent[x] < 0:

            return x

        else:

            self.parent[x] = self.root(self.parent[x])

            return self.parent[x]

    def size(self, x):

        return -self.parent[self.find(x)]



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

tree = UnionFind(n)



for i in range(m):

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

    x -= 1; y -= 1;

    tree.unite(x, y)



print((tree._size))
class uf_tree:
    def __init__(self, n):
        self._size = n
        self.sizes = [1] * n
        self.par = list(range(n))

    def find(self, x):
        if x == self.par[x]:
            return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

    def unite(self, x, y):
        self._size -= 1
        x, y = self.find(x), self.find(y)
        if x == y:
            return
        if self.sizes[x] < self.sizes[y]:
            x, y = y, x
        self.par[y] = x
        self.sizes[x] += self.sizes[y]

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

    def size(self, x):
        return self.sizes[self.find(x)]


n, m = list(map(int, input().split()))
uf = uf_tree(n + 1)
for _ in range(m):
    x, y, z = list(map(int, input().split()))
    if uf.same(x, y):
        continue
    uf.unite(x, y)
print((uf._size - 1))

class UnionFind:
    def __init__(self, n):
        self.parent = [-1 for _ in range(n)]
        self._size = n

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x != y:
            if self.parent[y] < self.parent[x]:
                x, y = y, x
            self.parent[x] += self.parent[y]
            self.parent[y] = x
            self._size -= 1

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

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

    def size(self, x):
        return -self.parent[self.find(x)]


n, m = list(map(int, input().split()))
tree = UnionFind(n)
for i in range(m):
    x, y, z = list(map(int, input().split()))
    x -= 1
    y -= 1
    tree.unite(x, y)
print((tree._size))

-class uf_tree: +class UnionFind: +        self.parent = [-1 for _ in range(n)] -        self.sizes = [1] * n -        self.par = list(range(n)) - -    def find(self, x): -        if x == self.par[x]: -            return x -        self.par[x] = self.find(self.par[x]) -        return self.par[x] -        self._size -= 1 -        x, y = self.find(x), self.find(y) -        if x == y: -            return -        if self.sizes[x] < self.sizes[y]: -            x, y = y, x -        self.par[y] = x -        self.sizes[x] += self.sizes[y] +        x, y = self.root(x), self.root(y) +        if x != y: +            if self.parent[y] < self.parent[x]: +                x, y = y, x +            self.parent[x] += self.parent[y] +            self.parent[y] = x +            self._size -= 1 -        return self.find(x) == self.find(y) +        return self.root(x) == self.root(y) + +    def root(self, x): +        if self.parent[x] < 0: +            return x +        else: +            self.parent[x] = self.root(self.parent[x]) +            return self.parent[x] -        return self.sizes[self.find(x)] +        return -self.parent[self.find(x)] -uf = uf_tree(n + 1) -for _ in range(m): +tree = UnionFind(n) +for i in range(m): -    if uf.same(x, y): -        continue -    uf.unite(x, y) -print((uf._size - 1)) +    x -= 1 +    y -= 1 +    tree.unite(x, y) +print((tree._size))