import Data.List

main :: IO ()
main = do
  [k, _] <- map read . words <$> getLine
  a <- map read . words <$> getLine
  let a1 = [x - head a | x <- a] ++ [k]
  let a2 = zipWith (-) (tail a1) a1
  print $ sum $ init $ sort a2

