#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, X, tmp, result;
  cin >> N >> X;
  result += N;
  N = N - X;
  while (X > 0) {
    result += 2 * X * (N / X);
    tmp = N % X;
    N = X;
    X = tmp;
  }
  cout << result - N << endl;
}
