Document signatures

Schedule N officials in fixed order on their accepted weekdays to minimize the total days from the first Monday.

Easy3GreedySimulationInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

A legislative document has to be produced before an important event can be organized. The document takes effect only after N officials sign it, and the order of signing is fixed. Officials are numbered in signing order, from official 1 to official N.

Each official accepts documents only on a fixed set of weekdays. A week has seven days, but officials accept documents on working days only, Monday to Friday. Every official has at least one accepting day in the week.

One official needs exactly one day to sign. If an official reviews the document on some day, no other official reviews the same document that day.

On a Monday morning you receive the document, and that day counts as day 1. Write a program that computes the minimum number of days needed to collect every signature in the fixed order. If the document cannot be finished within one week, signing continues in the following weeks.

The document is prepared well enough that every official signs it on the first submission, within one day, without objections.

Input

The first line contains N, the number of officials (1N501 \le N \le 50).

Each of the next N lines contains five space-separated integers for the weekdays Monday to Friday. The number in row i for weekday j is 0 if official i does not accept documents on weekday j, and 1 if the official accepts them.

Output

Print one integer, the minimum number of days needed to sign the document.

Hint

In the first example two officials sign the document. Official 1 accepts documents on Monday, Wednesday and Friday, and official 2 on Monday and Thursday. The fastest schedule submits the document to official 1 on Monday or Wednesday, then to official 2 on Thursday, which takes 4 days.

In the second example official 1 accepts documents on Tuesday and Friday, and official 2 on Monday and Tuesday. Even with a Tuesday submission to official 1, the process does not finish in the first week. Official 2 can sign only on the Monday of the next week, so the whole process takes 8 days.