#include <bits/stdc++.h>
using namespace std;
int solve(long long int a, long long int b, long long int sum) {
  if (a % b == 0) return sum + 2 * a - b;
  return solve(b, a % b, sum + 2 * b * (a / b));
}
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int a = max(n - x, x);
  long long int b = min(n - x, x);
  if (a == b)
    cout << 3 * x << endl;
  else
    cout << n + solve(a, b, 0) << endl;
  return 0;
}
