#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 * (n / x);
    ll n1 = n % x;
    if (n1 == 0) break;
    ll x1 = (n1 + x) % n1;
    n = n1;
    x = x1;
  }
  printf("%lld\n", ans);
}
