import sys
input = sys.stdin.readline
N, X = map(int, input().split())
def solve(n, x):
  #print(n, x)
  if n == 0:
    return 0
  if x == 1: return n * 3
  m = x
  y = n - n // x * x
  if y == 0: return n // x * x * 3
  return solve(m, y) + n // x * x * 3
print(solve(max(N - X, X), min(N - X, X)))