#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long linf = 1LL << 62;
const int MAX = 510000;
long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
int main() {
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  if (n % x == 0)
    cout << x * (2 * (n / x) - 1) << endl;
  else {
    long long ans = n;
    n = n - x;
    while (1) {
      if (n % x == 0) {
        ans += (2 * n / x - 1) * x;
        break;
      } else {
        ans += n / x * x * 2;
        long long tmp = n;
        n = x;
        x = tmp % x;
      }
    }
    cout << ans << endl;
  }
}
