#include <bits/stdc++.h>
using namespace std;
constexpr const int INT_INF = 0x3f3f3f3f;
constexpr const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
T LCM(T a, T b) {
  return a / __gcd(a, b) * b;
}
template <typename T>
T EXTGCD(T a, T b, T &x, T &y) {
  T d = a;
  if (b != 0) {
    d = EXTGCD(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}
template <typename T>
bool is_prime(T a) {
  for (int i = 2; i * i <= a; i++)
    if (a % i == 0) return 1;
  return 0;
}
const long long MOD = 1000000000 + 7;
template <typename T>
T mod_inverse(T a, T mod, bool prime) {
  if (prime) {
    T tmp = mod - 2, now = a, res = 1;
    while (tmp) {
      if (tmp & 1) res = ((res % MOD) * (now % MOD)) % MOD;
      now = ((now % MOD) * (now % MOD)) % MOD;
      tmp >>= 1;
    }
    return res;
  } else {
    T x, y;
    EXTGCD(a, mod, x, y);
    return (mod + x % mod) % mod;
  }
}
long long n, x;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  cin >> n >> x;
  long long len1 = x, len2 = n - x;
  long long ans = n;
  while (true) {
    cerr << "len1"
         << " " << len1 << endl;
    cerr << "len2"
         << " " << len2 << endl;
    if (len1 == 0 || len2 == 0) break;
    if (len1 > len2) {
      int tmp = len1 / len2;
      ans += 2L * tmp * len2;
      len1 %= len2;
    } else if (len1 < len2) {
      int tmp = len2 / len1;
      ans += 2L * tmp * len1;
      len2 %= len1;
    } else {
      ans += len1;
      break;
    }
  }
  cout << ans - len1 - len2 << endl;
  return 0;
}
