#include <bits/stdc++.h>
using namespace std;
long long dfs(long long n, long long x) {
  if (n % x == 0)
    return n + x;
  else
    return n / x * 2 * x + dfs(x, n - n / x * x);
}
int main() {
  long long n, x;
  while (~scanf("%d%d", &n, &x)) {
    printf("%lld\n", dfs(n - x, x) + n);
  }
}
