#include <bits/stdc++.h>
using namespace std;
long long f(long long n, long long res) {
  if (n < res) {
    long long alt = res;
    res = n;
    n = alt;
  }
  if (n % res == 0)
    return n / res * 2 - 1;
  else
    return (n / res - 1) * 2 + 2 + f(res, n % res);
}
int main() {
  long long n, x, sum = 0;
  cin >> n >> x;
  if (n == 2 * x)
    cout << 3 * x << endl;
  else {
    cout << n + f(n, n - x) << endl;
  }
  return 0;
}
