#include <bits/stdc++.h>
using namespace std;
signed main() {
  int N, X;
  cin >> N >> X;
  int ans = 0;
  int a = N, b = max(X, N - X);
  while (1) {
    int tmp = b;
    if (a != N) ans += 3 * (a / b) * b;
    b = a % b;
    a = tmp;
    if (b == 0) break;
  }
  cout << ans << endl;
  return 0;
}
