#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1000000007;
long long daikei(long long n, long long x) {
  if (2 * x == n) {
    return n;
  } else if (2 * x >= n) {
    return n + daikei(x, 2 * x - n);
  } else {
    return n + daikei(n - x, n - 2 * x);
  }
}
signed main() {
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  ;
  long long X;
  cin >> X;
  ;
  cout << X + daikei(N, X) << endl;
  return 0;
}
