#include <bits/stdc++.h>
using namespace std;
long long solve(long long n, long long x, int p) {
  long long r = n % x;
  if (r == 0) {
    return (n / x) * x * 2 - (p % 2 + 1) * x;
  }
  return (n / x) * x * 2 - x + solve(x, n % x, p + 1);
}
int main() {
  long long n, x;
  cin >> n >> x;
  if (2 * x == n)
    cout << 3 * x << endl;
  else
    cout << n + x + solve(n - x, x, 1) << endl;
  return 0;
}
