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