#include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
long long mod(long long A, long long M) { return (A % M + M) % M; }
const long long INF = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";
template <class T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long divCeil(long long A, long long B) { return (A + (B - 1)) / B; }
long long myctoi(char C) { return C - 48; }
char myitoc(long long N) { return '0' + N; }
signed main() {
  long long N, X;
  cin >> N >> X;
  long long a = X, b = N - X;
  if (a > b) swap(a, b);
  long long ans = N;
  while (a * b > 0) {
    if (b % a == 0)
      ans += 2 * b - a;
    else
      ans += b / a * a * 2;
    b %= a;
    swap(a, b);
  }
  cout << ans << endl;
}
