#include <bits/stdc++.h>
using namespace std;
int mod_gcd(int a, int b, int &l) {
  if (a % b != 0) {
    l = l + 2 * (a - (a % b));
    return mod_gcd(b, a % b, l);
  } else {
    l = l + b * (a / b + 1);
  }
}
int main(int argc, char *argv[]) {
  int n, x, l = 0;
  cin >> n >> x;
  mod_gcd(n - x, x, l);
  cout << l + n;
}
