#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long ans = 0;
int main() {
  cin >> n >> x;
  if (x * 2 >= n) x = n - x;
  ans = n;
  n -= x;
  while (x) {
    if (x == 1) ans--;
    ans += (2 * x * (n / x));
    long long temp = x;
    x = n % x;
    n = temp;
  }
  cout << ans;
}
