#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, N, X, sol = 0, c;
  cin >> N >> X;
  a = N - X;
  b = X;
  sol = a + b;
  c = a % b;
  while (c != 0) {
    sol += 2 * (a / b) * b;
    a = a % b;
    swap(a, b);
    c = a % b;
  }
  sol += 2 * (a / b) * b - b;
  cout << sol << endl;
  return 0;
}
