#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, a, b, ans, sav;
  scanf("%lld %lld", &n, &x);
  ans = n;
  if (n % 2 == 1) {
    printf("%lld", (n - 1) * 3ll);
  } else {
    a = min(n - x, x);
    b = max(n - x, x);
    while (a >= 1) {
      if (b % a == 0) {
        ans += (b / a) * 2 * a;
        ans -= a;
        break;
      } else {
        ans += (b / a) * 2 * a;
        sav = a;
        a = b % a;
        b = sav;
      }
    }
    printf("%lld", ans);
    return 0;
  }
}
