solve :: Int -> Int -> [Int] -> Int
solve ma _ [] = ma
solve ma mi (n:nx)
  | ma < n - mi = solve (n-mi) (mini mi n) nx
  | otherwise = solve ma (mini mi n) nx
  where mini mi n = if n < mi then n else mi

main = getContents >>= print . (\(a:b:x) -> solve (b-a) a (b:x)) . tail . map read . words