
main :: IO ()
main = do
    [k,_] <- (map read .words) <$> getLine
    as <- (map read .words) <$> getLine
    putStrLn . show $ getAns k as

getAns :: Int -> [Int] -> Int
getAns k as =
    let
        maxMap = getMax as k
    in
    maximum $ map maxMap as


getMax :: [Int] -> Int -> Int -> Int
getMax [] _ _ = 0
getMax (a:as) k x = 
    let
        d1 = ((a - x) + k) `mod` k
        d2 = ((x - a) + k) `mod` k
    in
    max (min d1 d2) $ getMax as k x
    
