import std.algorithm,
       std.string,
       std.array,
       std.range,
       std.stdio,
       std.conv;

R delegate(Args) Z(R, Args...)(R delegate(R delegate(Args), Args) f){
  return (Args args) => f(Z(f), args);
}

ulong delegate(ulong, ulong) gcd;

static this() {
  gcd = Z((ulong delegate(ulong, ulong) gcd, ulong a, ulong b) =>
          b ? gcd(b, a%b) : a < 0 ? a * -1 : a);
}

void main() {
  ulong[2] inputs = readln.chomp.split.to!(ulong[2]);
  ulong N = inputs[0],
        X = inputs[1];

  writeln(3 * (N - gcd(N, X)));
}

