#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, X, ans, ox;
  cin >> N;
  cin >> X;
  if (N >= 2 * X) {
    ans = N;
    N = N - X;
  } else {
    ans = N;
    ox = X;
    X = N - X;
    N = ox;
  }
  while (1) {
    if (N % X == 0) {
      ans += X * (2 * N / X - 1);
      break;
    } else {
      ans += X * (2 * N / X);
      X = N % X;
      N = X;
    }
  }
  cout << ans << endl;
  return 0;
}
