#include <bits/stdc++.h>
using namespace std;
const long long maxn = 10001;
long long a[maxn], b[maxn], n, ans, x;
signed main() {
  cin >> n >> x;
  ans += x, x = n - x;
  while (x) {
    ans += (n / x) * x;
    long long tmp = x;
    x = n % x;
    n = tmp;
    ans += x;
  }
  cout << ans;
}
