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