#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

int n, m, p, first, second, third, tmp;
int a [1001][4];
bool ok;

int main(int argc, const char* argv[])
{	
	ifstream in (argv[2]); // drugi parametar - ulazni fajl
	in >> n >> m >> p;
	for (int i = 1; i <= n; i++)
	    for (int j = 1; j <= 3; j++)
	        a [i][j] = 0;
	        	
	if ((n < 3) || (n > 1000) || (p < 1) || (p > 50) || (m < 1) || (m > 20000))
	{
		cout << "GRESKA" << endl;
	 	return 0;
	}
	for (int i = 0; i < m; i++)
	{
		in >> first >> second >> third;
        if ((first == second) || (first == third) || (second == third) || (first < 1) || (first > n) || (second < 1) || (second > n) || (third < 1) || (third > n))
        {
			cout << "GRESKA" << endl;
			return 0;
		}
        a [first][1]++;
        a [second][2]++;
        a [third][3]++;
	}
	in.close();
	
	ofstream score(argv[4]);
	
	ifstream sol (argv[3]);
	sol >> tmp;
	ifstream out (argv[1]);
	if (out.is_open() == false)
		 score << 0 << endl;
	else
	{
		out >> first;
	 	if (tmp == -1)
		{
			if (first != -1)
				score << 0 << endl;
	 	 	else
				score << 5 << endl;
		}
		else
		{
			if (first == -1)
				score << 0 << endl;
	 		else
	 		{
				out >> second >> third;
				if ((first < 0) || (first > p) || (second < 0) || (second > p) || (third < 0) || (third > p) || (first <= second) || (second <= third))
					score << 0 << endl;
				else
				{
					tmp = first * a [1][1] + second * a [1][2] + third * a [1][3];
					ok = true;
					for (int i = 2; i <= n; i++)
						if (tmp < first * a [i][1] + second * a [i][2] + third * a [i][3])
						{
                 			ok = false;
				       		break;
		    			}
					if (ok == true)
						score << 5 << endl;
			  		else
						score << 0 << endl;
				}
			}
		}
	}
	
 	sol.close();
 	out.close();
 	score.close();
	return 0;
}
