#include <bits/stdc++.h>
using namespace std;
int64_t N, X;
int main() {
  cin >> N >> X;
  int64_t ret = N;
  int64_t n = N - X, x = X;
  while (0 < n) {
    ret += (n / x) * 2 * x;
    n = x;
    x = n % x;
  }
  cout << ret << endl;
  return 0;
}
