main = getLine >>= print . yamanote . map read . words

yamanote [a,b,c] = circular 0 True
 where
  circular m False
   | mod (m+b) 60 == 0 = -1
   | otherwise = circular (m+b) True
  circular m True
   | (mod m 60) <= c && c <= (mod m 60) + a = 60 * (div m 60) + c
   | otherwise = circular (m+a) False