#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  scanf("%lld %lld", &n, &x);
  long long ans = n;
  n -= x;
  while (true) {
    if (n % x == 0) {
      ans += 2 * x * (n / x) - x;
      break;
    } else {
      ans += 2 * x * (n / x);
    }
    int temp = n;
    n = x;
    x = temp % x;
    if (n < x) {
      swap(n, x);
    }
  }
  cout << ans << endl;
  return 0;
}
