#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
int64 solve(int64 p, int64 q) {
  int64 mx = max(p, q);
  int64 mn = min(p, q);
  if (mx != mn) {
    return solve(mn, mx - mn) + 2 * mn;
  } else {
    return mn;
  }
}
signed main() {
  int64 n, x;
  cin >> n >> x;
  int64 ans = x + n - x + solve(x, n - x);
  cout << ans << endl;
}
