#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 300 + 5;
unsigned long long gcd(const unsigned long long a, const unsigned long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  unsigned long long n, x;
  cin >> n >> x;
  unsigned long long TotalLength = n;
  unsigned long long TriangleLength = x;
  unsigned long long quo = n / x;
  unsigned long long res = 0;
  while (true) {
    if (TriangleLength == 0) {
      cout << res << endl;
      return 0;
    }
    quo = TotalLength / TriangleLength;
    if (res != 0 && TotalLength % TriangleLength == 0)
      res += 3 * quo * TriangleLength;
    else
      res += 3 * (quo - 1) * TriangleLength;
    unsigned long long temp = TotalLength;
    TotalLength = TriangleLength;
    TriangleLength = temp % TriangleLength;
  }
}
