Under construction

Still being built. Things may change

Blistavost

Time limit4sMemory limit1024 MB

Summary
Find the minimum time for a walker starting at position 0 on a line to touch every crystal in the requested ranges no earlier than each range's bedtime.
Level

Hard8 of 10

Topics
Dynamic programming, Intervals, Sorting
Solved
No attempts yet

Problem

In the heart of the Glass Valley lies a mysterious star temple, known for its collection of magical crystals that shine like stars. Each crystal holds special power and emits a radiant glow that lights up the entire valley, as long as it stays untouched.

Every night, the temple guardian must touch ONLY the crystals inside the ranges requested by the valley's residents, and must satisfy all of their requests. Each request names a range of crystals that must NOT stop shining before the resident's bedtime, because the residents are afraid of the dark.

The guardian starts at the temple entrance and must plan the route so that each crystal stops shining at the right moment. The crystals are placed in a line, one meter apart, and the first crystal is one meter from the entrance. The guardian moves at one meter per second and may stop whenever needed. Touching and dimming a crystal takes no time. The guardian does not have to return to the starting position.

Find the minimum number of seconds needed to satisfy all requests.

Input

The first line contains an integer NN (1≤N≤50001 \le N \le 5000), the number of requests.

Each of the next NN lines contains integers lil_i, rir_i, and tit_i (1≤li≤ri≤10181 \le l_i \le r_i \le 10^{18}, 1≤ti≤10181 \le t_i \le 10^{18}), the left and right bounds of the crystal range and the resident's bedtime.

Output

Output the minimum time in seconds needed to fulfill all the requests, on a single line.

Examples3

  1. Example 1

    Input
    3
    1 1 1
    3 3 5
    5 5 3
    
    Expected output
    7
    
  2. Example 2

    Input
    3
    1 2 1
    1 1 5
    1 3 4
    
    Expected output
    6
    
  3. Example 3

    Input
    3
    6 6 6
    8 8 7
    9 9 9
    
    Expected output
    9