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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cerr << #a << " = " << a << endl;

template<typename T> void setmax(T& x, T y) {x = max(x, y);}
template<typename T> void setmin(T& x, T y) {x = min(x, y);}

const double PI = acos(-1.0);
const LL INF = 1e9 + 47;
const LL LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1 << 20;
int belong[3][N];

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	int n, k;
	cin >> n >> k;
	if (2 * k > n + 1){
		cout << -1 << endl;
		return 0;
	}

	FILL(belong, -1);

	if (n & 1){
		const int m = (n + 1) >> 1;
		FOR(i, 0, n >> 1){
			belong[0][i] = i;
			belong[1][i + m] = i;
			belong[2][1 + 2 * i] = i;
		}
		
		FOR(i, n >> 1, n){
			belong[0][i] = i;
			belong[1][i - (n >> 1)] = i;
			belong[2][2 * (i - (n >> 1))] = i;
		}
	}else{
		FOR(i, 0, 3) belong[i][0] = 0;
		const int m = n >> 1;
		FOR(i, 1, m){
			belong[0][i] = i;
			belong[1][m + i] = i;
			belong[2][2 * i] = i;
		}
		
		FOR(i, m, n){
			belong[0][i] = i;
			belong[1][1 + i - m] = i;
			belong[2][1 + 2 * (i - m)] = i;
		}
	}
	
	vector<vector<int>> ans(n);
	FOR(i, 0, 3) FOR(j, 0, n) ans[belong[i][j]].PB(k + i * n + j);
	FOR(i, 0, n){
		assert(ans[i][0] + ans[i][1] <= ans[i][2]);
		for(auto j: ans[i]) cout << j << ' ';
		cout << '\n';
	}
		
	cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
    return 0;
}