lengthList :: Integer -> [Integer] -> [Integer]
lengthList k [a1,a2] = [(a2-a1), a1+(k-a2)]
lengthList k xs = init (lengthList k (init xs)) ++ [(last xs) - last (init xs)] ++ [(head xs) + k - (last xs)]

minLength :: Integer -> [Integer] -> Integer
minLength k xs = k - (maximum $ lengthList k xs)

toInt :: String -> Integer
toInt x = read x :: Integer

main = do
    kn <- getLine
    aList <- getLine
    let k = head $ map toInt $ words kn
        aList' = map toInt $ words aList 
    print $ minLength k aList'