Under construction

Still being built. Things may change

Who Watches the Watchmen?

Time limit4sMemory limit2048 MB

Summary
Reposition sentries so each one watches a different sentry and is watched by exactly one other, minimizing energy (1 per rotation, 1000 per move).
Level

Hard9 of 10

Topics
Geometry, Graph, Math
Solved
No attempts yet

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 11 unit of energy to change the viewing direction of a sentry and 1 0001\,000 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 1 0011\,001 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 (x,y,z)(x,y,z) facing direction (vx,vy,vz)(vx,vy,vz) can see any point (x+t⋅vx,y+t⋅vy,z+t⋅vz)(x + t \cdot vx, y + t \cdot vy, z + t \cdot vz) for t≥0t \ge 0, 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 nn (1≤n≤5001 \le n \le 500), the number of sentries.

Each of the next nn lines contains six integers xx, yy, zz, vxvx, vyvy and vzvz, meaning there is a sentry at (x,y,z)(x,y,z) facing direction (vx,vy,vz)(vx,vy,vz). All values are in the range from −106-10^6 to 10610^6, inclusive. At least one of vxvx, vyvy or vzvz 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 −1-1 if it is not possible.

Examples1

  1. Example 1

    Input
    4
    66 45 10 73 39 36
    95 14 26 47 84 59
    14 66 89 89 36 78
    16 27 94 79 24 24
    
    Expected output
    4