<?php

    fscanf(STDIN, "%d %d", $N, $X);

    function uec($a, $b) {
        while ($a != $b) {
            $diff = abs($a - $b);
            $c = floor($diff / min($a, $b));
            if ($a > $b) {
                $a -= $b * $c;
            } else {
                $b -= $a * $c;
            }
        }
        return $a;
    }

    echo ($N - uec($N, $X)) * 3 . "\n";
