#include <bits/stdc++.h>
using namespace std;
int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
long long read() {
  long long s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * f;
}
void put(long long x) {
  if (x >= 10) put(x / 10);
  putchar(x % 10 + '0');
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  long long n = read(), x = read();
  put((long long)3LL * (n - gcd(n, x)));
  return 0;
}
