Under construction

Still being built. Things may change

Directions

Time limit4sMemory limit512 MB

Summary
Pick the cheapest set of direction vectors whose nonnegative multiples let Snuke reach any point from any other, or print -1 if no set works.
Level

Hard8 of 10

Topics
Geometry, Sorting, Dynamic programming
Solved
No attempts yet

Problem

Initially, Snuke cannot move at all. There are nn tickets, and the price of the ii-th ticket is pip_i. If Snuke buys the ii-th ticket, then for every point (x,y)(x, y) and every nonnegative number tt, he can move from (x,y)(x, y) to (x+tai,y+tbi)(x + t a_i, y + t b_i). Snuke wants to buy tickets so that he can travel between any two points. Compute the minimal possible total price of the tickets he must buy.

Input

The first line contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5). Then nn lines follow, the ii-th of which contains three integers aia_i, bib_i, pip_i (−109≤ai,bi≤109-10^9 \le a_i, b_i \le 10^9, 1≤pi≤1091 \le p_i \le 10^9).

Output

Print the minimal possible total price of the tickets he must buy to be able to move between any two points. If this is impossible, print -1 instead.

Hint

For example, Snuke can buy tickets 1, 3, and 6.

Examples2

  1. Example 1

    Input
    7
    0 3 1
    0 3 2
    1 -1 2
    0 0 1
    -2 4 1
    -4 0 1
    2 1 2
    
    Expected output
    4
    
  2. Example 2

    Input
    2
    1 2 3
    4 5 6
    
    Expected output
    -1