import Data.List
import Control.Monad

main = do
  d <- readLn
  cs <- concat . words <$> getContents
  let
    gs = (map length . group) (replicate 14 'x' ++ cs ++ replicate 14 'x')
    ans = do
      guard (length gs > 2)
      let
        ansh = let (_:h:_) = gs in h + d
        anst = let (_:t:_) = reverse gs in t + d
        ansi = map (\[x,y,z] -> if d>=y then x+y+z else max x z + d) <$> windows3 (init (tail gs))
      pure (maximum [ansh, anst, maybe 0 maximum ansi])
  print (maybe d id ans)

windows3 xs
 | length xs < 3 = Nothing
 | otherwise = Just [g | (i,g) <- zip [0..] (zipWith3 (\x y z -> [x,y,z]) xs (drop 1 xs) (drop 2 xs)), even i]