#include <bits/stdc++.h>
using namespace std;
long long dfs(long long n, long long x) {
  if (x <= 0) return n;
  return n + dfs(max(n - x, x), min(n - x, x));
}
int main() {
  long long n, x;
  cin >> n >> x;
  cout << dfs(n, x) << endl;
  return 0;
}
