main = do
    [k,n] <- map read . words <$> getLine :: IO [Int]
    an <- map read . words <$> getLine :: IO [Int]
    let an' = zip (zipWith (-) ((tail an) ++ [k + (head an)]) an) [1..n]
    let m = maximum $ an'
    print $ foldl (\acc (x,y) -> acc+x) 0 $ filter (/=m) an'
