#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 1;
long long rec(long long x, long long a) {
  if (x > a) swap(a, x);
  if (a % x == 0)
    return 2 * a - x;
  else
    return 2 * (a / x) * x + rec(a - x, x);
}
signed main() {
  long long n, x;
  cin >> n >> x;
  if (x > n - x)
    cout << n + rec(n - x, x);
  else
    cout << n + rec(x, n - x);
  return 0;
}
