<?php
    /*
        Problem URL : http://agc001.contest.atcoder.jp/tasks/agc001_b

        Score  :
        Result :
        Time   : ms
        Memory : KB
     */

    ini_set('display_errors', 'Off');
    ini_set('error_reporting', E_ALL & ~E_NOTICE);

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

    function uec($a, $b) {
        while ($a != $b) {
            if ($a > $b) {
                $a -= $b;
            } else {
                $b -= $a;
            }
        }
        return $a;
    }

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