#include <bits/stdc++.h>
using namespace std;
template <class T>
T IN() {
  T d;
  cin >> d;
  return d;
}
long long gcd(long long a, long long b) { return (b > 0) ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool uruu(long long Y) {
  return (((Y % 4 == 0 && Y % 100 != 0) || Y % 400 == 0) ? true : false);
}
int solve(long long yoko, long long tate) { return 0; }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ;
  bool dbgF = true;
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  ans = x + (n - x);
  long long tate = x;
  long long yoko = n - x;
  while (1) {
    if (tate > yoko) {
      swap(tate, yoko);
    }
    if (yoko % tate != 0) {
      ans += floor(yoko / tate) * tate + tate;
      yoko = yoko % tate;
    } else {
      ans += (yoko / tate - 1) * 2 * tate + tate;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
