#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int ans = n;
  long long int a = min(x, n - x);
  long long int b = max(x, n - x);
  while (a != b) {
    ans += 2 * a;
    b -= a;
    if (a > b) {
      long long int tmp = a;
      a = b;
      b = tmp;
    }
  }
  ans += a;
  cout << ans << endl;
}
