import Data.Maybe import Data.List import Control.Arrow import Control.Applicative 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 $ makeRoad n ss ts ys ms where search :: (Int, Int) -> Maybe Int -> Maybe Int search (y, m) Nothing = if y <= c then Just m else Nothing search (y, m) (Just t) = if y <= c && m < t then Just m else Just t makeRoad :: Int -> [Int] -> [Int] -> [Int] -> [Int] -> [(Int, Int)] makeRoad n ss ts ys ms = do to <- elemIndices n ts let from = ss !! to money = ys !! to time = ms !! to if from == 1 then return (money, time) else ((+ money) *** (+ time)) <$> makeRoad from ss ts ys ms