Edward is wandering around in Schenley Park in hopes of coming up with new problem ideas for DMOPC. The park can be modeled as a set of
junctions numbered from
to
connected by
roads such that there is exactly one path between any two junctions. Edward may choose to begin his walk from any junction, and during his walk he will repeatedly choose an adjacent road to walk on until he reaches the neighbouring junction. For convenience, we say that the length of his walk is simply the number of roads he passes through. Edward would love to wander forever to generate as many ideas as possible, but there is one problem: the roads often have benches where people may sit and observe passersby! Due to his social anxiety, he does not want to look like a wandering weirdo to the people on the benches and will restrict himself to walking along the road connecting junctions
and
at most
times. Given this condition, what is the length of the longest possible walk through the park?
Constraints



Subtask 1 [5%]
is even for all
.
Subtask 2 [10%]
for all
.
Subtask 3 [20%]
for all
.
Subtask 4 [25%]

Subtask 5 [40%]
No additional constraints.
Input Specification
The first line contains the integer
.
The next
lines each contain
integers
and
.
Output Specification
Output a single integer, the length of the longest possible walk through the park.
Sample Input
Copy
7
1 2 3
1 3 1
3 4 2
3 5 3
4 6 1
4 7 1
Sample Output
Copy
9
Explanation for Sample
Edward can walk through the junctions in the following order:
. This is a walk of length
, and it can be proven that no longer walk exists.
Comments