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