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