A. Shifts

A. Shifts

http://codeforces.com/contest/229/problem/A

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a table consisting of n rows and m 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.

Input

The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.

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

Output

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.

Examples

Input

Copy

3 6
101010
000100
100000

Output

Copy

3

Input

Copy

2 3
111
000

Output

Copy

-1

Note

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.

这码力也是醉了。。。水题捯饬了一会。

代码:

#include<bits/stdc++.h>
using namespace std;
char s[110][12100];
int L[110][12100],R[110][12100];
int ans;
const int INF=0x3f3f3f3f;
int main()
{
    int n,m;
    int flag;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(L,INF,sizeof(L));
        memset(R,INF,sizeof(R));
        flag=0;
        int haha;
        for(int i=1;i<=n;i++){
            haha=1;
            scanf("%s",s[i]);
            for(int j=0;j<m;j++){
                if(s[i][j]=='1')
                {
                    R[i][m-1]=j+1,haha=0;
                    break;
                }
            }
            if(haha==1 || flag==2)
            {
                flag=2;
               /// cout<<"-1"<<endl;
                continue;
            }
            if(s[i][0]=='1')
                L[i][0]=0;
            else{
                for(int j=m-1;j>=0;j--){
                    if(s[i][j]=='1')
                    {
                        L[i][0]=m-1-j+1;
                        break;
                    }
                }
            }
     ///       cout<<"L[][]"<<L[i][0]<<endl;
             for(int j=1;j<m;j++){
                if(s[i][j]=='1')
                    L[i][j]=0;
                else
                    L[i][j]=L[i][j-1]+1;
            }
            for(int j=m-2;j>=0;j--){
                if(s[i][j]=='1')
                    R[i][j]=0;
                else
                    R[i][j]=R[i][j+1]+1;
            }

        }
        if(flag==2)
        {
            cout<<"-1"<<endl;
            continue;
        }
        int sum=0;
        ans=INF;
        for(int i=0;i<=m-1;i++){
            sum=0;
            for(int j=1;j<=n;j++){
                    int lala=min(L[j][i],R[j][i]);
                  ////  cout<<"L:"<<L[j][i]<<"   R:"<<R[j][i]<<endl;
          ///  cout<<"lala:"<<lala<<endl;
               sum+=lala;


            }
         ///   cout<<"sum:"<<sum<<endl;
            ans=min(ans,sum);
        }
        printf("%d\n",ans);
    }
}
/*
5 5

10001

00100

01000

01001

11111

*/
发布了565 篇原创文章 · 获赞 110 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/86987995