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