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