#include <iostream>
#include <vector>

static const int N = 6;
static const int M = 6;

typedef std::vector<int> vec;
typedef std::vector<std::vector<int>> vecvec;

static const int U[M][N] = {{1, 12, 10, 8, 9, 3},
							{14, 16, 17, 6, 2, 15},
							{12, 1, 13, 2, 4, 18},
							{13, 9, 9, 10, 13, 12},
							{0, 2, 17, 14, 7, 11},
							{19, 3, 2, 19, 0, 1}};

vecvec assign(int n,int m, vec ne, vec po, vecvec ot, int j) {
	for (int i{}; i < m; i++) {
		if(po[i] == 0) {
			po[i] = 1;
			ne[j] = i;
			ne[n] += U[ne[j]][j];
			if (j < n-1) ot = assign(n, m, ne, po, ot, j+1);
			if(ne[n] >= ot[0][n]) {
				if(ne[n] > ot[0][n]) ot.clear();
				ot.push_back(ne);
			}
			else {
				po[i] = 0;
				ne[n] -= U[ne[j]][j];
			}
		}
	}
	
	
	return ot;
}

vecvec assign() {
	return assign( N, M, vec(N+1, 0), vec(N+1, 0), vecvec(1,vec(N+1, 0)), 0);
}


int main() {
	// your code goes here
	
	vecvec ot = assign();
	
	for (auto v : ot) {
		for(int i : v) std::cout << i << ' ';
		std::cout << std::endl;
	}
	
	return 0;
}