#include <bits/stdc++.h>
using namespace std;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i;
  }
}
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  if (k == 0) return 1;
  return n * COM(n - 1, k - 1) / k;
}
int main() {
  COMinit();
  long long int i, j, k, l, m, n, sum, flag, cc, N, Ma, Mb, X, A, B;
  sum = 0;
  flag = 0;
  cin >> N >> X;
  long long int temp, ans;
  ans = 0;
  if (2 * X == N) {
    ans = N * 3 / 2;
  }
  if (X > N / 2) {
    X = N - X;
  }
  N = N - X;
  ans += N;
  while (N % X != 0) {
    temp = X;
    ans += X * 2 * N / X;
    X = N % X;
    N = temp;
  }
  ans += X * 2 * N / X;
  ans -= X;
  cout << ans << endl;
  ;
  return 0;
}
