#include <bits/stdc++.h>
using namespace std;
int sum = 0;
int main() {
  int N, X;
  cin >> N >> X;
  int d, n;
  sum += X;
  sum += N - X;
  d = X;
  n = N - X;
  while (1) {
    if (n % d == 0) {
      sum += 2 * n - d;
      break;
    } else {
      sum += 2 * (n / d) * d;
      int tmp = d;
      d = n % d;
      n = tmp;
    }
  }
  cout << sum << endl;
}
