#include <bits/stdc++.h>
using namespace std;
long long n, x;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while ('0' > c || c > '9') {
    if (c == '-') f = -1;
    getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int solve(int a, int b) {
  if (a == 0) return -b;
  return b / a * 2 * a + solve(b % a, a);
}
int main() {
  n = read(), x = read();
  printf("%lld", solve(x, n - x) + n);
}
