<?php
fscanf(STDIN, "%d %d", $n, $x);
$ans = $n;
$y = 0;
$z = 0;
$y = $n - $x;
if ($x == $y) {
  $ans += $y;
} else {
  if ($n / $y > 2) {
    $ans += (floor($n / $y)- 1 ) * $y * 2;
    $z = $n - (floor($n / $y) * $y);
  } else {
    $z = $n - $y;
  }
  while ($y % $z != 0) {
    $ans += floor($y / $z)  * $z * 2;
    $a = $z;
    $z = $y % $z;
    $y = $a;
  }
    $ans += ($y / $z - 1) * $z * 2 + $z;
}
echo $ans."\n";
