#include <bits/stdc++.h>
using ll = long long;
ll n, x;
int main() {
  scanf("%lld %lld", &n, &x);
  ll ans = 0;
  while (x > 0) {
    ans += x * 3;
    ll n1 = n - x;
    ll x1 = n - (n / n1) * n1;
    n = n1;
    x = x1;
  }
  printf("%lld", ans);
}
