#include <bits/stdc++.h>
using namespace std;
unsigned long long f(unsigned long long n, unsigned long long x) {
  if (x == 0 || n == 0) return 0;
  if (n < x) swap(n, x);
  if (n % x == 0) return x * (2 * (n / x) - 1);
  return x * 2 * (n / x) + f(n - x, x);
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  unsigned long long n, x;
  if (n == x) {
    cout << 0;
    return 0;
  }
  cin >> n >> x;
  cout << n + f(n - x, x) << "\n";
  return 0;
}
