#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
int main() {
  long long n, x;
  cin >> n >> x;
  long long r = x;
  while (n) {
    r += x / (n - x) * 2;
    if (x % (n - x) == 0) break;
    r += n - x + x % (n - x);
    long long nn = n - x, nx = n - x - x % (n - x);
    n = nn, x = nx;
  }
  cout << r << endl;
  return 0;
}
