#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
using Hash = unordered_map<T, U>;
template <class T>
T read() {
  T a;
  cin >> a;
  return a;
}
template <class T>
void read(T& a) {
  cin >> a;
}
template <class T>
void rarr(T a, int n) {
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
}
template <class T>
void write(T a) {
  cout << a << endl;
}
template <class T>
void writes(vector<T> a, const char* c = " ") {
  cout << a[0];
  for (int i = 1; i < (int)a.size(); i++) cout << c << a[i];
  cout << endl;
  ;
}
void write(double a) { cout << fixed << setprecision(12) << a << endl; }
void write(long double a) { cout << fixed << setprecision(12) << a << endl; }
template <class T>
void warr(T a, int n, const char* c = " ") {
  cout << a[0];
  for (int i = 1; i < n; i++) cout << c << a[i];
  cout << endl;
}
void split(string s, string delim, vector<int>& result) {
  result.clear();
  string::size_type pos = 0;
  while (pos != string::npos) {
    string::size_type p = s.find(delim, pos);
    if (p == string::npos) {
      result.push_back(atoi(s.substr(pos).data()));
      break;
    } else {
      result.push_back(atoi(s.substr(pos, p - pos).data()));
    }
    pos = p + delim.size();
  }
}
void split(string s, string delim, vector<string>& result) {
  result.clear();
  string::size_type pos = 0;
  while (pos != string::npos) {
    string::size_type p = s.find(delim, pos);
    if (p == string::npos) {
      result.push_back(s.substr(pos));
      break;
    } else {
      result.push_back(s.substr(pos, p - pos));
    }
    pos = p + delim.size();
  }
}
long long comb(long long n, long long m) {
  long long p = 1;
  m = min(m, n - m);
  for (long long i = 1; i <= m; i++) {
    p *= n - i + 1;
    p /= i;
  }
  return p;
}
long long gcd(long long a, long long b, long long& res) {
  res = a + b;
  if (a < b) swap(a, b);
  while (true) {
    long long k = a % b;
    res += (a / b) * b * 2;
    if (k == 0) {
      res -= b;
      return b;
    }
    a = b;
    b = k;
  }
}
int main(void) {
  long long n, x;
  cin >> n >> x;
  int y = n - x;
  long long res;
  gcd(x, y, res);
  write(res);
  return 0;
}
