#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
int64 f(int64 x, int64 y) {
  if (x < y) {
    int64 mx = max(x, y - x), mn = min(y - x, x);
    return f(mn, mx) + x * 2;
  }
  return x;
}
signed main() {
  int64 n, x;
  cin >> n >> x;
  cout << n + f(x, n - x) << endl;
  return 0;
}
