#include <bits/stdc++.h>
using namespace std;
class Union_find {
 private:
  vector<int> par;
  vector<int> rank;
  int n;

 public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main(void) {
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  long long base_l = x;
  long long edge = n - x;
  ans = x;
  while (edge) {
    if (base_l / edge != 0) {
      ans += (int)(base_l / edge) * 2 * edge - edge;
    }
    if (base_l % edge != 0) {
      ans += edge;
    }
    long long tmp = edge;
    edge = base_l % edge;
    base_l = tmp;
  }
  cout << ans << endl;
  return 0;
}
