#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,fast-math,unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <class T>
istream &operator>>(istream &in, vector<T> &a) {
  for (auto &i : a) in >> i;
  return in;
}
template <class T>
ostream &operator<<(ostream &out, vector<T> a) {
  for (auto &i : a) out << i << ' ';
  return out;
}
template <class T, class U>
istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T, class U>
ostream &operator<<(ostream &out, pair<T, U> p) {
  out << p.first << " " << p.second << " ";
  return out;
}
inline void Start() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
signed main() {
  Start();
  ll ans = 0;
  ll a, b;
  cin >> a >> b;
  ans += a;
  a = a - b;
  if (a < b) swap(a, b);
  while (b != 0) {
    ll cur = a / b;
    ans += cur * (b + b);
    a %= b;
    swap(a, b);
  }
  cout << ans - 1;
  return 0;
}
