#include <bits/stdc++.h>
using namespace std;
long long gcd(long long n, long long x) {
  while (n != x) {
    if (n > x) {
      n = n - x;
    } else {
      x = x - n;
    }
  }
  return n;
}
int main(void) {
  long long n;
  cin >> n;
  long long x;
  cin >> x;
  long long val = gcd(n, x);
  cout << 3 * (n - val);
}
