#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <sstream>
using namespace std;

vector<int> input(size_t n){
	string s;
	if(!getline(cin, s)){ throw 0; }
	stringstream ss1(s), ss2;
	istream_iterator<int> beg(ss1), en;
	vector<int> ret(beg, en);
	if(ret.size() != n){ throw 0; }
	for(size_t i = 0; i < n; ++i){
		if(i){ ss2 << ' '; }
		ss2 << ret[i];
	}
	if(ss2.str() != s){ throw 0; }
	return ret;
}

int main(){
	while(1){
		vector<int> v = input(2);
		int n = v[0], m = v[1];
		if(n == 0 && m == 0){ break; }
		if(n < 2 || n > 100){ throw 0; }
		for(int i = 0; i < m; ++i){
			v = input(3);
			int a = v[0], b = v[1], c = v[2];
			if(a < 0 || a >= n || b < 0 || b >= n){ throw 0; }
			if(c < 1 || c > 100){ throw 0; }
		}
	}
	if(cin.eof()){ throw 0; }
	cin.peek();
	if(!cin.eof()){ throw 0; }
}