import Data.Char
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
main=print.solve.map(unfoldr$B.readInt.B.dropWhile isSpace).B.lines=<<B.getContents
solve([n,c]:stcs)=M.foldr(+)0.foldl f M.empty.sort$map(\[s,t,c]->(t,s,c))stcs
f m(t,s,c)=M.insertWith(+)(t,c)1$case M.lookup(s,c)m of
  Just _ -> g(s,c)m
  Nothing -> case M.lookupLT(s,0)m of
    Just(k,v) -> g k m
    Nothing -> m
g k m=M.update(\i->if i>1 then Just(i-1) else Nothing)k m