#include <bits/stdc++.h>
using namespace std;
const long long ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const double eps = 1e-7;
const int N = 2e6 + 6;
long long n, x;
int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> x;
  long long ans = n;
  long long cur1 = x, cur2 = n - x;
  while (1) {
    ans += cur2 / cur1 * cur1 * 2;
    cerr << cur2 / cur1 * cur1 * 2 << "\n";
    if (cur1 <= 1) break;
    int q = cur2;
    cur2 = cur1;
    cur1 = q % cur1;
  }
  cout << ans - 1 << "\n";
  return 0;
}
