#include <bits/stdc++.h>
using namespace std;
int main() {
  double n, x;
  while (cin >> n >> x) {
    double len, ans, xx;
    ans = n;
    len = n - x;
    xx = x;
    while (1) {
      if (len - (long long)(len / xx) * xx == 0.0) {
        ans += (len / xx * 2 - 1) * xx;
        break;
      } else {
        ans += ((long long)(len / x) * 2) * xx;
        long long t = xx;
        xx = len - (long long)(len / xx) * xx;
        len = t;
      }
    }
    printf("%.0f\n", ans);
  }
}
