#include <bits/stdc++.h>
using namespace std;
int rd() {
  int sum = 0;
  char c = getchar();
  bool flag = true;
  while (c < '0' || c > '9') {
    if (c == '-') flag = false;
    c = getchar();
  }
  while (c >= '0' && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  if (flag)
    return sum;
  else
    return -sum;
}
long long n, x;
long long work(long long a, long long b) {
  if (b % a == 0) return b * 2 - a;
  long long now = b / a * a * 2;
  now += work(b % a, a);
  return now;
}
int main() {
  n = rd(), x = rd();
  printf("%lld\n", work(x, n - x) + n);
  return 0;
}
