eolymp
bolt
Try our new interface for solving problems
Problems

Airports

Airports

An airline company offers flights out of n airports, conveniently labeled from 1 to n. The flight time tij from airport i to airport j is known for every i and j. It may be the case that tijtji, due to things like wind or geography. Upon landing at a given airport, a plane must be inspected before it can be own again. This inspection time pi is dependent only on the airport at which the inspection is taking place and not where the previous flight may have originated.

Given a set of m flights that the airline company must provide, determine the minimum number of planes that the company needs to purchase. The airline may add unscheduled flights to move the airplanes around if that would reduce the total number of planes needed.

Input

The first line contains two space-separated integers n and m (1n, m500). The next line contains n space-separated integers p1, ..., pn (0pi106).

Each of the next n lines contains n space-separated integers. The j-th integer in line i + 2 is tij (0tij106). It is guaranteed that tii = 0 for all i. However, it may be the case that tijtji when ij.

Each of the next m lines contains three space-separated integers si, fi and ti (1si, fin, sifi, 1ti106), indicating that the airline company must provide a flight that flies out from airport si at exactly time ti, heading directly to airport fi.

Output

Print, on a single line, a single integer indicating the minimum number of planes the airline company must purchase in order to provide the m requested flights.

Time limit 1 second
Memory limit 128 MiB
Input example #1
2 2
1 1
0 1
1 0
1 2 1
2 1 1
Output example #1
2
Input example #2
2 2
1 1
0 1
1 0
1 2 1
2 1 3
Output example #2
1
Input example #3
5 5
72 54 71 94 23
0 443 912 226 714
18 0 776 347 810
707 60 0 48 923
933 373 881 0 329
39 511 151 364 0
4 2 174
2 1 583
4 3 151
1 4 841
4 3 993
Output example #3
3
Source 2015 ACM North America - Pacific Northwest, Division 1, Problem A