Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years.
×

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

G. Mateusz and Escape Room

time limit per test

7 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputMateusz likes to travel! However, on his $$$42$$$nd visit to Saint Computersburg there is not much left to sightsee. That's why he decided to go to an escape room with his friends!

The team has solved all riddles flawlessly. There is only one riddle remaining — a huge circular table! There are $$$n$$$ weighing scales lying on top of the table, distributed along the circle. Each scale is adjacent to exactly two other scales: for each $$$i \in \{1, 2, \dots, n-1\}$$$, the $$$i$$$-th and the $$$(i+1)$$$-th scales are adjacent to each other, as well as the first and the $$$n$$$-th scale.

The $$$i$$$-th scale initially contains $$$a_i$$$ heavy coins. Mateusz can perform moves — each move consists of fetching a single coin from one scale and putting it on any adjacent scale.

It turns out that the riddle will be solved when there is a specific amount of coins on each of the scales. Specifically, each scale has parameters $$$l_i$$$ and $$$r_i$$$. If each coin lies on a single scale and for each $$$i$$$, the $$$i$$$-th scale contains at least $$$l_i$$$ and at most $$$r_i$$$ coins, the riddle will be solved and Mateusz's team will win!

Mateusz is aiming for the best possible time. Therefore, he wants to solved the riddle as quickly as possible. What is the minimum possible number of moves required to fulfill all the conditions?

Input

The first line contains an integer $$$n$$$ ($$$3 \le n \le 35\,000$$$) — the number of weighing scales in the circle.

The following $$$n$$$ lines describe the scales. The $$$i$$$-th of these lines describes the $$$i$$$-th scale and consists of three integers $$$a_i, l_i, r_i$$$ ($$$0 \le a_i \le 35\,000$$$, $$$0 \le l_i \le r_i \le 35\,000$$$).

It's guaranteed that the riddle is solvable, that is, $$$\sum_{i=1}^n l_i \le \sum_{i=1}^n a_i \le \sum_{i=1}^n r_i$$$.

Output

Output one integer — the minimum number of operations required to solve the riddle.

Examples

Input

5 0 2 3 1 2 3 4 3 3 4 3 3 4 3 3

Output

4

Input

3 0 1 2 3 0 3 1 0 0

Output

1

Input

4 1 0 2 3 3 3 4 0 4 5 3 5

Output

0

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Feb/22/2020 10:51:04 (h2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|