#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x, sum, b, a, c;
  cin >> n >> x;
  sum = 0;
  if (n == x * 2)
    cout << x * 3 << endl;
  else {
    if (n > x * 2) {
      sum += n;
      b = n - x;
      a = x;
      while (1) {
        sum += a * (b / a) * 2;
        if (b % a == 0)
          break;
        else {
          c = a;
          a = b % a;
          b = c;
        }
      }
    } else if (n < x * 2) {
      sum += x;
      b = x;
      a = n - x;
      while (1) {
        sum += a * (b / a) * 2;
        if (b % a == 0)
          break;
        else {
          c = a;
          a = b % a;
          b = c;
        }
      }
    }
    cout << sum - a << endl;
  }
  return 0;
}
