C. Shifts

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

3 6
101010
000100
100000

Output

3

Input

2 3
111
000

Output

-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.

每一行都可以將其循環移位一下,問最少移位多少下,能夠使得某一列都是1.


 題目思路:既然放到了二分搜索的題目裏,就要考慮用二分的思想來考慮這個問題,
首先,如果有一行全是 0,那麼直接輸出 -1即可
對於每列計算把每一行中與此列最近的1移到此列的距離之和,然後取得最小值。然後可以去枚舉每行兩個 1 之間的位置到兩個 1 的最小距離,然後將每列上的結果相加,取每列中的最小值

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=1e4+5;

int n,m,t,_;
int i,j,k;
int cnt;
char ch[100+5][idata];
int pos[idata],dis[idata];

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++){
            int f1=0;
            for(j=1;j<=m;j++){
                cin>>ch[i][j];
                if(ch[i][j]=='1'){
                    f1=1;
                }
            }
            if(f1==0){
                puts("-1");
                return 0;
            }
        }
        for(i=1;i<=n;i++){
            ms(pos,0),cnt=0;
            for(j=1;j<=m;j++){
                if(ch[i][j]=='1'){
                    pos[++cnt]=j;
                }
            }

            pos[++cnt]=pos[1]+m;
            for(j=1;j<cnt;j++){
                int mid=(pos[j]+pos[j+1])/2;
                for(k=pos[j];k<=pos[j+1];k++){
                    if(k<=mid){
                        dis[k%m]+=k-pos[j];
                    }
                    else{
                        dis[k%m]+=pos[j+1]-k;
                    }
                }
            }
        }

        int minn=0x7f7f7f7f;
        for(i=0;i<m;i++){
            minn=min(minn,dis[i]);
        }
        cout<<minn<<endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章