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