import Control.Monad import Data.Maybe import qualified Data.ByteString.Char8 as B rInt :: B.ByteString -> Int rInt = fst . fromJust . B.readInt sleep [[h1, m1], [h2, m2]] t = (h2' * 60 + m2) - (h1 * 60 + m1) + t where h2' | (h2 * 60 + m2) < (h1 * 60 + m1) = h2 + 24 | otherwise = h2 main = do n <- readLn times <- map (map (map rInt . (B.split ':')) . B.words) <$> replicateM n B.getLine print $ foldr sleep 0 times