import Data.List

main = do
  [k, n] <- map read . words <$> getLine
  as <- sort . map read . words <$> getLine
  let diff = zipWith (-) (tail as ++ [(head as + k)]) as
  print . sum . init . sort $ diff
