#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
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) {
  int N, X;
  cin >> N >> X;
  ll res = N;
  N = N - X;
  while (X != 0) {
    res += N / X * X * 2;
    int tmp = X;
    X = N % X;
    N = tmp;
  }
  res -= N;
  cout << res << endl;
}
