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

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