#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a, sum = 0;
  cin >> n >> a;
  long long p = min(a, n - a), q = max(a, n - a);
  sum += q;
  bool flag = false;
  while (q % p != 0) {
    sum += 2 * p * (q / p);
    long long a = q % p, b = p;
    p = min(a, b), q = max(a, b);
    sum += q;
    flag = true;
  }
  sum += 2 * p * (q / p);
  if (flag) sum -= p;
  cout << sum << endl;
}
