CF229A.Shifts

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a table consisting of nn rows and mm columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.

To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".

Determine the minimum number of moves needed to make some table column consist only of numbers 1.

输入格式

The first line contains two space-separated integers: nn ( 1<=n<=1001<=n<=100 ) — the number of rows in the table and mm ( 1<=m<=1041<=m<=10^{4} ) — the number of columns in the table. Then nn lines follow, each of them contains mm characters "0" or "1": the jj -th character of the ii -th line describes the contents of the cell in the ii -th row and in the jj -th column of the table.

It is guaranteed that the description of the table contains no other characters besides "0" and "1".

输出格式

Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.

输入输出样例

  • 输入#1

    3 6
    101010
    000100
    100000
    

    输出#1

    3
    
  • 输入#2

    2 3
    111
    000
    

    输出#2

    -1
    

说明/提示

In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.

In the second sample one can't shift the rows to get a column containing only 1s.

首页