#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long N = (long long)1e5 + 100;
const int M = (int)1e4 + 4;
const int INF = (int)0x3f3f3f3f;
long long n, x;
long long gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
int Solve() {
  std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> x;
  long long nn = n;
  n -= x;
  long long res = gcd(n, x);
  n /= res;
  x /= res;
  long long a = n / x;
  long long b = n % x;
  long long ans = (2 * a + 2) * x + 2 * b - 3;
  ans *= res;
  cout << ans + nn;
  return 0;
}
int main() {
  Solve();
  return 0;
}
