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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

//#define LOCAL
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Diagnostics;
using static System.Math;   
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;

public static class P
{
    public static void Main()
    {
#if LOCAL
        var rng = new Random();
        while (true)
        {
            var k = rng.Next(1, 100000);
            var n = rng.Next(1, 1000000000);
            Solve(k, n);
            Console.WriteLine("end validate");
        }
#else

        var nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int n = nk[0];
        long k = nk[1];
        var res = Solve(n, k);
        if (res == null)
        {
            Console.WriteLine(-1);
            return;
        }
        Console.WriteLine(string.Join("\n", res.Select(x => $"{x.Item1} {x.Item2} {x.Item3}")));
#endif
    }

    public static Tuple<long, long, long>[] Solve(int n, long k)
    {
        var cands = Enumerable.Range((int)k, 3 * n).Select(x => (long)x).ToArray();
        var prevSum = cands.Take(n * 2).Sum();
        var afterSum = cands.Skip(n * 2).Sum();
        
        //2k <= n + 1であれば合法
        if (prevSum > afterSum)
        {
            if (2 * k <= n + 1) throw new Exception();
            return null;
        } 
        var mother = cands.Skip(n).Take(n).ToArray();

        var a = cands.Take(n / 2).Reverse().Concat(cands.Take(n).Skip(n / 2).Reverse()).ToArray();
        var b = mother.Where(x => x % 2 == 1).Concat(mother.Where(x => x % 2 == 0)).ToArray();
        var c = cands.Skip(n * 2).ToArray();

        var res = a.Concat(b).Concat(c).ToArray();

        if (res.Distinct().Count() != 3 * n)
            throw new Exception();
      
        if (res.Min() != k)
            throw new Exception();

        if (res.Max() != k + 3 * n - 1)
            throw new Exception();

        try
        {
            for (int i = 0; i < n; i++)
                if (a[i] + b[i] > c[i]) throw new Exception();
        }
        catch
        {
            return Enumerable.Range(0, n).Select(x => new Tuple<long, long, long>(c[x] - b[x], b[x], c[x])).ToArray();
        }

        return Enumerable.Range(0, n).Select(x => new Tuple<long, long, long>(a[x], b[x], c[x])).ToArray();
    }
}