#include <bits/stdc++.h>
using namespace std;
int main() {
  long N, X;
  cin >> N >> X;
  long L = N;
  int a = N - X, b = X;
  while (a != 0 && b != 0) {
    if (a == b) {
      L += a;
      break;
    }
    if (a < b) {
      long n = b / a;
      long r = b % a;
      if (r == 0) {
        L += (n * 2 - 1) * a;
        break;
      }
      L += n * 2 * a;
      b = r;
    } else {
      long n = a / b;
      long r = a % b;
      if (r == 0) {
        L += (n * 2 - 1) * b;
        break;
      }
      L += n * 2 * b;
      a = r;
    }
  }
  cout << L;
  return 0;
}
