#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  long long n;
  long long x;
  long long ans = 0;
  cin >> n >> x;
  ans += x;
  long long now = n - x;
  long long tei = n;
  if (n - x == x) {
    cout << now * 3 << endl;
    return 0;
  }
  int c = 1;
  while (1) {
    cerr << ans << endl;
    long long cnt = tei / now;
    long long tmp = now;
    if (tei % now == 0) break;
    if (c & 1)
      ans += (cnt * 2 - 1) * now;
    else
      ans += (cnt * 2) * now;
    now = tei - tmp * cnt;
    tei = tmp;
    c++;
  }
  ans += (tei / now * 2 - 1) * now;
  cout << ans << endl;
  return 0;
}
