import           Data.List
import           Data.Maybe

main :: IO ()
main = do
    [n, k] <- getLine >>= return . map read . words
    ps <- getLine >>= return . map ((subtract 1) . read) . words
    cs <- getLine >>= return . map read . words
    let ans = solve n k ps cs
    print ans

solve :: Int -> Int -> [Int] -> [Int] -> Int
solve n k ps cs
  | total > 0 && m > 0 = total * (k `div` totallen) + rest m loopl
  | total > 0 = total * (k `div` totallen)
  | otherwise = rest (if m > 0 then k else 1) loopl
    where
        loopl = loop n ps cs
        loopm = sortBy (\(x, _) (y, _) -> x `compare` y)
                $ map (\xs -> (sum xs, length xs)) loopl
        (total, totallen) = loopm!!0
        m = k `mod` totallen

loop :: Int -> [Int] -> [Int] -> [[Int]]
loop n ps cs = f 0 [] $ replicate n True
    where
        f c ret cl
          | cl!!c = f (ps!!c) ([cs!!c]:ret) ((take c cl) ++ False:(drop (c+1) cl))
          | otherwise = if or cl
               then f (ps!!(fromJust $ findIndex id cl)) ([]:ret) ((take c cl)
                        ++ False:(drop (c+1) cl))
               else splitOf [] ret

rest :: Int -> [[Int]] -> Int
rest k ls = maximum . map sum . concat $ map (subsequence k) ls

splitOf :: (Eq a) => [a] -> [[a]] -> [[a]]
splitOf n xs = foldr (\x as@(a:acc) -> if x == n then ([]:as) else ((x++a):acc)) [[]] xs

subsequence :: Int -> [Int] -> [[Int]]
subsequence n xs = concat
    $ map (\x -> tail . inits . take n . drop x $ cycle xs) [0..(length xs)]
