from fractions import*

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

print((3*(n-gcd(n,x))))
import math

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

print((3*(n-math.gcd(n,x))))
from fractions import *

n, x = list(map(int, input().split()))
print((3 * (n - gcd(n, x))))

import math

n, x = list(map(int, input().split()))
print((3 * (n - math.gcd(n, x))))

-from fractions import * +import math -print((3 * (n - gcd(n, x)))) +print((3 * (n - math.gcd(n, x))))