//#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);
    if (__gcd(n, k) == 1ll)printf("%lld", 1 + (n - 1) + 1 + (2 * (n - 2)));
    else {
        k = min(k, __gcd(n, k));
        printf("%lld", k + (n - k) + k + (2 * k * (n - k * 2)));
    }
}