[CERC2017] Kitchen Knobs

题意翻译

题目描述 你正一家大型的快餐餐厅的巨型灶台上烹饪。这个灶台上有n个炉子排成一排,并按序标为1-n号。每个炉子都会被它的控制手柄所控制。这些手柄可不一般:每个手柄上面都有1-7的号码围成一圈。灶台的火力就是从它的控制手柄顶端开始顺时针读取数字而得到的。 【图片】:样例数据1输入的初始手柄位置如图所示。 每一步你都可转动一个或多个相邻手柄——向任意方向转动任意几下。但是,同一步转动的手柄只能向同一方向转动同样的次数。 找到最少的步数来吧所有的炉子都设定到可能的最大火力。 输入输出格式 输入格式 第一行包含一个整数(1<=n<=501)表示炉子的数量。接下去第j行包含7个非零整数,其中第一个是初始火力大小。 输出格式 输出一个整数,表示需要的步数。

题目描述

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$ 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. [![PujzQA.md.png](https://s1.ax1x.com/2018/07/12/PujzQA.md.png)](https://imgchr.com/i/PujzQA) 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.

输入输出格式

输入格式


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 a single integer — the minimal number of steps needed.

输入输出样例

输入样例 #1

6
9689331
1758824
3546327
5682494
9128291
9443696

输出样例 #1

3

输入样例 #2

7
5941186
3871463
8156346
9925977
8836125
9999999
5987743

输出样例 #2

2

说明

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.