#include <bits/stdc++.h>
using namespace std;
int bin[40], lg[1 << 21];
namespace mine {
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3fll;
long long qread() {
  long long ans = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) {
    num = -num;
    putchar('-');
  }
  if (num > 9) write(num / 10);
  putchar('0' + num % 10);
}
void writeln(int num) {
  write(num);
  puts("");
}
inline void chmax(int &x, const int y) { x = x > y ? x : y; }
inline void chmin(int &x, const int y) { x = x < y ? x : y; }
const int MAX_N = 2e5 + 10;
const int MOD = 1e9 + 7;
void add(int &x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  if (x < 0) x += MOD;
}
long long ans;
void T(long long x, long long y) {
  if (x > y) swap(x, y);
  int a = y / x, b = y % x;
  if (b == 0)
    ans += (a * 2 - 1) * x;
  else {
    ans += (a * 2) * x;
    T(x, b);
  }
}
void main() {
  long long n = qread(), x = qread();
  ans = n;
  T(x, n - x);
  write(ans);
}
};  // namespace mine
int main() {
  srand(time(0));
  bin[0] = 1;
  for (int i = 1; i <= 30; i++) bin[i] = bin[i - 1] << 1;
  lg[1] = 0;
  for (int i = 2; i < (1 << 21); i++) lg[i] = lg[i >> 1] + 1;
  mine::main();
}
