#P4749. [CERC2017] Kitchen Knobs

[CERC2017] Kitchen Knobs

题目描述

You are cooking on a gigantic stove at a large fast-food restaurant. The stove contains nn heating elements arranged in a line and numbered with integers 11 through nn 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.

输入格式

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

输出格式

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

6
9689331
1758824
3546327
5682494
9128291
9443696

3

7
5941186
3871463
8156346
9925977
8836125
9999999
5987743

2

提示

In the first example, one of the ways to achieve maximal possible power is: rotate knobs 22 through 33 by 33 positions in the counterclockwise direction, rotate knob 33 by 33 positions in the counterclockwise direction, and rotate knobs 44 through 66 by 22 positions in the clockwise direction.