#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 10000;
const long long int INF = 1000000010;
const int MAX = 100001;
int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  int r = a % b;
  while (r) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  int n, x;
  cin >> n >> x;
  cout << 3 * (n - gcd(n, x)) << endl;
  return 0;
}
