class UnionFind:

    def __init__(self, n):

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



    def find(self, x):

        if self.v[x] < 0:

            return x

        else:

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

            return self.v[x]



    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        if -self.v[x] < -self.v[y]:

            x, y = y, x

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

        self.v[y] = x



    def root(self, x):

        return self.v[x] < 0



    def same(self, x, y):

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



    def size(self, x):

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





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

uf = UnionFind(N * 2)

for _ in range(M):

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

    x -= 1

    y -= 1



    if z & 1:

        uf.unite(x + N, y)

        uf.unite(x, y + N)

    else:

        uf.unite(x, y)

        uf.unite(x + N, y + N)

print((sum(1 for i in range(N) if uf.root(i))))

class UnionFind:

    def __init__(self, n):

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



    def find(self, x):

        if self.v[x] < 0:

            return x

        else:

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

            return self.v[x]



    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

        if -self.v[x] < -self.v[y]:

            x, y = y, x

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

        self.v[y] = x



    def root(self, x):

        return self.v[x] < 0



    def same(self, x, y):

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



    def size(self, x):

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





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

uf = UnionFind(N)

for _ in range(M):

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

    x -= 1

    y -= 1

    uf.unite(x, y)

print((sum(1 for i in range(N) if uf.root(i))))

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

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if -self.v[x] < -self.v[y]:
            x, y = y, x
        self.v[x] += self.v[y]
        self.v[y] = x

    def root(self, x):
        return self.v[x] < 0

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

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


N, M = list(map(int, input().split()))
uf = UnionFind(N * 2)
for _ in range(M):
    x, y, z = list(map(int, input().split()))
    x -= 1
    y -= 1
    if z & 1:
        uf.unite(x + N, y)
        uf.unite(x, y + N)
    else:
        uf.unite(x, y)
        uf.unite(x + N, y + N)
print((sum(1 for i in range(N) if uf.root(i))))

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

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if -self.v[x] < -self.v[y]:
            x, y = y, x
        self.v[x] += self.v[y]
        self.v[y] = x

    def root(self, x):
        return self.v[x] < 0

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

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


N, M = list(map(int, input().split()))
uf = UnionFind(N)
for _ in range(M):
    x, y, z = list(map(int, input().split()))
    x -= 1
    y -= 1
    uf.unite(x, y)
print((sum(1 for i in range(N) if uf.root(i))))

-uf = UnionFind(N * 2) +uf = UnionFind(N) -    if z & 1: -        uf.unite(x + N, y) -        uf.unite(x, y + N) -    else: -        uf.unite(x, y) -        uf.unite(x + N, y + N) +    uf.unite(x, y)