#include <bits/stdc++.h>
using namespace std;
unsigned long long inf = (1LL << 62);
long long mod = 1000000007;
pair<long long, long long> dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
long long N, X;
long long solve(int a, int b) {
  if (a % b == 0) {
    return 2 * b * ((a / b) - 1) + b;
  } else {
    return 2 * b * ((a / b)) + solve(b, a % b);
  }
}
int main() {
  cin >> N >> X;
  cout << N + solve(((long long)(N - X) > (long long)(X)) ? (N - X) : (X),
                    ((long long)(N - X) > (long long)(X)) ? (X) : (N - X))
       << endl;
  return 0;
}
