#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int read() {
  int s = 0, t = 1, k = getchar() - 48;
  for (; k < 0 || 9 < k; k = getchar() - 48)
    if (k == -3) t = -1;
  for (; 0 <= k && k <= 9; k = getchar() - 48) s = (s << 1) + (s << 3) + k;
  return s * t;
}
int n, A, x, y;
void in() {
  cin >> A >> x;
  y = A - x;
}
void wor() {
  while (x) {
    A += y / x * 2 * x;
    y %= x;
    swap(x, y);
  }
  A -= y;
}
void out() { cout << A; }
int main() {
  in();
  wor();
  out();
  exit(0);
}
