Woburn Challenge 2016-17 Round 4 - Senior Division

"All right, slick Nick, you're under arrest."
"Really, for what?"
"Gee, I don't know. How about selling food without a permit,
transporting undeclared commerce across borough lines, false
advertising…"
"Here: Permit, receipt of declared commerce, and I did not falsely
advertise anything. Take care."
"You told that mouse the pawpsicle sticks were redwood!"
"That's right. Red wood. With a space in the middle. Wood that is red."
Nick Wilde has got quite the clever money-making scheme going on. Every day, he…
- Heads to an ice cream parlor, purchases a "Jumbo Pop" popsicle (unless he can hustle someone else into purchasing it for him), and melts it down into a sugary liquid.
- Transports it to Zootopia's Tundratown district, makes paw prints in a snowy field, fills them with the popsicle liquid, places a popsicle stick in each one, and waits for the resulting "Pawpsicles" to solidify.
- Sets up a sales booth outside a lemming bank just in time for the end of the workday, and sells his Pawpsicles to the lemmings as they leave the bank, collecting the discarded popsicle sticks.
- Delivers the popsicle sticks to a miniature construction site in Little Rodentia, passing it off as lumber.
By following this sequence of steps, Nick is able to pull in a handsome profit of $200 every day. And the whole operation is perfectly legal! (Well, he may be failing to record his income on his tax returns, but that's besides the point.)
Still, time is money, so Nick's wondering if he's going about things as efficiently as possible. After all, Zootopia's a big place, so studying its map more carefully may suggest some improvements to his daily route.
Zootopia has
For Nick's purposes, he's classified each location
Nick starts each day in location
In order to optimize the efficiency of his Pawpsicle operation, Nick
would like to determine the minimum amount of time in which he can
complete a route which visits the
In test cases worth
Input Specification
The first line of input consists of two space-separated integers
Output Specification
Output one line consisting of a single integer - the minimum number of
minutes required for Nick to complete his Pawpsicle operation, or -1
if
it can't be done.
Sample Input
9 9
2 0 0 1 2 3 4 4 3
1 4 9
4 2 3
2 1 4
5 4 1
5 6 4
7 2 9
3 1 2
3 7 3
3 9 4
Sample Output
27
Sample Explanation
One optimal route which Nick can take is as follows (with the
This route will take
Comments