You are cooking on a gigantic stove in a large fast food restaurant. The stove has n heating elements in a row, numbered 1 through n from left to right. Each element is operated by its own control knob. The knobs are a bit unusual: seven non-zero digits are written at equal spacing around each knob, and the power of the element is the integer you get by reading those digits clockwise, starting at the top of the knob.

The picture shows the starting positions of the knobs in the first sample input.
In a single step you can rotate one or more consecutive knobs by any number of positions in either direction. All knobs rotated in one step move by the same number of positions in the same direction.
Find the smallest number of steps needed to set every heating element to its maximum possible power.