import sys
input = sys.stdin.readline

def main():
    N, X = map(int, input().split())
    ans = N
    min_ = min(X, N-X)
    max_ = max(X, N-X)
    while min_ != 0:
        # print(max_, min_)
        ans += (max_ // min_) * 2 * min_
        tmp = max_ % min_
        max_ = min_
        min_ = tmp
    ans -= max_
    print(ans)


if __name__ == "__main__":
    main()