#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return (b, a % b);
}
uint64_t solve(uint64_t n, uint64_t x) { return 3 * (n - gcd(n, x)); }
int main() {
  ios::sync_with_stdio(false);
  uint64_t n, x;
  cin >> n >> x;
  cout << solve(n, x) << '\n';
}
