#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline int getmin(Tp &x, Tp y) {
  return y < x ? x = y, 1 : 0;
}
template <typename Tp>
inline int getmax(Tp &x, Tp y) {
  return y > x ? x = y, 1 : 0;
}
template <typename Tp>
inline void read(Tp &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') f = 1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  if (f) x = -x;
}
long long n, x, d;
long long calc(int n, int x) {
  if (!x) return 0ll;
  return calc(x, n % x) + n / x * 2ll * x - n % x;
}
int main() {
  read(n);
  read(x);
  if (n % x == 0) {
    printf("%lld\n", x * 3 * (n / x - 1));
    return 0;
  }
  printf("%lld\n", n + calc(n - x, x));
  return 0;
}
