use std::fmt::Debug;
use std::str::FromStr;
use std::io::*;

fn main() {
    let i = stdin();
    solve(&mut i.lock(), &mut stdout());
}

fn solve(i: &mut BufRead, o: &mut Write) {
    let nx = read_vec::<usize>(i);
    let n = nx[0];
    let x = nx[1];

    fn f(
        l: usize, // 左の長さ
        b: usize, // 下の長さ
    ) -> usize {
        if l == b {
            l
        } else if l > b {
            b * 2 + f(l - b, b)
        } else {
            let b_next = b % l;
            if b_next == 0 {
                ((b / l) * 2 - 1) * l
            } else {
                (b / l) * 2 * l + f(l, b_next)
            }
        }
    }
    writeln!(o, "{}", n + f(n - x, x)).unwrap();
}

fn read_line(i: &mut BufRead) -> String {
    let mut line = String::new();
    i.read_line(&mut line).unwrap();
    line.trim().to_string()
}
fn read_vec<T: FromStr>(i: &mut BufRead) -> Vec<T>
where
    T::Err: Debug,
{
    read_line(i)
        .split(' ')
        .map(|x| x.parse().unwrap())
        .collect()
}
