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