#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long det(long long a, long long b) {
  if (a > b) swap(a, b);
  if (b % a == 0) return 2 * (b / a) - 1;
  return 2 * a * (b / a) + det(a, b - a * (b / a));
}
int main() {
  cin >> n >> x;
  if (2 * x == n) {
    cout << 3 * (n / 2) << "\n";
    return 0;
  }
  cout << n + det(x, n - x) << "\n";
  return 0;
}
