import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

getInts::IO[Int]
getInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

main = do
  [k,n] <- map read . words <$> getLine
  an <- getInts
  let closewiseLength = (last an - head an)
  let (x,y) = foldl (\(a,b) (x,y) -> if (x-y) >= (div k 2) then (x,y) else (a,b)) (-1,-1) $ zip (tail an) an
  print $ if x == -1 then closewiseLength else (k-x) + y