let rec solve n x =
  if n = x then x
  else 
    let y = (n - 1) / x in
    2 * x * y + solve x (n - x * y)

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)))
