#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int large = max(n - x, x);
  long long int small = min(n - x, x);
  long long int t = n;
  while (large != small) {
    long long int v = large / small;
    if (large % small == 0) {
      t += v * small + small;
      cout << t << endl;
      return 0;
    }
    t += v * small * 2;
    long long int tmp = large - small * v;
    large = small;
    small = tmp;
  }
  cout << t + small << endl;
  return 0;
}
