#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
static const int dx[] = {0, 0, 1, -1};
static const int dy[] = {-1, 1, 0, 0};
int f(int a, int b) {
  if (a > b)
    return 2 * b + f(a - b, b);
  else if (a < b)
    return 2 * a + f(a, b - a);
  else
    return a;
}
int main() {
  int n, x;
  cin >> n >> x;
  int ans = n + f(n - x, x);
  cout << ans << endl;
  return 0;
}
