#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-9;
int N;
int X;
int gcd(int a, int b) {
  if (b > a) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  cin >> N >> X;
  int M = N - X;
  cout << (N - gcd(N, M)) * 3 << endl;
  return 0;
}
