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