import Control.Monad
import Control.Applicative
import Data.List
import Debug.Trace

type Channel = (Int, Int)

main :: IO ()
main = do
  [n, p, q] <- getInts
  unless (n + p + q == 0)
    $ do
      ns <- replicateM n $ map tupleToMinutes <$>
            getChannel (p, q)
      let candidate = merge ns
      print $ maximum $ map (\(a, b) -> b - a) candidate
      main
      
getInts :: IO [Int]
getInts = map read . words <$> getLine

getChannel :: Channel -> IO [Channel]
getChannel (p, q) = do
  n <- readLn :: IO Int
  ns <- getInts
  let ns' = (p, p) : go ns ++ [(q, q)]
  return $ go' ns'
  where
    go :: [Int] -> [(Int, Int)]
    go [] = []
    go (a:b:z) = (a, b) : go z
    go' :: [(Int, Int)] -> [(Int, Int)]
    go' [x] = [x]
    go' (a:b:c) = (snd a, fst b) : (go' (b:c))

--tupleToMinutes = id
tupleToMinutes (a, b) = (toMinutes a, toMinutes b)

toMinutes :: Int -> Int
toMinutes n = (n `div` 100) * 60 + (n `mod` 100)

merge :: [[Channel]] -> [Channel]
merge = go . sort . concat
  where
    go :: [Channel] -> [Channel]
    go ls = let ls' = flatten (merge' ls) in
--              trace (show ls) $
              if ls' == ls then ls
              else go ls'
    merge' :: [Channel] -> [[Channel]]
    merge' = groupBy (\(a1, b1) (a2, _) ->
                    a1 <= a2 && a2 <= b1)
    flatten :: [[Channel]] -> [Channel]
    flatten = map (\ls -> (minimum (map fst ls),
                           maximum (map snd ls)))