module Main where
import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B
import Data.Graph
import Control.Monad
import Data.List(sortOn, foldl')
import Data.Maybe
import qualified Data.Array.IO as MV
import qualified Data.Array as A

readIntPair :: B.ByteString -> (Int,Int)
readIntPair s = p
    where
    Just p = do
        (n1, s') <- B.readInt s
        (n2, _ ) <- B.readInt (B.tail s')
        return (n1,n2)
readIntTriple :: B.ByteString -> (Int,Int, Int)
readIntTriple s = p
    where
    Just p = do
        (n1, s') <- B.readInt s
        (n2, s'') <- B.readInt (B.tail s')
        (n3, _) <- B.readInt (B.tail s'')
        return (n1,n2,n3)

main :: IO ()
main = do
    (n,m) <- fmap readIntPair B.getLine 
    xs <- fmap (map (fst . fromJust . B.readInt) . B.words) B.getLine
    es <- fmap (map readIntTriple . B.lines) B.getContents
    parent <- MV.newArray (1,n) 0 :: IO (MV.IOUArray Int Int)
    rankTbl <- MV.newArray (1,n) 0 :: IO (MV.IOUArray Int Int)
    costTbl <- MV.newArray (1,n) 0 :: IO (MV.IOUArray Int Int)
    countTbl <- MV.newArray (1,n) 0 :: IO (MV.IOUArray Int Int)
    forM_ [1..n] $ \i -> do 
        MV.writeArray parent i i
        MV.writeArray rankTbl i 1
    zipWithM_ (\i x -> MV.writeArray costTbl i x) [1..] xs
    let find :: Int -> IO Int
        find a = do
            a' <- MV.readArray parent a
            if a == a' then return a else do
                b <- find a'
                MV.writeArray parent a b
                return b
        unite :: Int -> Int -> IO ()
        unite a b = do
            x <- find a
            y <- find b
            unless (x == y) $ do
                s1 <- MV.readArray rankTbl x
                s2 <- MV.readArray rankTbl y
                c1 <- MV.readArray costTbl x
                c2 <- MV.readArray costTbl y
                d1 <- MV.readArray countTbl x
                d2 <- MV.readArray countTbl y
                if s1 < s2
                    then do
                        MV.writeArray parent x y
                        MV.writeArray costTbl y (c1 + c2)
                        MV.writeArray countTbl y (d1 + d2)
                    else if s2 < s1
                        then do
                            MV.writeArray parent y x
                            MV.writeArray costTbl x (c1 + c2)
                            MV.writeArray countTbl x (d1 + d2)
                        else do 
                            MV.writeArray parent x y
                            MV.writeArray rankTbl x (s1 + 1)
                            MV.writeArray costTbl y (c1 + c2)
                            MV.writeArray countTbl y (d1 + d2)
    let es' = sortOn (\(a,b,y) -> y) es
    ans <- foldM (\acc (a,b,y) -> do
        unite a b
        x <- find a
        c <- MV.readArray costTbl x
        d <- MV.readArray countTbl x
        --  print ("a",a,"b",b,"c",c,"d",d,"y",y, "c>=y",c>=y)
        if c >= y then do
            MV.writeArray countTbl x 0
            return (acc + d + 1)
        else do
            MV.writeArray countTbl x (d+1) 
            return acc) 0 es'
    print (m - ans)
            
