#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (b) {
    int m = a % b;
    a = b;
    b = m;
  }
  return a;
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int isPrim(int a) {
  if (a == 1) {
    return 0;
  }
  for (int i = 2; i <= (a + 1) / 2; i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  return 1;
}
long long mod_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      ret = (ret * x) % mod;
    }
    x = (x * x) % mod;
    n = n / 2;
  }
  return ret;
}
struct VV {
  int x;
  int y;
  int z;
};
class LessInt {
 public:
  bool operator()(const VV& riLeft, const VV& riRight) const {
    if (riLeft.x == riRight.x) {
      return riLeft.y > riRight.y;
    }
    return riLeft.x > riRight.x;
  }
};
class indInt {
 public:
  bool operator()(const VV& riLeft, const VV& riRight) const {
    return riLeft.x < riRight.x;
  }
};
struct XX {
  int a;
  int b;
};
class xxIntu {
 public:
  bool operator()(const XX& riLeft, const XX& riRight) const {
    if (riLeft.a != riRight.a) {
      return riLeft.a > riRight.a;
    }
    return riLeft.b > riRight.b;
  }
};
bool operator<(const XX& riLeft, const XX& riRight) {
  return riLeft.a < riRight.a;
};
bool operator>(const XX& riLeft, const XX& riRight) {
  return riLeft.a > riRight.a;
};
int f(int n, int r) {
  int si = 1;
  int bo = 1;
  for (int i = 0; i < r; i++) {
    si = si * (si - i);
  }
  for (int i = 0; i < r; i++) {
    bo = bo * (bo - i);
  }
  return si / bo;
}
int main(int argc, const char* argv[]) {
  long long N, X;
  cin >> N >> X;
  long long ans = 0;
  long long shou;
  long long amari = 1;
  lldiv_t l;
  if (N - (2 * X) > 0) {
    ans = ans + (X + N);
    long long a = N - 2 * X;
    while (amari != 0) {
      l = lldiv(X, a);
      shou = l.quot;
      amari = l.rem;
      ans = ans + (X + 2 * (shou - 1) * a + a + amari);
      X = amari;
      a = a - X;
    }
  } else if (N - (2 * X) == 0) {
    ans = ans + 3 * X;
  } else {
    l = lldiv(N, N - X);
    shou = l.quot;
    amari = l.rem;
    if (amari == 0) {
      ans = ans + (X + 2 * (shou - 1) * (N - X));
    } else {
      ans = ans + (X + 2 * (shou - 2) * (N - X) + (N - X) + amari);
    }
    long long a = N - X - amari;
    X = amari;
    while (amari != 0) {
      l = lldiv(X, a);
      shou = l.quot;
      amari = l.rem;
      ans = ans + (X + 2 * (shou - 1) * a + a + amari);
      X = amari;
      a = a - X;
    }
  }
  cout << ans << endl;
  return 0;
}
