#include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
void solve(long long N, long long X) {
  LL ans = N;
  LL a = X, b = N - X;
  if (a > b) swap(a, b);
  while (a != b && a != 0) {
    ;
    ans += 2 * a * (b / a);
    b %= a;
    if (a > b) swap(a, b);
  }
  cout << ans - b << endl;
}
int main() {
  long long N;
  long long X;
  scanf("%lld", &N);
  scanf("%lld", &X);
  solve(N, X);
  return 0;
}
