#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int64_t n, x;
  cin >> n >> x;
  int64_t ans = x;
  x = n - x;
  if (x > n) swap(x, n);
  int64_t a = n, b = x;
  int64_t cnt = 0;
  while (a != 0 && b != 0) {
    if (cnt % 2 == 0) {
      ans += b * (a / b);
      cnt += a / b - 1;
      a %= b;
    } else {
      if (a < b) {
        ans += a;
        swap(a, b);
      } else {
        ans += b;
      }
    }
    cnt++;
  }
  cout << ans << "\n";
  return 0;
}
