import Data.List
import Data.Maybe

main = do
  [n,k] <- map read.words <$> getLine
  as <- map read.words <$> getLine
  print $ solve n k as

solve :: Int -> Int -> [Int] -> Int
solve n k as = fromJust $ find eval ds where
  eval d = f $ zip ir dr where
    ir = scanl (+) 0 ms
    dr = scanr (+) 0 $ map (d-) ms
    ms = sort $ map (`mod` d) as
    f ((i,d):rest)
      | i == d = i <= k
      | otherwise = i < d && f rest
    f _ = False
  ds = reverse $ divisiors $ sum as

divisiors n = small ++ reverse large where
  (small, large) = f $ takeWhile ((<=n).(^2)) [1..]
  f (x:xs)
    | r == 0 && q /= x = (x:sm, q:la)
    | r == 0 = (x:sm, la)
    | otherwise = (sm, la)
    where
      (sm, la) = f xs
      (q, r) = divMod n x
  f _ = ([], [])