import Data.Char
import Data.List
import qualified Data.IntMap as M
import Data.Maybe

main = do
  s <- getLine
  print $ compute s

compute :: String -> Int
compute s = sum $ unfoldr step (s,M.empty)

step :: (String,M.IntMap Int) -> Maybe (Int,(String,M.IntMap Int))
step ("",_) = Nothing
step (d:ds,m) = Just (fromMaybe 0 $ m' M.!? 0, (ds, m'))
  where
    e = digitToInt d
    f (x,y) = ((10 * x + e) `mod` 2019, y)
    m' = M.fromListWith (+) $ (e,1) : map f (M.toList m)