//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;


int main() {
    // freopen("in.txt", "r", stdin);
    long long n, k;
    scanf("%lld %lld", &n, &k);
    k = __gcd(n, k);
    printf("%lld", k + (n - k) + k + (2 * k * (n - k * 2)));

}
