#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
long long solve() {
  long long ret = 0;
  long long N, X;
  cin >> N >> X;
  ret += N;
  long long a = X;
  long long b = N - X;
  while (true) {
    if (a == 0 || b == 0) break;
    if (a <= b) {
      ret += b / a * 2 * a;
      b = b - b / a * a;
    } else {
      ret += a / b * 2 * b;
      ret -= b;
      a = a - a / b * b;
    }
  }
  return ret;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << solve() << endl;
  return 0;
}
