#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long MOD = pow(10, 9) + 7;
const long long INF = (1LL << 62);
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const int ddx[8] = {1, 0, -1, -1, -1, 0, 1, 1};
const int ddy[8] = {1, 1, 1, 0, -1, -1, -1, 0};
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
}
long long ans = 0;
void bfs(long long a, long long b) {
  if (a % b == 0) {
    ans += (a / b) * b;
    return;
  }
  ans += (a / b) * b;
  bfs(b, a % b);
  return;
}
int main() {
  init();
  long long N, X;
  cin >> N >> X;
  bfs(N, N - X);
  cout << ans + N << endl;
}
