import Control.Monad

solve k l ak bk
 | (null ak) || (null bk) = [0]
 | (head ak + head bk) <= k = l : solve k (l+1) ak (tail bk)
 | otherwise = solve k (l-1) (tail ak) bk

main = do
 k <- read . last . words <$> getLine
 [ak,bk] <- replicateM 2 $ takeWhile (<= k) . scanl (+) 0 . map read . words <$> getLine :: IO [[Int]]
 print $ max (maximum $ solve k (length ak - 1) (reverse ak) bk) (maximum $ solve k (length bk - 1) (reverse bk) ak)