main = do
    [k, _] <- map readAsInt . words <$> getLine
    aList <- map readAsInt . words <$> getLine
    print $ solve k aList

readAsInt :: String -> Int
readAsInt = read

solve :: Int -> [Int] -> Int
solve k aList = 
    let distances = fst $ foldl (\(ds, lastPos) ai -> ((ai - lastPos) : ds, ai ))
                                ([], last aList - k)
                                aList
    in k - maximum distances