import Control.Monad
import Control.Applicative
import Data.Array

solve ms max ns = filter iter ms
  where
    memo = listArray (0, max) $ map iter [0..max]

    iter 0 = True
    iter i = any id $ map (\n -> if i < n then False else memo ! (i - n)) ns

primes = 2:3:[x|i<-[1..], j<-[-1,1], let x = 6*i+j, isPrime x] where
  isPrime n = null [i|i<-takeWhile (\x -> x*x <= n) primes, rem n i == 0]

primesBRev max = reverse $ takeWhile (<= max) primes

v0202 max ns ps = let res = solve ps max ns in
               if res == [] then "NA"
                            else show $ head res

parse :: [String] -> [(Int, [Int])]
parse [] = []
parse (x:xs)
  | x == "0 0" = []
  | otherwise  = (max, map read (take n xs)) : parse (drop n xs)
  where
    [n, max] = map read $ words x

main = do
  inp <- lines <$> getContents
  let inps = parse inp
  mapM_ (\(max, costs) -> putStrLn $ v0202 max costs (primesBRev max)) inps