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