#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
LL n, x;
LL ans = 0;
int main() {
  cin >> n >> x;
  ans += n;
  bool update = true;
  while (update) {
    LL copyn = max(n - x, x);
    LL copyx = min(n - x, x);
    n = copyn;
    x = copyx;
    if (n % x == 0) break;
    ans += (n / x) * x * 2;
  }
  ans += (n / x) * x * 2 - x;
  cout << ans << endl;
}
