#include <bits/stdc++.h>
int main() {
  int N, X, result, lon, sho, div, per;
  scanf("%d %d", &N, &X);
  if (X * 2 < N) {
    result = N;
    lon = N - X;
    sho = X;
    while (1) {
      div = lon / sho;
      per = lon % sho;
      result += div * sho * 2;
      if (per == 0) {
        result -= sho;
        break;
      }
      lon = sho;
      sho = per;
    }
  }
  if (X * 2 == N) result = X * 3;
  if (X * 2 > N) {
    result = X;
    lon = X;
    sho = N - X;
    while (1) {
      div = lon / sho;
      per = lon % sho;
      result += div * sho * 2;
      if (per == 0) {
        result -= sho;
        break;
      }
      lon = sho;
      sho = per;
    }
  }
  printf("%d\n", result);
  return 0;
}
