#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
long long solve(long long a, long long b) {
  if (a % b == 0) return 2 * (a / b) * b - 1;
  if (a < b) swap(a, b);
  return 2 * (a / b) * b + solve(b, a % b);
}
int main() {
  long long n, x;
  read(n);
  read(x);
  printf("%lld\n", n + solve(n - x, x));
  return 0;
}
