#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long int N, X;
  cin >> N >> X;
  if (X == 0 || X == N) {
    cout << N << endl;
    return 0;
  }
  long int ans = N;
  N = N - X;
  while (N % X != 0) {
    int next = N % X;
    ans += (N - next) * 2;
    N = X;
    X = next;
  }
  ans += (N * 2 - X);
  cout << ans << endl;
  return 0;
}
