module Main where

import Data.List
import Control.Monad
import Debug.Trace
import Data.Bits
import Data.Array.MArray
import Data.Array.ST
import Control.Monad.ST
import Control.Applicative
import Data.STRef
import qualified Data.Set as S

data UnionFind s = UnionFind {graph :: STArray s Int (Maybe Int), total :: STArray s Int Int}
newUnionFind :: Int -> Int -> ST s (UnionFind s)
newUnionFind start end = liftA2 UnionFind (newArray (start, end) Nothing) (newArray (start, end) 1)

root :: UnionFind s -> Int -> ST s Int
root u i = do
    parent <- readArray (graph u) i
    parent <- case parent of
        Nothing -> do
            writeArray (graph u) i $ Just i
            pure i
        Just p ->
            pure p
    if parent == i then pure parent
    else do
        parent' <- root u parent
        writeArray (graph u) i (Just parent')
        pure parent'

unite :: UnionFind s -> Int -> Int -> ST s ()
unite u ix iy = do
    rx <- root u ix
    ry <- root u iy
    if rx == ry then pure ()
    else do 
        sizex <- size u rx 
        sizey <- size u ry
        if sizex < sizey then do
            -- yが高い yが親
            writeArray (graph u) rx (Just ry)
            writeArray (total u) ry (sizex + sizey) 
        else do
            -- xが高い xが親
            writeArray (graph u) ry (Just rx)
            writeArray (total u) rx (sizex + sizey)

same :: UnionFind s -> Int -> Int -> ST s Bool
same u x y = liftA2 (==) (root u x) (root u y)

size :: UnionFind s -> Int -> ST s Int
size u i = root u i >>= readArray (total u)



readTuple2 :: IO (Int, Int)
readTuple2 = do
    [a, b] <- words <$> getLine
    return (read a, read b)

readTuple3 :: IO (Int, Int, Int)
readTuple3 = do
    [a, b, c] <- words <$> getLine
    return (read a, read b, read c)

solve :: Int -> [(Int, Int, Int)] -> Int
solve n xyzs = runST $ do
    uf <- newUnionFind 1 n
    forM_ xyzs $ \(x,y,_) ->
        unite uf x y
    roots <- forM [1..n] $ \i->
        root uf i
    pure . S.size . S.fromList $ roots
    


main = do
    (n, m) <- readTuple2
    xyzs <- replicateM m readTuple3
    print $ solve n xyzs
