#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

int main(){

	int N,X,M,table[7][7];
	scanf("%d %d %d",&N,&X,&M);

	for(int i = 1; i <= 6; i++){
		for(int k = 1; k <= 6; k++)table[i][k] = -1;
	}

	int left,right,num;
	bool Error = false;

	for(int i = 0; i < M; i++){
		scanf("%d %d %d",&left,&right,&num);
		if(Error == false && table[left][right] != -1 && table[left][right] != num){
			Error = true;
		}
		table[left][right] = num;
	}

	if(Error){
		printf("-1\n");
		return 0;
	}

	int maximum = -1,tmp,ans[7];
	bool FLG;

	for(int a = 0; a <= X; a++){
		if(N == 1){
			FLG = true;

			if(table[1][1] != a){
				FLG = false;
			}
			if(FLG){
				if(maximum < a){
					maximum = a;
					ans[0] = a;
				}
			}
		}
		for(int b = 0; b <= X; b++){
			if(N == 1)break;

			if(N == 2){
				int work[7] = {0,a,b,0,0,0,0};

				FLG = true;

				for(int i = 1; i <= 2; i++){
					for(int k = i; k <= 2; k++){
						if(table[i][k] == -1)continue;
						tmp = 0;
						for(int p = i; p <= k; p++){
							tmp += work[p];
						}
						if(tmp != table[i][k]){
							FLG = false;
							break;
						}
					}
					if(!FLG)break;
				}

				if(FLG){
					if(maximum < a+b){
						maximum = a+b;
						ans[0] = a,ans[1] = b;
					}
				}

			}
			for(int c = 0; c <= X; c++){
				if(N == 2)break;

				if(N == 3){
					int work[7] = {0,a,b,c,0,0,0};

					FLG = true;

					for(int i = 1; i <= 3; i++){
						for(int k = i; k <= 3; k++){
							if(table[i][k] == -1)continue;
							tmp = 0;
							for(int p = i; p <= k; p++){
								tmp += work[p];
							}
							if(tmp != table[i][k]){
								FLG = false;
								break;
							}
						}
						if(!FLG)break;
					}

					if(FLG){
						if(maximum < a+b+c){
							maximum = a+b+c;
							ans[0] = a,ans[1] = b,ans[2] = c;
						}
					}
				}
				for(int d = 0; d <= X; d++){
					if(N == 3)break;

					if(N == 4){
						int work[7] = {0,a,b,c,d,0,0};

						FLG = true;

						for(int i = 1; i <= 4; i++){
							for(int k = i; k <= 4; k++){
								if(table[i][k] == -1)continue;
								tmp = 0;
								for(int p = i; p <= k; p++){
									tmp += work[p];
								}
								if(tmp != table[i][k]){
									FLG = false;
									break;
								}
							}
							if(!FLG)break;
						}

						if(FLG){
							if(maximum < a+b+c+d){
								maximum = a+b+c+d;
								ans[0] = a,ans[1] = b,ans[2] = c,ans[3] = d;
							}
						}


					}
					for(int e = 0; e <= X; e++){
						if(N == 4)break;

						if(N == 5){
							int work[7] = {0,a,b,c,d,e,0};

							FLG = true;

							for(int i = 1; i <= 5; i++){
								for(int k = i; k <= 5; k++){
									if(table[i][k] == -1)continue;
									tmp = 0;
									for(int p = i; p <= k; p++){
										tmp += work[p];
									}
									if(tmp != table[i][k]){
										FLG = false;
										break;
									}
								}
								if(!FLG)break;
							}

							if(FLG){
								if(maximum < a+b+c+d+e){
									maximum = a+b+c+d+e;
									ans[0] = a,ans[1] = b,ans[2] = c,ans[3] = d,ans[4] = e;
								}
							}
						}
						for(int f = 0; f <= X; f++){
							if(N == 5)break;

							if(N == 6){
								int work[7] = {0,a,b,c,d,e,f};

								FLG = true;

								for(int i = 1; i <= 6; i++){
									for(int k = i; k <= 6; k++){
										if(table[i][k] == -1)continue;
										tmp = 0;
										for(int p = i; p <= k; p++){
											tmp += work[p];
										}
										if(tmp != table[i][k]){
											FLG = false;
											break;
										}
									}
									if(!FLG)break;
								}

								if(FLG){
									if(maximum < a+b+c+d+e+f){
										maximum = a+b+c+d+e+f;
										ans[0] = a,ans[1] = b,ans[2] = c,ans[3] = d,ans[4] = e,ans[5] = f;
									}
								}
							}
						}
					}
				}
			}
		}
	}

	if(maximum == -1){
		printf("%d\n",maximum);
	}else{
		printf("%d",ans[0]);
		for(int i = 1; i < N; i++){
			printf(" %d",ans[i]);
		}
		printf("\n");
	}

	return 0;
}

Measured memory_bytes: 2732