#include <bits/stdc++.h>
using namespace std;
long long mod(long long a) {
  long long c = a % (long long)(1E9 + 7);
  if (c >= 0)
    return c;
  else
    return c + (long long)(1E9 + 7);
}
long long inf = (long long)1E12;
long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
const double EPS = 1E-8;
long long f(long long a, long long b) {
  if (a == b) return a;
  if (a > b) swap(a, b);
  return 2 * a + f(a, b - a);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, x;
  cin >> n >> x;
  long long ans = n + f(n - x, x);
  cout << ans << endl;
  return 0;
}
