#include <bits/stdc++.h>
using namespace std;
int n, ans;
long long a, b;
void solve() {
  ans = n;
  b = n - a;
  while (true) {
    if (a < b) swap(a, b);
    ans += ((a / b) * 2) * b;
    if (a % b == 0) {
      ans -= b;
      break;
    }
    a = a % b;
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> a;
  solve();
  return 0;
}
