#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long f(long long n, long long x) {
  long long ret = n;
  if (n % x == 0) {
    return ((n / x) * 2 + 1) * x;
  }
  while (n - x >= 0) {
    ret += x;
    n -= x;
  }
  if (n != 0) {
    ret += n;
    ret += f(n, x - n);
  }
  return ret;
}
void solve() {
  scanf("%lld %lld", &n, &x);
  if (n % x == 0) {
    printf("%lld\n", n + x);
    return;
  }
  printf("%lld\n", (n - x) + f(x, n - x));
}
int main() {
  int tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
