Pizza Delivery Schedule

No attempts yetTime limit1sMemory limit128 MB

Problem

PizzaSocks makes and delivers pizza on a regular schedule. A schedule has a period of 1, 2, 3, or 4 weeks, and the quantity of pizzas delivered is fixed for every day of the period. The first day of every period is always the first day of a week.

A schedule is described by:

  • LL — the length of the schedule period in weeks (1L41 \le L \le 4);
  • A1,A2,,AL×7A_1, A_2, \dots, A_{L \times 7} — the quantity of pizzas delivered on each day of the period (Ai0A_i \ge 0).

A manager lost the schedule of one very important client; only the history of past deliveries survives. You must determine how well a schedule can explain that history.

The task is complicated because the client sometimes changed an order — asking for more or fewer pizzas than scheduled, cancelling an order entirely, or placing an unscheduled one — so no schedule may fit the history perfectly.

For a fixed schedule, count the number of days on which the recorded quantity differs from the quantity the schedule prescribes. Days before the first recorded order and after the last recorded order are ignored. Over all schedules whose period is 1, 2, 3, or 4 weeks, find the smallest possible number of such mismatched days.

Input

The input consists of integers describing the delivery history:

  • NN — the number of fulfilled orders;
  • then NN triples wi,di,qiw_i, d_i, q_i — the week number, the day of the week, and the quantity delivered.

There is at most one record for any single day. Every day that lies between the earliest and the latest recorded day but is not listed explicitly is treated as a day with a zero-quantity order. Weeks are aligned so that a schedule period always begins on the first day of a week.

Output

Print a single integer: the minimum possible number of days on which the recorded quantity differs from the schedule, taken over all schedules whose period is 1, 2, 3, or 4 weeks.

Constraints

  • 1qi1001 \le q_i \le 100, 1wi521 \le w_i \le 52, 1di71 \le d_i \le 7