#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
ULL solve2(ULL a, ULL b, ULL c, ULL d, ULL e, ULL f) {
  if (a == 0 || f == 0) return 0;
  if (b != 0) return solve2(f, e, d, c, b, a);
  if (c >= a) {
    if (f > a) {
      ULL t = min(c, f) / a;
      return a * t * 2 + solve2(a - a * t, b, c, d, e, f - a * t);
    }
    return a + solve2(a, 0, f, e, d, c - a);
  }
  return c + solve2(c, 0, a, 0, c, d - f);
}
void solve() {
  ULL n, x;
  cin >> n >> x;
  ULL ans = x;
  ans += solve2(n - x, 0, n, 0, n - x, x);
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
