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