import sys

sys.setrecursionlimit(1000)

def f(x, y):

  if y < x:
    (y, x) = (x, y)

  if x == 0:
    return y

  return x + y + f(x, y - x)

n, x = map(int, input().split())

print(x + f(x, n - x) - 1)

