#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  long long int N, X;
  long long int ans = 0;
  cin >> N >> X;
  ans += N;
  long long int a = X;
  long long int b = N - X;
  while (b % a != 0) {
    ans += (b / a) * a * 2;
    long long int tmp = b;
    b = a;
    a = tmp % a;
  }
  cout << ans + (b * 2) - 1 << endl;
}
