#include <bits/stdc++.h>
using namespace std;
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
int n, x;
int func(int a, int b) {
  if (a == b)
    return a;
  else if (a < b)
    return 2 * a + func(a, b - a);
  else
    return 2 * b + func(b, a - b);
}
signed main() {
  cin >> n >> x;
  cout << n + func(n - x, x) << endl;
  return 0;
}
