import sys
sys.setrecursionlimit(10**9)


class UnionFind():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()

def main():
    N,M = mi()
    U = UnionFind(2*N+1)

    for i in range(1,N+1):
        U.unite(i,N+i)

    minus = 0
    for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

    print(U.calc_group_num() - 1)




if __name__ == "__main__":
  main() import sys sys sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 class UnionFind():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans UnionFind def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

     __init__ n for n in range(0, N) for n in range(0, N) n range(0, N) range 0 N for n in range(0, N) n self._parent = [n for n in range(0, N)] self._parent self _parent [n for n in range(0, N)] self._size = [1] * N self._size self _size [1] * N [1] 1 N self self N N def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

     find_root if self._parent[x] == x: return x
         self._parent[x] == x self._parent[x] self._parent self _parent x x self._parent[x] = self.find_root(self._parent[x]) self._parent[x] self._parent self _parent x self.find_root(self._parent[x]) self.find_root self find_root self._parent[x] self._parent self _parent x return self._parent[x] self._parent[x] self._parent self _parent x self self x x def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

     unite gx = self.find_root(x) gx self.find_root(x) self.find_root self find_root x gy = self.find_root(y) gy self.find_root(y) self.find_root self find_root y if gx == gy: return

         gx == gy gx gy if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

     self._size[gx] < self._size[gy] self._size[gx] self._size self _size gx self._size[gy] self._size self _size gy self._parent[gx] = gy self._parent[gx] self._parent self _parent gx gy self._size[gy] += self._size[gx] self._size[gy] self._size self _size gy self._size[gx] self._size self _size gx self._parent[gy] = gx self._parent[gy] self._parent self _parent gy gx self._size[gx] += self._size[gy] self._size[gx] self._size self _size gx self._size[gy] self._size self _size gy self self x x y y def get_size(self, x):
        return self._size[self.find_root(x)]

     get_size return self._size[self.find_root(x)] self._size[self.find_root(x)] self._size self _size self.find_root(x) self.find_root self find_root x self self x x def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

     is_same_group return self.find_root(x) == self.find_root(y) self.find_root(x) == self.find_root(y) self.find_root(x) self.find_root self find_root x self.find_root(y) self.find_root self find_root y self self x x y y def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans calc_group_num N = len(self._parent) N len(self._parent) len self._parent self _parent ans = 0 ans 0 for i in range(N):
            if self.find_root(i) == i:
                ans += 1
         i range(N) range N if self.find_root(i) == i:
                ans += 1
         self.find_root(i) == i self.find_root(i) self.find_root self find_root i i ans += 1 ans 1 return ans ans self self def mi(): return map(int,input().split()) mi def ii(): return int(input()) ii def isp(): return input().split() isp def main():
    N,M = mi()
    U = UnionFind(2*N+1)

    for i in range(1,N+1):
        U.unite(i,N+i)

    minus = 0
    for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

    print(U.calc_group_num() - 1) main N,M = mi() N mi() mi M U = UnionFind(2*N+1) U UnionFind(2*N+1) UnionFind 2*N+1 2*N 2 N 1 for i in range(1,N+1):
        U.unite(i,N+i)

     i range(1,N+1) range 1 N+1 N 1 U.unite(i,N+i) U.unite U unite i N+i N i minus = 0 minus 0 for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

     i range(M) range M x,y,z = mi() x mi() mi y z U.unite(x,y) U.unite U unite x y print(U.calc_group_num() - 1) print U.calc_group_num() - 1 U.calc_group_num() U.calc_group_num U calc_group_num 1 if __name__ == "__main__":
  main() __name__ == "__main__" __name__ "__main__" main() main class UnionFind():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans class UnionFind():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans UnionFind def main():
    N,M = mi()
    U = UnionFind(2*N+1)

    for i in range(1,N+1):
        U.unite(i,N+i)

    minus = 0
    for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

    print(U.calc_group_num() - 1) def main():
    N,M = mi()
    U = UnionFind(2*N+1)

    for i in range(1,N+1):
        U.unite(i,N+i)

    minus = 0
    for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

    print(U.calc_group_num() - 1) main def mi(): return map(int,input().split()) def mi(): return map(int,input().split()) mi def isp(): return input().split() def isp(): return input().split() isp def ii(): return int(input()) def ii(): return int(input()) ii
import sys
sys.setrecursionlimit(10**9)


class UnionFind():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def calc_group_num(self):
        N = len(self._parent)
        ans = 0
        for i in range(N):
            if self.find_root(i) == i:
                ans += 1
        return ans

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()

def main():
    N,M = mi()
    U = UnionFind(2*N+1)

    for i in range(1,N+1):
        U.unite(i,N+i)

    minus = 0
    for i in range(M):
        x,y,z = mi()
        U.unite(x,y)

    print(U.calc_group_num() - 1)




if __name__ == "__main__":
  main()
