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

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