#include <bits/stdc++.h>
using namespace std;
const int M = 3e5 + 5, LG = 26, SM = 600, inf = 1e9 + 1;
int mod = 95542721;
int n, x;
int calc(int a, int b) {
  if (a < b) return calc(b, a);
  if (a % b == 0) {
    return 2 * a - b;
  }
  return 2 * (a / b) * b + calc(b, a % b);
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> x;
  cout << calc(x, n - x) + n;
}
