#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000ll;
const long long MOD = 1000000007ll;
const double EPS = 1e-8;
long long f(long long a, long long b);
int main(void) {
  long long int n;
  long long int x;
  cin >> n >> x;
  cout << n + f(n - x, x) << endl;
  return 0;
}
long long f(long long a, long long b) {
  if (a == 1 && b == 1) return 1;
  if (a > b) {
    long long t;
    t = b;
    b = a;
    a = t;
  }
  return 2 * a + f(a, b - a);
}
