P28833 [洛谷] [CERC2017] Kitchen Knobs
提高+
题目描述

Problem Description

You are cooking on a gigantic stove at a large fast-food restaurant. The stove contains \( n \) heating elements arranged in a line and numbered with integers \( 1 \) through \( n \) from left to right. Each element is operated by its control knob. The knobs are a bit unusual: each knob is marked with seven non-zero digits evenly distributed around a circle. The power of the heating element is equal to the positive integer obtained by reading the digits on its control knob clockwise starting from the top of the knob.

In a single step, you can rotate one or more consecutive knobs by any number of positions in any direction. However, all knobs rotated in one step need to be rotated by the same number of positions in the same direction.

Find the smallest number of steps needed to set all the heating elements to maximal possible power.

Input Format

The first line contains an integer \( n \) ( \( 1 \le n \le 501 \) ) — the number of heating elements. The \( j \)-th of the following \( n \) lines contains an integer \( x_j \) — the initial power of the \( j \)-th heating element. Each \( x_j \) consists of exactly seven non-zero digits.

Output Format

Output a single integer — the minimal number of steps needed.

Sample Input and Output

Input Sample #1

6
9689331
1758824
3546327
5682494
9128291
9443696

Output Sample #1

3

Input Sample #2

7
5941186
3871463
8156346
9925977
8836125
9999999
5987743

Output Sample #2

2

Notes/Hints

In the first example, one of the ways to achieve maximal possible power is: rotate knobs \( 2 \) through \( 3 \) by \( 3 \) positions in the counterclockwise direction, rotate knob \( 3 \) by \( 3 \) positions in the counterclockwise direction, and rotate knobs \( 4 \) through \( 6 \) by \( 2 \) positions in the clockwise direction.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
6 9689331 1758824 3546327 5682494 9128291 9443696
样例输出
3