lengthList :: Integer -> [Integer] -> [Integer]
lengthList k [a2,a1] = [(a1+k-a2), (a2-a1)]
lengthList k (x:xs) = [(last xs) + k - x] ++ [x - (head xs)] ++ tail (lengthList k 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' = reverse $ map toInt $ words aList 
    print $ minLength k aList'