Scanf.scanf "%d" (fun n ->
    let rec gcd a b = if b = 0 then a else gcd b (a mod b) in

    let rec loop i acc sum =
        if i mod 2 = 0 || i mod 3 = 0 then (
            Printf.printf "%d " i;
            let acc = acc + 1 in
            let sum = sum + i in
            if acc = n - 1 then (
                let rec loop2 k =
                    if (k + sum) mod 2 = 0 &&
                       (k + sum) mod 3 = 0 &&
                        gcd k sum <> 1 then k else loop2 (k + 1)
                in
                Printf.printf "%d\n" @@ loop2 (i + 1)
            ) else loop (i +1) acc sum
        ) else loop (i + 1) acc sum
    in
    loop 2 0 0
)