#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2> p) {
  o << "(" << p.first << ":" << p.second << ")";
  return o;
}
int main() {
  int i, j, k, l;
  long long int n, x;
  cin >> n >> x;
  long long int len = n + x;
  long long int wid = n - x;
  long long int cut = x;
  while (cut != 0) {
    long long int u = wid / cut;
    len += (u + u - 1 - (wid % cut == 0)) * cut;
    long long int _w = wid;
    wid = cut;
    cut = _w % cut;
    len += cut;
  }
  cout << len << endl;
  return 0;
}
