#include <bits/stdc++.h>
using namespace std;
long long int parallelogram(long long int, long long int);
long long int trapezium(long long int, long long int);
long long int parallelogram(long long int s1, long long int s2) {
  if (s1 == s2)
    return s1;
  else if (s1 < s2)
    return s1 + trapezium(s2 - s1, s1);
  else
    return s2 + trapezium(s1 - s2, s2);
}
long long int trapezium(long long int s1, long long int s2) {
  return s2 + parallelogram(s1, s2);
}
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int ans = x + trapezium(x, n - x);
  cout << ans << "\n";
  return 0;
}
