import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))

a = K
b = N - K
c = 0
d = min(a, b)
while not a == b == c:
    a, b, c = sorted([a, b, c])
    if a != b:
        d = b - a
    cnt = (c - b) // d
    b += d * cnt
    a += d * cnt + d
print(a + b + c)
