#include <bits/stdc++.h>
using namespace std;
using llong = long long;
llong length(llong L, llong X) {
  if (L == X) {
    return L;
  } else if (L < X) {
    return 2 * L + length(L, X - L);
  } else {
    return 2 * X + length(L - X, X);
  }
}
int main() {
  llong N, X;
  cin >> N >> X;
  cout << N + length(N - X, X) << endl;
}
