#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, suma, menor, mayor;
  cin >> n >> x;
  suma = n;
  menor = min(n - x, x);
  mayor = max(n - x, x);
  while (mayor % menor) {
    suma += 2 * (mayor / menor) * menor;
    if ((mayor / menor) % 2 == 0 || mayor == menor) suma -= menor;
    int aux = menor;
    menor = mayor % menor;
    mayor = aux;
  }
  suma += 2 * (mayor / menor) * menor;
  suma -= menor;
  cout << suma << endl;
  system("pause");
  return 0;
}
