from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10


def main():
    N, X = map(int, input().split())
    print(3 * (N - gcd(N, X)))


if __name__ == '__main__':
    main()
