#include <bits/stdc++.h>
using namespace std;
long long solve(long long x, long long y) {
  if (x % y == 0) {
    return 2 * x - y;
  }
  return y * 2 * (x / y) + solve(y, x % y);
}
signed main() {
  long long n, i, j, z;
  cin >> n >> z;
  if (2 * z == n) {
    cout << 3 * n / 2;
    return 0;
  }
  if (2 * z < n) {
    cout << n + 2 * z + solve(z, n - 2 * z);
  } else {
    cout << z + solve(z, n - z);
  }
}
