#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X, ans;
  cin >> N >> X;
  ans = X;
  while (true) {
    if (2 * X == N) {
      ans += N;
      break;
    }
    if (2 * X < N) {
      ans += N;
      N = N - X;
      X = N - X;
    } else {
      long long side = N - X, tmp1 = X / side, tmp2 = X % side;
      ans += tmp1 * 2 * side;
      N = N - tmp1 * side;
      X = X - tmp1 * side;
    }
  }
  cout << ans << endl;
  return 0;
}
