Who Watches the Watchmen?
Time limit4sMemory limit2048 MB
Reposition sentries so each one watches a different sentry and is watched by exactly one other, minimizing energy (1 per rotation, 1000 per move).
Problem
There are some sentry drones guarding a top-secret facility. Each sentry is stationary at some point in 3D space and faces in some viewing direction.
With recent advances in artificial intelligence, the owners of the facility have realized that the greatest threats to the facility are not intruders but the sentries themselves! For security, they want to adjust the sentries so that every sentry is watching another sentry and every sentry is seen by exactly one other sentry.
It costs unit of energy to change the viewing direction of a sentry and units of energy to move a sentry to a new location. These operations are independent. Changing both a sentry's position and viewing direction costs units of energy in total. No two sentries can be at the same position at the end of a move. After being moved, a sentry's position might not be on an integer lattice point.
A sentry at location facing direction can see any point for , as long as no other sentry is directly between it and the point. What is the minimum amount of energy needed to reposition the sentries so that each sentry is seen by exactly one other sentry?
Input
The first line contains a single integer (), the number of sentries.
Each of the next lines contains six integers , , , , and , meaning there is a sentry at facing direction . All values are in the range from to , inclusive. At least one of , or is non-zero. No two sentries are at the same position.
Output
Output a single integer, the minimum amount of energy needed to reposition the sentries so that each sentry can be seen by exactly one other sentry. Output if it is not possible.