{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Foldable
import Data.Graph
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B
import Debug.Trace

solve :: Int -> Int -> VU.Vector (Int, Int, Int) -> Int
solve !n !m !xyz = length $ components $ buildG
  (0, n - 1)
  (VU.toList $ VU.map (\(x, y, z) -> (x, y)) xyz)
 where
  graph :: V.Vector [Int]
  graph = V.accumulate (flip (:)) (V.replicate n []) $ V.convert $ VU.map
    (\(x, y, z) -> (x, y))
    xyz

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, m) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  uvw <-
    VU.replicateM m
    $   (\vec -> (vec VU.! 0 - 1, vec VU.! 1 - 1, vec VU.! 2))
    .   VU.unfoldrN 3 readInt
    <$> B.getLine
  print $ solve n m uvw
