#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
void swap(int* a, int* b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}
int main() {
  int N, X;
  cin >> N >> X;
  int ans = N;
  int a = X, b = N - X;
  if (a < b) swap(&a, &b);
  while (b) {
    if (a % b == 0) {
      ans += ((2 * (a / b) - 1) * b);
      break;
    }
    ans += ((a / b) * 2) * b;
    a %= b;
    if (a < b) swap(&a, &b);
  }
  cout << ans << "\n";
  return 0;
}
