import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Vector as V

main :: IO ()
main = do
  s <- map (subtract (fromEnum 'a') . fromEnum) <$> getLine :: IO [Int]
  let
    l = length s :: Int
    sSets = V.accum (flip IS.insert) (V.replicate 26 IS.empty) . flip zip [0..] $ s :: V.Vector IntSet
    f :: [Int] -> Maybe (Int, Maybe Int)
    f = foldl step Nothing
      where
        step :: Maybe (Int, Maybe Int) -> Int -> Maybe (Int, Maybe Int)
        step Nothing x = Just (0, IS.lookupGE 0 (sSets V.! x))
        step (Just (i, Just j)) x | Nothing <- m = Just (i + 1, IS.lookupGE 0 (sSets V.! x))
                                  | otherwise    = Just (i, m)
          where
            m = IS.lookupGT j (sSets V.! x)
        step _ _ = Just (0, Nothing)
    g :: Maybe (Int, Maybe Int) -> Int
    g (Just (i, Just j)) = l * i + j + 1
    g _                  = -1
  t <- map (subtract (fromEnum 'a') . fromEnum) <$> getLine :: IO [Int]
  print . g . f $ t
