import Data.Array
import Control.Monad

readS :: String -> Int
readS s = read s::Int

zeroOrPlus :: Int -> Int
zeroOrPlus value
    | value < 0 = 0
    | otherwise = value

solveDP::[String] -> Int
solveDP [status, coins] = let
  [value, variety] = map readS (words status)
  coinsInt = map readS (words coins)
  matrix = array ((0,0),(value, variety)) $
       [((0,0),0)] ++
       [((x,0),500) | x <- [1..value]] ++
       [((0,y),0) | y <- [1..variety]] ++
       [((x,y), matrix!(x,y-1)) |  y <- [1..variety], x <- [1..min value (coinsInt!!(y-1) - 1)]] ++
       [((x,y), min (matrix!(x,y-1)) (matrix!(x-coinsInt!!(y-1),y) + 1)) | y <- [1..variety], x <- [zeroOrPlus (coinsInt!!(y-1))..value]]
  in last $ elems matrix

main = do
  [status, coins] <- replicateM 2 getLine
  print $ solveDP [status, coins]