Problem:
Score : 700 points

Problem Statement

Given are positive integers N and K.

Determine if the 3N integers K, K+1, ..., K+3N-1 can be partitioned into N triples (a_1,b_1,c_1), ..., (a_N,b_N,c_N) so that the condition below is satisfied. Any of the integers K, K+1, ..., K+3N-1 must appear in exactly one of those triples.

For every integer i from 1 to N, a_i + b_i \leq c_i holds.

If the answer is yes, construct one such partition.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq 10^9

Input

Input is given from Standard Input in the following format:

N K

Output

If it is impossible to partition the integers satisfying the condition, print -1. If it is possible, print N triples in the following format:

a_1 b_1 c_1
:
a_N b_N c_N

Sample Input 1

1 1

Sample Output 1

1 2 3

Sample Input 2

3 3

Sample Output 2

-1

Solution in Rust (1.15.1) (judge verdict: Accepted)

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let n: usize = sc.read();
    let k: usize = sc.read();

    let prefix = (n + 1) / 2;
    let suffix = n / 2;

    let mut a = vec![0; n];
    let mut b = vec![0; n];
    for i in 0..prefix {
        a[prefix - 1 - i] = k + i;
    }
    for i in 0..suffix {
        a[n - 1 - i] = k + i + prefix;
    }
    for i in 0..prefix {
        b[i] = k + n + i * 2;
    }
    for i in 0..suffix {
        b[i + prefix] = k + n + i * 2 + 1;
    }

    let mut ans = vec![];
    for i in 0..n {
        let a = a[i];
        let b = b[i];
        let c = k + n * 2 + i;
        if a + b <= c {
            ans.push((a, b, c));
        } else {
            println!("-1");
            return;
        }
    }

    for (a, b, c) in ans.into_iter() {
        sc.write(format!("{} {} {}\n", a, b, c));
    }
}

/// 22 26
/// 21 28
/// 20 30
/// 25 27
/// 24 29
/// 23 31

/// (m+1)
/// a   b
/// a-1 b+2
/// a-2 b+4
/// a-3 b+6
/// a-m b+2m    =   a+b+m
///
/// m
/// a+m b+1     =   a+b+m+1,    a+m
/// a+m-1 b+3
/// z-2 b+5
/// z-3 b+7
/// ...
/// a+m-(m+1)    b+2(m-1)+1
///
///
/// a   b
/// a-1 b+2
/// a-2 b+4
/// a-3 b+6
/// ...
/// a-(m-1) b+2(m-1)    =   a+b+m-1
///
/// m
/// z b+1     =   z+b+1 = a+b+m => z=a+m-1
/// z-1 b+3
/// z-2 b+5
/// z-3 b+7
/// ...
/// z-(m-1) b+2(m-1)+1   z-(m-1) = a

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}