#include <bits/stdc++.h>
using namespace std;
int f(int n, int x) {
  int sum = 2 * (n / x) * x;
  if (n % x == 0) {
    sum -= x;
    return sum;
  }
  int rem = n % x;
  sum += f(x, rem);
  return sum;
}
int main() {
  int n, i, j, x;
  cin >> n >> x;
  int ans = n + f(n - x, x);
  cout << ans;
}
