let solve n x =
  let rec aux acc n x =
    if x = 0 then acc - n
    else aux (2*(n/x*x) + acc) x (n mod x) in
  if n > x+x then aux n (n-x) x else aux n x (n-x)

let () = print_int (Scanf.scanf "%d %d" solve)