#include <bits/stdc++.h>
using namespace std;
auto chmax = [](int &a, int b) { a = max(a, b); };
auto chmin = [](int &a, int b) { a = min(a, b); };
const int INF = 100000000;
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
bool isDebugMode = 1;
long long n;
long long x;
void swap(long long &a, long long &b) {
  long long tmp = a;
  a = b;
  b = tmp;
}
long long f(long long a, long long b) {
  if (a == b) return a;
  if (a > b) swap(a, b);
  return 2 * a + f(b - a, a);
}
void solve() { cout << n + f(n - x, x) << endl; }
int main() {
  cin >> n >> x;
  solve();
  return 0;
}
