#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) { return a > b; }
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
  return a * (b / gcd(a, b));
}
double getTime() {
  timeval tv;
  gettimeofday(&tv, 0);
  return tv.tv_sec + tv.tv_usec * 1e-6;
}
long long n, x, ans;
vector<int> vv;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> x;
  int a = n - x, b = x;
  if (x <= n / 2) {
    a = n - x;
    b = x;
  } else {
    a = x;
    b = n - x;
  }
  ans = n;
  while (1) {
    if (a % b == 0) {
      ans += 2LL * a - b;
      break;
    } else {
      ans += 2LL * (a / b) * b;
      long long ta, tb;
      ta = b, tb = a % b;
      a = ta, b = tb;
    }
  }
  cout << ans << endl;
  return 0;
}
