dfs a b
  | a' == 0 = b'
  | a' == 1 && b' == 1 = 2 * a + 1
  | otherwise = 2 * a + dfs a' b'
  where
    a' = min a (b - a)
    b' = max a (b - a)

solve (n : x : _) = a + b + dfs a b
  where
    a = min x (n - x)
    b = max x (n - x)

main = getLine >>= print . solve . map read . words