#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N;
  cin >> N;
  long long X;
  cin >> X;
  long long sum = N;
  N -= X;
  long long a(N / X);
  long long r(N % X);
  sum += 2 * a * X;
  while (r != 0) {
    a = N / X;
    r = N % X;
    sum += 2 * a * X;
    if (r == 0) sum -= X;
    N = a;
    X = r;
  }
  cout << sum << endl;
  return 0;
}
