let rec solve n x =
  if n = x then x
  else if n < x then solve x n
  else 2 * x + solve (n - x) x

let () = Scanf.scanf "%d %d" (fun n x ->
  Printf.printf "%d\n"
    (if 2 * x <= n then n + solve (n - x) x
     else n + solve x (n - x)))
