#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x;
  cin >> n >> x;
  long long int m = n;
  long long int rem, ram;
  n -= x;
  while (n % x != 0) {
    rem = n / x * x;
    m += rem * 2;
    ram = n;
    n = x;
    x = ram - x;
  }
  rem = n / x * x;
  m += rem * 2 - x;
  printf("%lld\n", m);
}
