#include <bits/stdc++.h>
using namespace std;
int gcd(int, int);
int main() {
  int N, X, sum;
  cin >> N;
  cin >> X;
  sum = 3 * (N - gcd(N, X));
  cout << sum;
  return 0;
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
