#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000009;
long long L(long long a, long long b) {
  if (a < b) {
    if (b % a == 0)
      return a * (2 * (b / a) - 1);
    else
      return 2 * a * (b / a) + L(a, b % a);
  } else {
    return L(b, a);
  }
}
signed main() {
  long long n, x;
  cin >> n >> x;
  cout << n + L(x, n - x) << endl;
  return 0;
}
