#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const long long int LINF = 1 << 30;
template <typename T>
void printv(vector<T> const& s) {
  for (int i = (0); i < (((int)(s).size())); i++) {
    cout << s[i] << " ";
  }
  cout << endl;
}
int main() {
  cin.tie(0);
  cout << setprecision(10);
  long long int n, x;
  cin >> n >> x;
  long long int ans = n;
  n = n - x;
  while (n > 0 && x > 0) {
    if (n > x) {
      ans += x * 2 * (n / x);
      n = n % x;
    } else if (n < x) {
      ans += n * 2 * (x / n);
      x = x % n;
    } else {
      ans += n;
      break;
    }
  }
  ans -= n + x;
  cout << ans << endl;
}
