#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, X;
  cin >> N >> X;
  long long int a = X, b = N - X, ans = N;
  while (true) {
    long long int m = b / a;
    ans += 2 * m * a;
    long long int a_next, b_next;
    a_next = b - m * a;
    b_next = a;
    a = a_next;
    b_next = b;
    if (a == 0) {
      ans -= b;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
