#include <bits/stdc++.h>
int main() {
  long long n, k;
  scanf("%lld %lld", &n, &k);
  long long r = n % k;
  long long res = n;
  while (r > 0) {
    res += (n / k - 1) * k;
    n = k;
    k = r;
    r = n % k;
  }
  res += (k - 1) * 2 + 1;
  printf("%lld", res);
}
