#include <bits/stdc++.h>
using namespace std;
int64_t mod = 1e9 + 7;
int64_t gcd(int64_t n, int64_t m) {
  int64_t a = max(n, m);
  int64_t b = min(n, m);
  return a % b == 0 ? b : gcd(b, a % b);
}
int main() {
  int64_t n, x;
  cin >> n >> x;
  if (n % x == 0) {
    cout << ((n - x) * 3LL) << '\n';
    return 0;
  }
  int64_t res = x;
  int64_t tmp = n - x;
  n = x;
  x = tmp;
  res += x + (n / x) * (2LL * x);
  while (true) {
    tmp = n % x;
    n = x;
    x = tmp;
    if (n % x == 0) {
      res += (n / x) * (2LL * x) - x;
      break;
    } else {
      res += (n / x) * (2LL * x);
    }
  }
  cout << (res) << '\n';
  return 0;
}
