#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, sum;
  cin >> n >> x;
  sum = n;
  while ((n - x) % x != 0) {
    sum += 2 * x * (n - x) / x;
    x = (n - x) % x;
    n = x;
  }
  sum += 2 * x * ((n - x) / x) + x;
  cout << sum;
}
