value = input()
values = value.split()
     
N = int( values[0] )
X = int( values[1] )
     
shorter = min([X,N-X])
longer = max([X,N-X])
     
length = 0

while shorter>0 and longer >0 :
 
    length = int(longer/shorter)*3*shorter
     
    sho = longer/shorter
    amari = longer%shorter

    longer = shorter
    shorter = amari
    
     
print(length)