import Data.Maybe import Data.List import Control.Arrow import Control.Applicative import Control.Monad main :: IO () main = do n <- read <$> getLine c <- read <$> getLine getLine ss <- map read . words <$> getLine ts <- map read . words <$> getLine ys <- map read . words <$> getLine ms <- map read . words <$> getLine print $ fromMaybe (-1) $ solve n c ss ts ys ms solve :: Int -> Int -> [Int] -> [Int] -> [Int] -> [Int] -> Maybe Int solve n c ss ts ys ms = foldr search Nothing road where road = makeRoad n c ss ts ys ms 0 search :: Int -> Maybe Int -> Maybe Int search m Nothing = Just m search m (Just t) = if m < t then Just m else Just t makeRoad :: Int -> Int -> [Int] -> [Int] -> [Int] -> [Int] -> Int -> [Int] makeRoad n c ss ts ys ms cost = do to <- elemIndices n ts let from = ss !! to money = ys !! to time = ms !! to when (cost + money > c) mzero if from == 1 then return time else map (+ time) $ makeRoad from c ss ts ys ms (cost + money)