Goal
Given the condensed formula of an alicylic hydrocarbon, decide whether it is valid or not.
The condensed formula includes units of carbon linked to one another by one or more bonds.
1 unit of carbon is represented as CHn.
The bonds are adjacent to the carbon units, either horizontally or vertically. Bonds are represented as (m), where m is the number of bonds between two adjacent carbon units.
The inputs given will have a valid representation, but the bonds might not match the carbon units. For a carbon unit CHn, the sum of the m values of all bonds adjacent to that carbon must be 4 - n. Each bond contributes its m value to both carbon units that it connects.
Input
Line 1: An integer N, denoting the number of lines in the condensed formula of a single hydrocarbon
Next N Lines: String COMPOUND representing each line of the condensed formula
Output
If the hydrocarbon is valid, a single line VALID.
If it is not valid, a single line INVALID.
Constraints
1 ≤ N ≤ 20
In carbon units, 0 ≤ n ≤ 4
In bonds, 0 < m < 4