import Control.Applicative import Control.Monad import Data.Array import Data.Maybe import qualified Data.Set as S import Debug.Trace rotates s = f s $ reverse s where n = length s f xs [] = [xs] f xs (y:ys) = xs:f (take n (y:xs)) ys main = do _ <- getLine a <- map read . words <$> getLine b <- map ((`div`2).read) . words <$> getLine let pq = foldl (flip S.insert) S.empty $ map (\i->(i,0)) a check m elevels =min m $ last . fst $ break (>m) $ map fst $ scanl f (0,pq) $ elevels f (mn,tpq) elevel = (max mn (cnt+1),S.insert (level+elevel,cnt+1) gotpq) where ((level,cnt),gotpq) = S.deleteFindMin tpq print $ foldl check (10^4) $ rotates b