#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 123;
const int inf = 1e9 + 123;
const long long INF = 1e18;
const double eps = 1e-5;
long long n, x, ans;
void rec(int a, int b) {
  if (b % a == 0) {
    ans += b / a * (2 * a) - a;
    return;
  } else {
    ans += b / a * (2 * a);
    rec(b % a, a);
  }
}
int main() {
  cin >> n >> x;
  rec(x, n - x);
  cout << ans + n;
}
