#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a)
#define pl(a) printf("%lld\n",a)
#define pi(a) printf("%d\n",a)
#define pd(a) printf("%lf\n",a)
#define pf(a) printf("%f\n",a)
#define lc (start+end)/2
#define rc  lc+1
#define INT_MAX 100000000
 int  lefty[1000][1000];
  int  righty[1000][1000];
  char arr[1000][10000];
int main()
 {
    int n,m;
    cin>>n>>m;
     {
         for(int i=0;i<n;i++)
          {
             for(int j=0;j<m;j++)
              {
                  lefty[i][j]=INT_MAX;
                    righty[i][j]=INT_MAX;
                  }
            }
      }

    for(int i=0;i<n;i++) cin>>arr[i];

    for(int i=0;i<n;i++)
       {
         int c=0;
         for(int j=0;j<m;j++)
          {
             if(arr[i][j]=='1') c++;
             }
             if(c==0)
              {
                 cout<<"-1"<<endl;
                 exit(0);
              }
       }

     for(int i=0;i<n;i++)
      {
         int lt=INT_MAX;
          if(arr[i][0]=='0')
           {
             //cout<<" prob "<<i<< "0 "<<endl;
             for(int j=m-1;j>=0;j--)
              {
                 if(arr[i][j]=='1')
                  {
                    lefty[i][0]=m-j;
                    break;
                          }
                          else
                          lefty[i][j]=INT_MAX;
                    }
              }
              else lefty[i][0]=0;
              for(int j=1;j<m;j++)
               {
                 if(arr[i][j]=='1') lefty[i][j]=0;
                 else lefty[i][j]=lefty[i][j-1]+1;
               }

       }

    for(int i=0;i<=n;i++)
      {

         int lt=INT_MAX;
          if(arr[i][m-1]=='0')
           {
         //      cout<<" prob right "<<i<<endl;
             for(int  j=0;j<m;j++)
              {
                 if(arr[i][j]=='1')
                  {
                    righty[i][m-1]=j+1;
                    // cout<<"set "<<righty[i][0]<<endl;
                    break;
                          }
                          else
                          righty[i][j]=INT_MAX;
                    }
              }
              else righty[i][m-1]=0;

              for(int j=m-2;j>=0;j--)
               {
                 if(arr[i][j]=='1') righty[i][j]=0;
                 else  righty[i][j]=righty[i][j+1]+1;
               }

       }

    int anss=INT_MAX;
    for(int i=0;i<m;i++)
     {
         int temp=0;
        for(int j=0;j<n;j++)
         {
             temp+=min(lefty[j][i],righty[j][i]);
          }
          anss=min(anss,temp);
           //cout<<anss<<endl;
     }
    /* cout<<"lefty "<<endl;
     for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<lefty[i][j]<<" ";
          cout<<endl;
      }
      cout<<" right y "<<endl<<endl;

       for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<righty[i][j]<<" ";
          cout<<endl;
      }*/

      cout<<anss<<endl;

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a)
#define pl(a) printf("%lld\n",a)
#define pi(a) printf("%d\n",a)
#define pd(a) printf("%lf\n",a)
#define pf(a) printf("%f\n",a)
#define lc (start+end)/2
#define rc  lc+1
#define INT_MAX 100000
 int  lefty[1000][1000];
  int  righty[1000][1000];
  char arr[1000][10000];
int main()
 {
    int n,m;
    cin>>n>>m;
     {
         for(int i=0;i<n;i++)
          {
             for(int j=0;j<m;j++)
              {
                  lefty[i][j]=INT_MAX;
                    righty[i][j]=INT_MAX;
                  }
            }
      }

    for(int i=0;i<n;i++) cin>>arr[i];

    for(int i=0;i<n;i++)
       {
         int c=0;
         for(int j=0;j<m;j++)
          {
             if(arr[i][j]=='1') c++;
             }
             if(c==0)
              {
                 cout<<"-1"<<endl;
                 exit(0);
              }
       }

     for(int i=0;i<n;i++)
      {
         int lt=INT_MAX;
          if(arr[i][0]=='0')
           {
             //cout<<" prob "<<i<< "0 "<<endl;
             for(int j=m-1;j>=0;j--)
              {
                 if(arr[i][j]=='1')
                  {
                    lefty[i][0]=m-j;
                    break;
                          }
                          else
                          lefty[i][j]=INT_MAX;
                    }
              }
              else lefty[i][0]=0;
              for(int j=1;j<m;j++)
               {
                 if(arr[i][j]=='1') lefty[i][j]=0;
                 else lefty[i][j]=lefty[i][j-1]+1;
               }

       }

    for(int i=0;i<=n;i++)
      {

         int lt=INT_MAX;
          if(arr[i][m-1]=='0')
           {
         //      cout<<" prob right "<<i<<endl;
             for(int  j=0;j<m;j++)
              {
                 if(arr[i][j]=='1')
                  {
                    righty[i][m-1]=j+1;
                    // cout<<"set "<<righty[i][0]<<endl;
                    break;
                          }
                          else
                          righty[i][j]=INT_MAX;
                    }
              }
              else righty[i][m-1]=0;

              for(int j=m-2;j>=0;j--)
               {
                 if(arr[i][j]=='1') righty[i][j]=0;
                 else  righty[i][j]=righty[i][j+1]+1;
               }

       }

    int anss=INT_MAX;
    for(int i=0;i<m;i++)
     {
         int temp=0;
        for(int j=0;j<n;j++)
         {
             temp+=min(lefty[j][i],righty[j][i]);
          }
          anss=min(anss,temp);
           //cout<<anss<<endl;
     }
    /* cout<<"lefty "<<endl;
     for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<lefty[i][j]<<" ";
          cout<<endl;
      }
      cout<<" right y "<<endl<<endl;

       for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<righty[i][j]<<" ";
          cout<<endl;
      }*/

      cout<<anss<<endl;

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a)
#define pl(a) printf("%lld\n",a)
#define pi(a) printf("%d\n",a)
#define pd(a) printf("%lf\n",a)
#define pf(a) printf("%f\n",a)
#define lc (start+end)/2
#define rc  lc+1
#define INT_MAX 100000000
 int  lefty[102][10200];
  int  righty[102][10200];
  char arr[102][10200];

int main()
 {
    int n,m;
    cin>>n>>m;
     {
         for(int i=0;i<n;i++)
          {
             for(int j=0;j<m;j++)
              {
                  lefty[i][j]=INT_MAX;
                    righty[i][j]=INT_MAX;
                  }
            }
      }

    for(int i=0;i<n;i++) cin>>arr[i];

    for(int i=0;i<n;i++)
       {
         int c=0;
         for(int j=0;j<m;j++)
          {
             if(arr[i][j]=='1') c++;
             }
             if(c==0)
              {
                 cout<<"-1"<<endl;
                 exit(0);
              }
       }

     for(int i=0;i<n;i++)
      {
         int lt=INT_MAX;
          if(arr[i][0]=='0')
           {
             //cout<<" prob "<<i<< "0 "<<endl;
             for(int j=m-1;j>=0;j--)
              {
                 if(arr[i][j]=='1')
                  {
                    lefty[i][0]=m-j;
                    break;
                          }
                          else
                          lefty[i][j]=INT_MAX;
                    }
              }
              else lefty[i][0]=0;
              for(int j=1;j<m;j++)
               {
                 if(arr[i][j]=='1') lefty[i][j]=0;
                 else lefty[i][j]=lefty[i][j-1]+1;
               }

       }

    for(int i=0;i<=n;i++)
      {

         int lt=INT_MAX;
          if(arr[i][m-1]=='0')
           {
         //      cout<<" prob right "<<i<<endl;
             for(int  j=0;j<m;j++)
              {
                 if(arr[i][j]=='1')
                  {
                    righty[i][m-1]=j+1;
                    // cout<<"set "<<righty[i][0]<<endl;
                    break;
                          }
                          else
                          righty[i][j]=INT_MAX;
                    }
              }
              else righty[i][m-1]=0;

              for(int j=m-2;j>=0;j--)
               {
                 if(arr[i][j]=='1') righty[i][j]=0;
                 else  righty[i][j]=righty[i][j+1]+1;
               }

       }

    int anss=INT_MAX;
    for(int i=0;i<m;i++)
     {
         int temp=0;
        for(int j=0;j<n;j++)
         {
             temp+=min(lefty[j][i],righty[j][i]);
          }
          anss=min(anss,temp);
           //cout<<anss<<endl;
     }
    /* cout<<"lefty "<<endl;
     for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<lefty[i][j]<<" ";
          cout<<endl;
      }
      cout<<" right y "<<endl<<endl;

       for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<righty[i][j]<<" ";
          cout<<endl;
      }*/

      cout<<anss<<endl;

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a)
#define pl(a) printf("%lld\n",a)
#define pi(a) printf("%d\n",a)
#define pd(a) printf("%lf\n",a)
#define pf(a) printf("%f\n",a)
#define lc (start+end)/2
#define rc  lc+1
#define INT_MAX 100000
 int  lefty[1000][1000];
  int  righty[1000][1000];
  char arr[1000][10000];
int main()
 {
    int n,m;
    cin>>n>>m;
     {
         for(int i=0;i<n;i++)
          {
             for(int j=0;j<m;j++)
              {
                  lefty[i][j]=INT_MAX;
                    righty[i][j]=INT_MAX;
                  }
            }
      }

    for(int i=0;i<n;i++) cin>>arr[i];

     for(int i=0;i<n;i++)
      {
         int lt=INT_MAX;
          if(arr[i][0]=='0')
           {
             //cout<<" prob "<<i<< "0 "<<endl;
             for(int j=m-1;j>=0;j--)
              {
                 if(arr[i][j]=='1')
                  {
                    lefty[i][0]=m-j;
                    break;
                          }
                          else
                          lefty[i][j]=INT_MAX;
                    }
              }
              else lefty[i][0]=0;
              for(int j=1;j<m;j++)
               {
                 if(arr[i][j]=='1') lefty[i][j]=0;
                 else lefty[i][j]=lefty[i][j-1]+1;
               }

       }

    for(int i=0;i<=n;i++)
      {

         int lt=INT_MAX;
          if(arr[i][m-1]=='0')
           {
         //      cout<<" prob right "<<i<<endl;
             for(int  j=0;j<m;j++)
              {
                 if(arr[i][j]=='1')
                  {
                    righty[i][m-1]=j+1;
                    // cout<<"set "<<righty[i][0]<<endl;
                    break;
                          }
                          else
                          righty[i][j]=INT_MAX;
                    }
              }
              else righty[i][m-1]=0;

              for(int j=m-2;j>=0;j--)
               {
                 if(arr[i][j]=='1') righty[i][j]=0;
                 else  righty[i][j]=righty[i][j+1]+1;
               }

       }

    int anss=INT_MAX;
    for(int i=0;i<m;i++)
     {
         int temp=0;
        for(int j=0;j<n;j++)
         {
             temp=max(temp,max(lefty[j][i],righty[j][i]));
          }
          anss=min(anss,temp);
           //cout<<anss<<endl;
     }
    /* cout<<"lefty "<<endl;
     for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<lefty[i][j]<<" ";
          cout<<endl;
      }
      cout<<" right y "<<endl<<endl;
       for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<righty[i][j]<<" ";
          cout<<endl;
      }*/
      if(anss<m)
      cout<<anss<<endl;
      else cout<<"-1"<<endl;

 }


#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a)
#define pl(a) printf("%lld\n",a)
#define pi(a) printf("%d\n",a)
#define pd(a) printf("%lf\n",a)
#define pf(a) printf("%f\n",a)
#define lc (start+end)/2
#define rc  lc+1
#define INT_MAX 100000000
 int  lefty[10200][10200];
  int  righty[10200][10200];
  char arr[10200][10200];

int main()
 {
    int n,m;
    cin>>n>>m;
     {
         for(int i=0;i<n;i++)
          {
             for(int j=0;j<m;j++)
              {
                  lefty[i][j]=INT_MAX;
                    righty[i][j]=INT_MAX;
                  }
            }
      }

    for(int i=0;i<n;i++) cin>>arr[i];

    for(int i=0;i<n;i++)
       {
         int c=0;
         for(int j=0;j<m;j++)
          {
             if(arr[i][j]=='1') c++;
             }
             if(c==0)
              {
                 cout<<"-1"<<endl;
                 exit(0);
              }
       }

     for(int i=0;i<n;i++)
      {
         int lt=INT_MAX;
          if(arr[i][0]=='0')
           {
             //cout<<" prob "<<i<< "0 "<<endl;
             for(int j=m-1;j>=0;j--)
              {
                 if(arr[i][j]=='1')
                  {
                    lefty[i][0]=m-j;
                    break;
                          }
                          else
                          lefty[i][j]=INT_MAX;
                    }
              }
              else lefty[i][0]=0;
              for(int j=1;j<m;j++)
               {
                 if(arr[i][j]=='1') lefty[i][j]=0;
                 else lefty[i][j]=lefty[i][j-1]+1;
               }

       }

    for(int i=0;i<=n;i++)
      {

         int lt=INT_MAX;
          if(arr[i][m-1]=='0')
           {
         //      cout<<" prob right "<<i<<endl;
             for(int  j=0;j<m;j++)
              {
                 if(arr[i][j]=='1')
                  {
                    righty[i][m-1]=j+1;
                    // cout<<"set "<<righty[i][0]<<endl;
                    break;
                          }
                          else
                          righty[i][j]=INT_MAX;
                    }
              }
              else righty[i][m-1]=0;

              for(int j=m-2;j>=0;j--)
               {
                 if(arr[i][j]=='1') righty[i][j]=0;
                 else  righty[i][j]=righty[i][j+1]+1;
               }

       }

    int anss=INT_MAX;
    for(int i=0;i<m;i++)
     {
         int temp=0;
        for(int j=0;j<n;j++)
         {
             temp+=min(lefty[j][i],righty[j][i]);
          }
          anss=min(anss,temp);
           //cout<<anss<<endl;
     }
    /* cout<<"lefty "<<endl;
     for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<lefty[i][j]<<" ";
          cout<<endl;
      }
      cout<<" right y "<<endl<<endl;

       for(int i=0;i<n;i++)
      {
         for(int j=0;j<m;j++)
         cout<<righty[i][j]<<" ";
          cout<<endl;
      }*/

      cout<<anss<<endl;

 }


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}

int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
/*
int n,m;
vector < vector < int > > v;
vector < bool >  visited;
char a[100][100];
int visit[10000];
int dfs(int node,int last)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        {
            if(!visit[v[node][i]])
              return dfs(v[node][i],node);
            else if(v[node][i] != last)
                return 1;
        }
        return 0;
}

int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
*/
int main()
{

 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
            b[i][j] = 100000000;
            int pos = lower_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            pos = c[i][pos];
            b[i][j] = abs(j - pos);
            pos = upper_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            pos = c[i][pos];
            b[i][j] = min(abs(j - pos),b[i][j]);
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
        b[i][j] = 100000000;
        for(int k=0;k<c[i].size();k++)
        {
            b[i][j] = min(b[i][j] , abs(j - c[i][k]));
        }
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    c[i].push_back(c[i][0]+m);
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{

    int x = 0 ,y = 1 , F , L;
    F = c[i][x]; L = c[i][y];
    for(int j=0;j<m;j++)
    {
        b[i][j] = min(abs(j-F),abs(j-L));
        if(L == j)
        {
            if(c[i].size() > y)
            {
                x++;
                y++;
            }
        }
    }

}
int minx = 1000000;
for(int j=0;j<m;j++)
{
    int sum = 0;
    for(int i=0;i<n;i++)
        sum+=b[i][j];
    minx = min(minx,sum);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}

int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
/*
int n,m;
vector < vector < int > > v;
vector < bool >  visited;
char a[100][100];
int visit[10000];
int dfs(int node,int last)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        {
            if(!visit[v[node][i]])
              return dfs(v[node][i],node);
            else if(v[node][i] != last)
                return 1;
        }
        return 0;
}

int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
*/
int main()
{

 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
            b[i][j] = 100000000;
            int pos = lower_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            b[i][j] = min(abs(j - c[i][pos]),abs(j - c[i][pos-1]));
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}

int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
/*
int n,m;
vector < vector < int > > v;
vector < bool >  visited;
char a[100][100];
int visit[10000];
int dfs(int node,int last)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        {
            if(!visit[v[node][i]])
              return dfs(v[node][i],node);
            else if(v[node][i] != last)
                return 1;
        }
        return 0;
}

int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
*/
int main()
{

 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
        b[i][j] = 100000000;
        for(int k=0;k<c[i].size();k++)
        {
            int pos = lower_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            pos = c[i][pos];
            b[i][j] = abs(j - pos);
            pos = upper_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            pos = c[i][pos];
            b[i][j] = min(abs(j - pos),b[i][j]);
        }
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{

    int x = 0 ,y = 0;
    if(c[i].size() > 1)
         y = 1;
    for(int j=0;j<m;j++)
    {
        b[i][j] = min(abs(j-c[i][x]),abs(j-c[i][y]));
        if(c[i][y] == j && c[i].size() > y)
        {
            x++;
            y++;
        }
    }

}
int minx = 1000000;
for(int j=0;j<m;j++)
{
    int sum = 0;
    for(int i=0;i<n;i++)
        sum+=b[i][j];
    minx = min(minx,sum);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,-1,sizeof(b));
for(int i=0;i<n;i++)
{
    int x = 0;
    for(int j=0;j<m;j++)
    {
        if(a[i][j] == '1' && j!= c[i][x])
            x++;
        if(b[i][j]==-1)
            b[i][j] = 100000000;
        b[i][j] = min(b[i][j] , abs(c[i][x] - j));
    }
    x = c[i].size()-1;
    for(int j=0;j<m;j++)
    {
        if(a[i][j] == '1' && j!= c[i][x])
            x--;
        if(b[i][j]==-1)
            b[i][j] = 100000000;
        b[i][j] = min(b[i][j] , abs(c[i][x] - j));
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    c[i].push_back(c[i][0]+n);
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{

    int x = 0 ,y = 0 , F , L;
    if(c[i].size() > 1)
         y = 1;
    F = c[i][x]; L = c[i][y];
    for(int j=0;j<m;j++)
    {
        b[i][j] = min(abs(j-F),abs(j-L));
        if(L == j)
        {
            if(c[i].size() > y)
            {
                x++;
                y++;
            }
        }
    }

}
int minx = 1000000;
for(int j=0;j<m;j++)
{
    int sum = 0;
    for(int i=0;i<n;i++)
        sum+=b[i][j];
    minx = min(minx,sum);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size()>0)
    c[i].push_back(c[i][0]+m);
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{

    int x = 0 ,y = 1 , F , L;
    F = c[i][x]; L = c[i][y];
    for(int j=0;j<m;j++)
    {
        b[i][j] = min(abs(j-F),abs(j-L));
        if(L == j)
        {
            if(c[i].size() > y)
            {
                x++;
                y++;
            }
        }
    }

}
int minx = 1000000;
for(int j=0;j<m;j++)
{
    int sum = 0;
    for(int i=0;i<n;i++)
        sum+=b[i][j];
    minx = min(minx,sum);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}

int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
/*
int n,m;
vector < vector < int > > v;
vector < bool >  visited;
char a[100][100];
int visit[10000];
int dfs(int node,int last)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        {
            if(!visit[v[node][i]])
              return dfs(v[node][i],node);
            else if(v[node][i] != last)
                return 1;
        }
        return 0;
}

int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
*/
int main()
{

 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size() > 0)
        {
            c[i].push_back(c[i][0]+m);
            int Z = c[i].size()-2;
            c[i].push_back(c[i][Z]-m);
            sort(c[i].begin(),c[i].end());
        }
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
for(int i=0;i<n;i++)
{
    for(int j=0;j<m;j++)
    {
            b[i][j] = 100000000;
            int pos = lower_bound(c[i].begin(),c[i].end(),j)-c[i].begin();
            b[i][j] = min(abs(j - c[i][pos]),abs(j - c[i][pos-1]));
    }
}
int minx = 1000000000;
for(int i=0;i<m;i++)
{
    int Y = 0;
    for(int j=0;j<n;j++)
        Y+=b[j][i];
    minx = min(minx,Y);
}
cout<<minx;
}


/* fflush(stdout);  */
#include <iostream>
#include <stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define fr first
#define sc second
#include <sstream>
#define ii pair < int , int >
using namespace std;
int Mod(int x)
{
    return (x%1000000007 + 1000000007)%1000000007;
}
ll gcd(ll a,ll b)
{
    return (b==0 ? a : gcd(b,a%b));
}
ll lcm(ll a,ll b)
{
    return(a*(b/gcd(a,b)));
}
ll bigmod(ll b,ll p ,ll m)
{
    if(p==0)
        return 1;
    if(p%2 == 0)
    {
        ll temp = bigmod(b,p/2,m);
        return (temp*temp)%m;
    }
    return (bigmod(b,p-1,m)*(b%m))%m;
}
char inp[10010];
string scan()
{
    scanf("%s", inp);
    string s;
    for (int i = 0; inp[i]; i++)
        s += inp[i];
    return s;
}
vector < vector < int > > v;
vector < bool >  visited;
int visit[100];
void dfs(int node)
{
    visit[node] = 1;
    for(int i=0 ; i<(int)v[node].size(); i++)
        if(!visit[v[node][i]])
            dfs(v[node][i]);
}
int connectedcomponenetscnt(int head)
{
    int cnt = 0;
    for(int i=1; i<500; i++)
    {
        if(!visit[i])
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int com(string t,string s)
{
    string a,b;
    a = s+t;
    b = t+s;
    for(int i=0; i<a.length(); i++)
    {
        if(a[i] > b[i])
            return 1;
        if(a[i] < b[i])
            return 0;
    }
    return 0;
}
int main()
{
 int n,m;
 cin>>n>>m;
 string a[n];
 vector < vector < int > > c;
 c.resize(n);
 for(int i=0;i<n;i++)
 {
     string s = scan();
     a[i] = s;
     for(int j=0;j<m;j++)
        if(a[i][j] == '1')
          c[i].push_back(j);
    if(c[i].size()>0)
    c[i].push_back(c[i][0]+m);
 }
 for(int i=0;i<n;i++)
    if(c[i].size() == 0)
     {
         cout<<-1;
         return 0;
     }
int b[n][m];
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
{

    int x = 0 ,y = 1 , F , L;
    F = c[i][x]; L = c[i][y];
    for(int j=0;j<m;j++)
    {
        b[i][j] = min(min(abs(j-F),abs(m-abs(j-F))),min(abs(j-L),abs(m-abs(j-L))));
        if(L == j)
        {
            if(c[i].size() > y)
            {
                x++;
                y++;
            }
        }
    }

}
int minx = 1000000;
for(int j=0;j<m;j++)
{
    int sum = 0;
    for(int i=0;i<n;i++)
        sum+=b[i][j];
    minx = min(minx,sum);
}
cout<<minx;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <deque>
#include <queue>
#include <stack>

#define sc scanf
#define pr printf
#define fi first
#define se second
#define pb push_back
#define p_b pop_back
#define pf push_front
#define p_f pop_front
#define mp make_pair
#define lli long long int
#define p_q priority_queue
#define fin freopen( "INPUT.TXT", "r", stdin );
#define fout freopen( "output.txt", "w", stdout );
#define N 100000
#define dist( x, xx, y, yy ) sqrt( (x - y) * (x - y) + (xx - yy) * (xx - yy) )
using namespace std;
int a,b,i,j,k,l,dp[101][10001],dp1[101][10001],ans=99999999,x[101],y[101];
bool used[101];
char c[101][10001];

int main()
{
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        for( j=1; j<=b; j++ ){
            cin>>c[i][j];
            if( !x[i] && c[i][j]=='1' )x[i]=j;
            if( c[i][j]=='1' )y[i]=j;
        }
        if( !x[i] ){
            cout<<"-1";
            return 0;
        }
    }
    for( i=1; i<=a; i++ ){
        for( j=x[i]; j<=b; j++ ){
            if( c[i][j]=='1' )continue;
            dp[i][j]=dp[i][j-1]+1;
        }
        if( x[i]!=1 ){
            dp[i][1]=dp[i][b]+1;
            for( j=2; j<x[i]; j++ )dp[i][j]=dp[i][j-1]+1;
        }
    }
    for( i=1; i<=a; i++ ){
        for( j=y[i]; j>=1; j-- ){
            if( c[i][j]=='1' )continue;
            dp1[i][j]=dp1[i][j+1]+1;
        }
        if( y[i]!=b ){
            dp1[i][b]=dp1[i][1]+1;
            for( j=b-1; j>y[i]; j-- )dp1[i][j]=dp1[i][j+1]+1;
        }
    }
    for( i=1; i<=b; i++ ){
        int cnt=0;
        for( j=1; j<=a; j++ ){
            cnt+=min(dp[j][i],dp1[j][i]);
        }
        ans=min(ans,cnt);
    }
    cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <deque>
#include <queue>
#include <stack>

#define sc scanf
#define pr printf
#define fi first
#define se second
#define pb push_back
#define p_b pop_back
#define pf push_front
#define p_f pop_front
#define mp make_pair
#define lli long long int
#define p_q priority_queue
#define fin freopen( "INPUT.TXT", "r", stdin );
#define fout freopen( "output.txt", "w", stdout );
#define N 100000
#define dist( x, xx, y, yy ) sqrt( (x - y) * (x - y) + (xx - yy) * (xx - yy) )
using namespace std;
int a,b,i,j,k,l,dp[101][10001],dp1[101][10001],ans=99999999;
bool used[101];
char c[101][10001];

int main()
{
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        for( j=1; j<=b; j++ ){
            cin>>c[i][j];
        }
        if( c[i][b]=='1' && c[i][1]=='0' )dp[i][1]=1;
        if( c[i][1]=='1' && c[i][b]=='0' )dp1[i][b]=1;
    }
    for( i=1; i<=a; i++ ){
        for( j=1; j<=b; j++ ){
            if( c[i][j]=='1' )used[i]=true;
            if( j>1 && c[i][j-1]=='1' && c[i][j]!='1' )dp[i][j]=1;
            else if( j>1 && dp[i][j-1]>0 && c[i][j]!='1' )dp[i][j]=dp[i][j-1]+1;
        }
        if( c[i][b]=='1' && c[i][1]=='0' )dp[i][1]=1;
    }
    for( i=1; i<=a; i++ ){
        for( j=b; j>=1; j-- ){
            if( c[i][j+1]=='1' && c[i][j]!='1' )dp1[i][j]=1;
            else if( dp1[i][j+1]>0 && c[i][j]!='1' )dp1[i][j]=dp1[i][j+1]+1;
        }
    }
    for( i=1; i<=b; i++ ){
        int cnt=0;
        for( j=1; j<=a; j++ ){
            if( dp[j][i]>0 && dp1[j][i]>0 )cnt+=min(dp[j][i],dp1[j][i]);
            else if( dp[j][i]>0 )cnt+=dp[j][i];
            else if( dp1[j][i]>0 )cnt+=dp1[j][i];
        }
        ans=min(ans,cnt);
    }
    for( i=1; i<=a; i++ ){
        if( !used[i] ){
            cout<<-1;
            return 0;
        }
    }
    cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <deque>
#include <queue>
#include <stack>

#define sc scanf
#define pr printf
#define fi first
#define se second
#define pb push_back
#define p_b pop_back
#define pf push_front
#define p_f pop_front
#define mp make_pair
#define lli long long int
#define p_q priority_queue
#define fin freopen( "INPUT.TXT", "r", stdin );
#define fout freopen( "output.txt", "w", stdout );
#define N 100000
#define dist( x, xx, y, yy ) sqrt( (x - y) * (x - y) + (xx - yy) * (xx - yy) )
using namespace std;
int main()
{
    int a,b[10001],i,k;
    cin>>a>>k;
    a=a*2+1;
    for( i=1; i<=a; i++ ) cin>>b[i];
    b[0]=b[a+1]=b[2];
    for( i=1; i<=a; i+=2 ){
        if( k && b[i]>=b[i-1] || b[i]>=b[i+1] ){
            b[i]--;
            k--;
        }
    }
    for( i=2; i<=a; i+=2 ){
        if( k && abs(b[i]-b[i-1])>1 && abs(b[i]-b[i+1])>1 ){
            b[i]--;
            k--;
        }
    }
    for( i=1; i<=a; i++ )cout<<b[i]<<" ";
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <deque>
#include <queue>
#include <stack>

#define sc scanf
#define pr printf
#define fi first
#define se second
#define pb push_back
#define p_b pop_back
#define pf push_front
#define p_f pop_front
#define mp make_pair
#define lli long long int
#define p_q priority_queue
#define fin freopen( "INPUT.TXT", "r", stdin );
#define fout freopen( "output.txt", "w", stdout );
#define N 100000
#define dist( x, xx, y, yy ) sqrt( (x - y) * (x - y) + (xx - yy) * (xx - yy) )
using namespace std;
int a,b,i,j,k,l,dp[101][10001],dp1[101][10001],ans=99999999;
bool used[101];
char c[101][10001];

int main()
{
    cin>>a>>b;
    for( i=1; i<=a; i++ ){
        for( j=1; j<=b; j++ ){
            cin>>c[i][j];
            if( c[i][j]=='1' )used[i]=true;
            if( j>1 && c[i][j-1]=='1' && c[i][j]!='1' )dp[i][j]=1;
            else if( j>1 && dp[i][j-1]>0 && c[i][j]!='1' )dp[i][j]=dp[i][j-1]+1;
        }
    }
    for( i=1; i<=a; i++ ){
        for( j=b; j>=1; j-- ){
            if( c[i][j+1]=='1' && c[i][j]!='1' )dp1[i][j]=1;
            else if( dp1[i][j+1]>0 && c[i][j]!='1' )dp1[i][j]=dp1[i][j+1]+1;
        }
    }
    for( i=1; i<=b; i++ ){
        int cnt=0;
        for( j=1; j<=a; j++ ){
            if( dp[j][i]>0 && dp1[j][i]>0 )cnt+=min(dp[j][i],dp1[j][i]);
            else if( dp[j][i]>0 )cnt+=dp[j][i];
            else if( dp1[j][i]>0 )cnt+=dp1[j][i];
        }
        ans=min(ans,cnt);
    }
    for( i=1; i<=a; i++ ){
        if( !used[i] ){
            cout<<-1;
            return 0;
        }
    }
    cout<<ans;
}


#include<iostream>
#include<vector>
#include<string>

#define inf 1000000000

using namespace std;

int main () {
    int n, m; cin >> n >> m;
    vector<string> table(n);
    for (int i = 0; i < n; i++)
        cin >> table[i];
    vector< vector<int> > count(n, vector<int>(m, inf));
    for (int i = 0; i < n; i++) {
        int last = inf;
        for (int j = 0; j < m; j++) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
                count[i][j] = min(j - last, last + m - j);
        }
        last = inf;
        for (int j = m - 1; j >= 0; j--) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
               count[i][j] = min(last - j, m - last + j);
        }
    }
    int result = inf;
    for (int j = 0; j < m; j++) {
        int candidate = 0;
        for (int i = 0; i < n; i++)
            if (count[i][j] == inf) {
                candidate = inf;
                break;
            } else candidate += count[i][j];
        result = min(result, candidate);
    }
    if (result == inf) cout << "-1";
    else cout << result;
    return 0;
}


#include<iostream>
#include<vector>
#include<string>

#define inf 2000000000

using namespace std;

int main () {
    int n, m; cin >> n >> m;
    vector<string> table(n);
    for (int i = 0; i < n; i++)
        cin >> table[i];
    vector< vector<int> > count(n, vector<int>(m, inf));
    for (int i = 0; i < n; i++) {
        int last = inf;
        for (int j = 0; j < m; j++) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
                count[i][j] = min(j - last, last + m - j);
        }
        last = inf;
        for (int j = m - 1; j >= 0; j--) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
               count[i][j] = min(count[i][j], min(last - j, m - last + j));
        }
    }
    int result = inf;
    for (int j = 0; j < m; j++) {
        int candidate = 0;
        for (int i = 0; i < n; i++)
            if (count[i][j] == inf) {
                candidate = inf;
                break;
            } else candidate += count[i][j];
        result = min(result, candidate);
    }
    if (result == inf) cout << "-1";
    else cout << result;
    return 0;
}


#include<iostream>
#include<vector>
#include<string>

#define inf 1000000000

using namespace std;

int main () {
    int n, m; cin >> n >> m;
    vector<string> table(n);
    for (int i = 0; i < n; i++)
        cin >> table[i];
    vector< vector<int> > count(n, vector<int>(m, inf));
    for (int i = 0; i < n; i++) {
        int last = inf;
        for (int j = 0; j < m; j++) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
                count[i][j] = min(j - last, last + m - j);
        }
        last = inf;
        for (int j = m - 1; j >= 0; j--) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
               count[i][j] = min(count[i][j], min(last - j, m - last + j));
        }
    }
    int result = inf;
    for (int j = 0; j < m; j++) {
        int candidate = 0;
        for (int i = 0; i < n; i++)
            if (count[i][j] == inf) {
                candidate = inf;
                break;
            } else candidate += count[i][j];
        result = min(result, candidate);
    }
    if (result == inf) cout << "-1";
    else cout << result;
    return 0;
}


#include<iostream>
#include<vector>
#include<string>

#define inf 2000000000

using namespace std;

void relax(int& target, int candidate) {
    target = min(target, candidate);
}

int main () {
    int n, m; cin >> n >> m;
    vector<string> table(n);
    for (int i = 0; i < n; i++)
        cin >> table[i];
    vector< vector<int> > count(n, vector<int>(m, inf));
    for (int i = 0; i < n; i++) {
        int first = inf, last = inf;
        for (int j = 0; j < m; j++) {
            if (table[i][j] == '1') {
                if (first == inf) first = j;
                last = j;
            }
            if (first != inf)
                relax(count[i][j], min(j - first, first + m - j)),
                relax(count[i][j], min(j - last, last + m - j));
        }
        first = inf, last = inf;
        for (int j = m - 1; j >= 0; j--) {
            if (table[i][j] == '1') {
                if (first == inf) first = j;
                last = j;
            }
            if (first != inf)
               relax(count[i][j], min(first - j, m - first + j)),
               relax(count[i][j], min(last - j, m - last + j));
        }
    }
    int result = inf;
    for (int j = 0; j < m; j++) {
        int candidate = 0;
        for (int i = 0; i < n; i++)
            if (count[i][j] == inf) {
                candidate = inf;
                break;
            } else candidate += count[i][j];
        result = min(result, candidate);
    }
    if (result == inf) cout << "-1";
    else cout << result;
    return 0;
}


#include<iostream>
#include<vector>
#include<string>

#define inf 1000000000

using namespace std;

int main () {
    int n, m; cin >> n >> m;
    vector<string> table(n);
    for (int i = 0; i < n; i++)
        cin >> table[i];
    vector< vector<int> > count(n, vector<int>(m, inf));
    for (int i = 0; i < n; i++) {
        int last = inf;
        for (int j = 0; j < m; j++) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
                count[i][j] = min(j - last, last + m - j);
        }
        last = inf;
        for (int j = m - 1; j >= 0; j--) {
            if (table[i][j] == '1') last = j;
            if (last != inf)
               count[i][j] = min(last - j, m - last + j);
        }
    }
    int result = inf;
    for (int j = 0; j < m; j++) {
        int candidate = 0;
        for (int i = 0; i < n; i++)
            candidate += count[i][j];
        result = min(result, candidate);
    }
    if (result == inf) cout << "-1";
    else cout << result;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10005
#define INF 2147483647
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
int n,m;
char s[M];
int mi=INF,sum[M],l[M],r[M];
void find(){
    for(int i=1;i<=m;i++)
        if(s[i]=='1') {r[m+1]=i-1;break;}
    for(int i=m;i>0;i--)
        if(s[i]=='1') {l[0]=m-i;break;}
    for(int i=m;i>0;i--){
        if(s[i]=='1') r[i]=0;
        else r[i]=r[i+1]+1;
    }
    for(int i=1;i<=m;i++){
        if(s[i]=='1') l[i]=0;
        else l[i]=l[i-1]+1;
        sum[i]+=min(l[i],r[i]);
    }
}
int main(){
    memset(sum,0,sizeof(sum));
    cin>>n>>m;
    bool jud=1;
    for(int i=0;i<n;i++){
        scanf("%s",s+1);
        if(jud){
            bool tmp=0;
            for(int i=1;i<=m;i++) if(s[i]=='1') {tmp=1;break;}
            if(!tmp) jud=0;
            if(jud) find();
        }
    }
    if(jud) for(int i=1;i<=m;i++) mi=min(sum[i],mi);
    else mi=-1;
    cout<<mi<<endl;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10005
#define INF 2147483647
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
int n,m;
char s[M];
int mi=INF,sum[M]={0},l[M],r[M];
void find(){
    for(int i=1;i<=m;i++)
        if(s[i]=='1') {r[m+1]=i-1;break;}
    for(int i=m;i>0;i--)
        if(s[i]=='1') {l[0]=m-i;break;}
    for(int i=m;i>0;i--){
        if(s[i]=='1') r[i]=0;
        else r[i]=r[i+1]+1;
    }
    for(int i=1;i<=m;i++){
        if(s[i]=='1') l[i]=0;
        else l[i]=l[i-1]+1;
        sum[i]+=min(l[i],r[i]);
    }
}
int main(){
    cin>>n>>m;
    bool jud=1;
    for(int i=0;i<n;i++){
        scanf("%s",s+1);
        if(jud){
            for(int i=1;i<=m;i++) if(s[i]=='1') {jud=0;break;}
            if(jud) find();
        }
    }
    if(jud) for(int i=1;i<=m;i++) mi=min(sum[i],mi);
    else mi=-1;
    cout<<mi<<endl;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10005
#define INF 2147483647
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
int n,m;
char s[M];
int dp[M],mi=INF,sum[M]={0},l[M],r[M];
void find(){
    for(int i=1;i<=m;i++)
        if(s[i]=='1') {r[m+1]=i;break;}
    for(int i=m;i>0;i--)
        if(s[i]=='1') {l[0]=m-i;break;}
    for(int i=m;i>0;i--){
        if(s[i]=='1') r[i]=0;
        else r[i]=r[i+1]+1;
        if(r[i]>m) r[i]-=m;
    }
    for(int i=1;i<=m;i++){
        if(s[i]=='1') l[i]=0;
        else l[i]=l[i-1]+1;
        if(l[i]>m) l[i]-=m;
        dp[i]=min(l[i],r[i]);
    }
}
int main(){
    cin>>n>>m;
    bool jud=1;
    for(int i=0;i<n;i++){
        scanf("%s",s+1);
        if(jud){
            bool tmp=0;
            for(int i=1;i<=m;i++) if(s[i]=='1') {tmp=1;break;}
            if(!tmp) jud=0;
            if(jud) {
                find();
                for(int i=1;i<=m;i++) sum[i]+=dp[i];
            }
        }
    }
    if(jud) for(int i=1;i<=m;i++) mi=min(sum[i],mi);
    else mi=-1;
    cout<<mi<<endl;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10005
#define INF 2147483647
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
int n,m;
char s[M];
int dp[M],mi=INF,sum[M]={0},l[M],r[M];
void find(){
    for(int i=1;i<=m;i++)
        if(s[i]=='1') {r[m+1]=i;break;}
    for(int i=m;i>=0;i--)
        if(s[i]=='1') {l[0]=m+1-i;break;}
    for(int i=m;i>0;i--){
        if(s[i]=='1') r[i]=0;
        else r[i]=r[i+1]+1;
        if(r[i]>m) r[i]-=m;
    }
    for(int i=1;i<=m;i++){
        if(s[i]=='1') l[i]=0;
        else l[i]=l[i-1]+1;
        if(l[i]>m) l[i]-=m;
        dp[i]=min(l[i],r[i]);
    }
}
int main(){
    cin>>n>>m;
    bool jud=1;
    for(int i=0;i<n;i++){
        scanf("%s",s+1);
        if(jud){
            bool tmp=0;
            for(int i=1;i<=m;i++) if(s[i]=='1') {tmp=1;break;}
            if(!tmp) jud=0;
            if(jud) {
                find();
                for(int i=1;i<=m;i++) sum[i]+=dp[i];
            }
        }
    }
    if(jud) for(int i=1;i<=m;i++) mi=min(sum[i],mi);
    else mi=-1;
    cout<<mi<<endl;
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#define M 10005
#define INF 2147483647
using namespace std;
inline int min(int a,int b){return a<b?a:b;}
int n,m;
char s[M];
int dp[M],mi=INF,sum[M]={0},l[M],r[M];
void find(){
    for(int i=1;i<=m;i++)
        if(s[i]=='1') {r[m+1]=i;break;}
    for(int i=m;i>0;i--)
        if(s[i]=='1') {l[0]=m-i;break;}
    for(int i=m;i>0;i--){
        if(s[i]=='1') r[i]=0;
        else r[i]=r[i+1]+1;
    }
    for(int i=1;i<=m;i++){
        if(s[i]=='1') l[i]=0;
        else l[i]=l[i-1]+1;
        dp[i]=min(l[i],r[i]);
    }
}
int main(){
    cin>>n>>m;
    bool jud=1;
    for(int i=0;i<n;i++){
        scanf("%s",s+1);
        if(jud){
            bool tmp=0;
            for(int i=1;i<=m;i++) if(s[i]=='1') {tmp=1;break;}
            if(!tmp) jud=0;
            if(jud) {
                find();
                for(int i=1;i<=m;i++) sum[i]+=dp[i];
            }
        }
    }
    if(jud) for(int i=1;i<=m;i++) mi=min(sum[i],mi);
    else mi=-1;
    cout<<mi<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>

#define X first
#define Y second

using namespace std;
int n, m, mas[110][10100], dp1[110][10100], dp2[110][10100], inf=1000000000;
char s[100100];
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    {
        scanf("%s", s);
        if ( s[0]=='0' )
        {
           dp1[i][0]=inf;
        }
        for (int j=1; j<m; j++)
        {
            if ( s[j]=='1' )
            {
               dp1[i][j]=0;
            }
            else
            {
               dp1[i][j]=dp1[i][j-1]+1;
            }
        }

        if ( s[m-1]=='0' )
        {
           dp2[i][m-1]=inf;
        }
        for (int j=m-2; j>=0; j--)
        {
            if ( s[j]=='1' )
            {
               dp2[i][j]=0;
            }
            else
            {
               dp2[i][j]=dp2[i][j+1]+1;
            }
        }
        for (int j=0; j<m; j++)
        {
            if ( dp1[i][j]<inf )
               dp1[i][j]=min(dp1[i][j],  m-dp1[i][j] );
            if ( dp2[i][j]<inf )
               dp2[i][j]=min(dp2[i][j],  m-dp2[i][j] );
            dp1[i][j]=min( dp1[i][j], dp2[i][j] );
            //dp1[i][j]=min( min(dp1[i][j], ( m-dp1[i][j] )  ), min(dp2[i][j], ( m-dp2[i][j] )  ) );
        }
    }
    int ans=inf, q;
    for (int i=0; i<m; i++)
    {
        q=0;
        for (int j=1; j<=n; j++)
        {
            if (dp1[j][i]>=inf )
               q=inf;
            else
               q+=dp1[j][i];
        }
        ans=min(ans, q);
    }
    if (ans==inf)
       cout<<-1<<endl;
    else
       cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>

#define X first
#define Y second

using namespace std;
int n, m, mas[110][10100], dp1[110][10100], dp2[110][10100], inf=1000000000;
char s[100100];
int first[110], last[110];
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    {
        scanf("%s", s);
        first[i]=inf;
        last[i]=-1;
        if ( s[0]=='0' )
        {
           dp1[i][0]=inf;
        }
        else
        {
           first[i]=0;
        }

        for (int j=1; j<m; j++)
        {
            if ( s[j]=='1' )
            {
               dp1[i][j]=0;
               first[i]=min(j, first[i]);
            }
            else
            {
               dp1[i][j]=dp1[i][j-1]+1;
            }
        }
        if ( s[m-1]=='0' )
        {
           dp2[i][m-1]=inf;
        }
        else
           last[i]=m-1;
        for (int j=m-2; j>=0; j--)
        {
            if ( s[j]=='1' )
            {
               dp2[i][j]=0;
               last[i]=max(j, last[i]);
            }
            else
            {
               dp2[i][j]=dp2[i][j+1]+1;
            }
        }

    }
    int ans=inf, q;
    for (int i=0; i<m; i++)
    {
        q=0;
        for (int j=1; j<=n; j++)
        {
            if ( last[j]==-1 )
               q=inf;
            else
            {
               int e;
               e=min( dp1[j][i], dp2[j][i] );
               if (last[j]>i)
                  e=min(e, m-last[j] + i);
               if (first[j]<i)
                  e=min(e, m-i+first[j]);
               q+=e;
            }
        }
        ans=min(ans, q);
    }
    if (ans==inf)
       cout<<-1<<endl;
    else
       cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>

#define X first
#define Y second

using namespace std;
int n, m, mas[110][10100], dp1[110][10100], dp2[110][10100], inf=1000000000;
char s[100100];
int first[110], last[110];
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    {
        scanf("%s", s);
        first[i]=inf;
        last[i]=-1;
        if ( s[0]=='0' )
        {
           dp1[i][0]=inf;
        }
        else
        {
           first[i]=0;
        }

        for (int j=1; j<m; j++)
        {
            if ( s[j]=='1' )
            {
               dp1[i][j]=0;
               first[i]=min(j, first[i]);
            }
            else
            {
               dp1[i][j]=dp1[i][j-1]+1;
            }
        }
        if ( s[m-1]=='0' )
        {
           dp2[i][m-1]=inf;
        }
        else
           last[i]=m-1;
        for (int j=m-2; j>=0; j--)
        {
            if ( s[j]=='1' )
            {
               dp2[i][j]=0;
               last[i]=max(j, last[i]);
            }
            else
            {
               dp2[i][j]=dp2[i][j+1]+1;
            }
        }

    }
    int ans=inf, q;
    for (int i=0; i<m; i++)
    {
        q=0;
        for (int j=1; j<=n; j++)
        {
            if ( last[i]==-1 )
               q=inf;
            else
            {
               int e;
               e=min( dp1[j][i], dp2[j][i] );
               if (last[j]>i && last[j]>-1)
                  e=min(e, last[j]-i);
               if (first[j]<i && first[j]<inf)
                  e=min(e, i-first[j]);
               q+=e;
            }
        }
        ans=min(ans, q);
    }
    if (ans==inf)
       cout<<-1<<endl;
    else
       cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>

#define X first
#define Y second

using namespace std;
int n, m, mas[110][10100], dp1[110][10100], dp2[110][10100], inf=1000000000;
char s[100100];
int first[110], last[110];
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    {
        scanf("%s", s);
        first[i]=inf;
        last[i]=-1;
        if ( s[0]=='0' )
        {
           dp1[i][0]=inf;
        }
        else
        {
           first[i]=0;
        }

        for (int j=1; j<m; j++)
        {
            if ( s[j]=='1' )
            {
               dp1[i][j]=0;
               first[i]=min(j, first[i]);
            }
            else
            {
               dp1[i][j]=dp1[i][j-1]+1;
            }
        }
        if ( s[m-1]=='0' )
        {
           dp2[i][m-1]=inf;
        }
        else
           last[i]=m-1;
        for (int j=m-2; j>=0; j--)
        {
            if ( s[j]=='1' )
            {
               dp2[i][j]=0;
               last[i]=max(j, last[i]);
            }
            else
            {
               dp2[i][j]=dp2[i][j+1]+1;
            }
        }

    }
    int ans=inf, q;
    for (int i=0; i<m; i++)
    {
        q=0;
        for (int j=1; j<=n; j++)
        {
            if ( last[i]==-1 )
               q=inf;
            else
            {
               int e;
               e=min( dp1[j][i], dp2[j][i] );
               if (last[j]>i)
                  e=min(e, m-last[j] + i);
               if (first[j]<i)
                  e=min(e, m-i+first[j]);
               q+=e;
            }
        }
        ans=min(ans, q);
    }
    if (ans==inf)
       cout<<-1<<endl;
    else
       cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>

#define X first
#define Y second

using namespace std;
int n, m, mas[110][10100], dp1[110][10100], dp2[110][10100], inf=1000000000;
char s[100100];
int main ()
{
    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    {
        scanf("%s", s);
        if ( s[0]=='0' )
        {
           dp1[i][0]=inf;
        }
        for (int j=1; j<m; j++)
        {
            if ( s[j]=='1' )
            {
               dp1[i][j]=0;
            }
            else
            {
               dp1[i][j]=dp1[i][j-1]+1;
            }
        }

        if ( s[m-1]=='0' )
        {
           dp2[i][m-1]=inf;
        }
        for (int j=m-2; j>=0; j--)
        {
            if ( s[j]=='1' )
            {
               dp2[i][j]=0;
            }
            else
            {
               dp2[i][j]=dp2[i][j+1]+1;
            }
        }
        for (int j=0; j<m; j++)
        {
            if ( dp1[i][j]<inf )
               dp1[i][j]=min(dp1[i][j],  m-dp1[i][j] );
            if ( dp2[i][j]<inf )
               dp2[i][j]=min(dp2[i][j],  m-dp2[i][j] );
            dp1[i][j]=min( dp1[i][j], dp2[i][j] );
            //dp1[i][j]=min( min(dp1[i][j], ( m-dp1[i][j] )  ), min(dp2[i][j], ( m-dp2[i][j] )  ) );
        }
    }
    int ans=inf, q;
    for (int i=0; i<m; i++)
    {
        q=0;
        for (int j=1; j<=n; j++)
        {
            if (dp1[j][i]==inf )
               q=inf;
            else
               q+=dp1[j][i];
        }
        ans=min(ans, q);
    }
    if (ans==inf)
       cout<<-1<<endl;
    else
       cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<complex>
#include<algorithm>

#define pb push_back
#define sz size()
#define clr clear()
#define EPS (1e-10)
#define INF 500000000
#define fs first
#define sc second

#define REP(i,a) for(int i=0;i<(int)(a);i++)
#define let(i,a) __typeof(a)i=(a)
#define all(a) (a).begin(),(a).end()
#define each(i,a) for(let(i,(a).begin());i!=(a).end();i++)
#define INIT(i) memset(i,0,sizeof(i))
#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
typedef complex<double> Po;

int n,m,prv;
string s[110];
int num[30100][110];

int main(){
  cin >> n >> m;
  for(int i=0;i<n;i++){
    cin >> s[i];
    s[i] = s[i] + s[i] + s[i];
  }

  for(int i=0;i<n;i++){
    prv = -1;
    for(int j=0;j<3*m;j++){
      if(s[i][j] == '1')prv = j;
      num[i][j] = j-prv;
    }
    if(prv<0){
      cout << -1 << endl;
      return 0;
    }
  }

  for(int i=0;i<n;i++){
    prv = 3*m;
    for(int j=3*m;j>=0;j--){
      if(s[i][j] == '1')prv = j;
      num[i][j] = min(num[i][j],prv-j);
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<3*m;j++)cout << num[i][j] << " ";
    cout << endl;
  }
  */

  int ans = n*m;
  for(int i=0;i<n;i++){
    int sum = 0;
    for(int j=0;j<m;j++)sum += num[i][j+m];
    ans = min(ans,sum);
  }

  if(ans == n*m)cout << -1 << endl;
  else cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<complex>
#include<algorithm>

#define pb push_back
#define sz size()
#define clr clear()
#define EPS (1e-10)
#define INF 500000000
#define fs first
#define sc second

#define REP(i,a) for(int i=0;i<(int)(a);i++)
#define let(i,a) __typeof(a)i=(a)
#define all(a) (a).begin(),(a).end()
#define each(i,a) for(let(i,(a).begin());i!=(a).end();i++)
#define INIT(i) memset(i,0,sizeof(i))
#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
typedef complex<double> Po;

int n,m,prv;
string s[110];
int num[110][10100];

int main(){
  cin >> n >> m;
  for(int i=0;i<n;i++)cin >> s[i];

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)num[i][j] = 5*n*m;
    prv = -5*n*m;
    for(int j=0;j<2*m;j++){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],j-prv);
    }
    prv = 5*n*m;
    for(int j=2*m-1;j>=0;j--){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],prv-j);
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)cout << num[i][j] << " ";
    cout << endl;
  }
  */
  int ans = 2*n*m;
  for(int i=0;i<m;i++){
    int sum = 0;
    for(int j=0;j<n;j++)sum += num[j][i];
    ans = min(ans,sum);
  }

  if(ans >= n*m)cout << -1 << endl;
  else cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<complex>
#include<algorithm>

#define pb push_back
#define sz size()
#define clr clear()
#define EPS (1e-10)
#define INF 500000000
#define fs first
#define sc second

#define REP(i,a) for(int i=0;i<(int)(a);i++)
#define let(i,a) __typeof(a)i=(a)
#define all(a) (a).begin(),(a).end()
#define each(i,a) for(let(i,(a).begin());i!=(a).end();i++)
#define INIT(i) memset(i,0,sizeof(i))
#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
typedef complex<double> Po;

int n,m,prv;
string s[110];
int num[30100][110];

int main(){
  cin >> n >> m;
  for(int i=0;i<n;i++){
    cin >> s[i];
    s[i] = s[i] + s[i] + s[i];
  }

  for(int i=0;i<n;i++){
    prv = -1;
    for(int j=0;j<3*m;j++){
      if(s[i][j] == '1')prv = j;
      num[i][j] = j-prv;
    }
    if(prv<0){
      cout << -1 << endl;
      return 0;
    }

    prv = 3*m;
    for(int j=3*m-1;j>=0;j--){
      if(s[i][j] == '1')prv = j;
      num[i][j] = min(num[i][j],prv-j);
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<3*m;j++)cout << num[i][j] << " ";
    cout << endl;
  }
  */

  int ans = n*m;
  for(int i=0;i<m;i++){
    int sum = 0;
    for(int j=0;j<n;j++)sum += num[j][i+m];
    ans = min(ans,sum);
  }

  if(ans == n*m)cout << -1 << endl;
  else cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<complex>
#include<algorithm>

#define pb push_back
#define sz size()
#define clr clear()
#define EPS (1e-10)
#define INF 500000000
#define fs first
#define sc second

#define REP(i,a) for(int i=0;i<(int)(a);i++)
#define let(i,a) __typeof(a)i=(a)
#define all(a) (a).begin(),(a).end()
#define each(i,a) for(let(i,(a).begin());i!=(a).end();i++)
#define INIT(i) memset(i,0,sizeof(i))
#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
typedef complex<double> Po;

int n,m,prv;
string s[110];
int num[30100][110];

int main(){
  cin >> n >> m;
  for(int i=0;i<n;i++)cin >> s[i];

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)num[i][j] = 5*n*m;
    prv = -5*n*m;
    for(int j=0;j<2*m;j++){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],j-prv);
    }
    prv = 5*n*m;
    for(int j=2*m-1;j>=0;j--){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],prv-j);
    }
  }
  /*
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)cout << num[i][j] << " ";
    cout << endl;
  }
  */
  int ans = 2*n*m;
  for(int i=0;i<m;i++){
    int sum = 0;
    for(int j=0;j<n;j++)sum += num[j][i];
    ans = min(ans,sum);
  }

  if(ans >= n*m)cout << -1 << endl;
  else cout << ans << endl;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<string>
#include<sstream>
#include<set>
#include<map>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<complex>
#include<algorithm>

#define pb push_back
#define sz size()
#define clr clear()
#define EPS (1e-10)
#define INF 500000000
#define fs first
#define sc second

#define REP(i,a) for(int i=0;i<(int)(a);i++)
#define let(i,a) __typeof(a)i=(a)
#define all(a) (a).begin(),(a).end()
#define each(i,a) for(let(i,(a).begin());i!=(a).end();i++)
#define INIT(i) memset(i,0,sizeof(i))
#define EQ(a,b) (abs((a)-(b)) < EPS)
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
typedef complex<double> Po;

int n,m,prv;
string s[110];
int num[30100][110];

int main(){
  cin >> n >> m;
  for(int i=0;i<n;i++)cin >> s[i];

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)num[i][j] = 5*n*m;
    prv = -5*n*m;
    for(int j=0;j<2*m;j++){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],j-prv);
    }
    prv = 5*n*m;
    for(int j=2*m-1;j>=0;j--){
      if(s[i][j%m] == '1')prv = j;
      num[i][j%m] = min(num[i][j%m],prv-j);
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++)cout << num[i][j] << " ";
    cout << endl;
  }

  int ans = 2*n*m;
  for(int i=0;i<m;i++){
    int sum = 0;
    for(int j=0;j<n;j++)sum += num[j][i];
    ans = min(ans,sum);
  }

  if(ans >= n*m)cout << -1 << endl;
  else cout << ans << endl;
}


#include <iostream>
using namespace std;
#include <string.h>

string a[10005];
int n;
int m;
int b[10005];

int min(int a,int b){
    if(a > b) return b;
    else return a;
}

bool isZero(string test){
    for(int i=0;i<test.length();i++){
        if(test[i] == '1')
            return false;
    }
    return true;
}

int indexNear(string s1,int pos){

    if(s1[pos]=='1')
        return 0;
    int first=-1;
    int last=-1;
    string s=s1+s1;

    for(int i=0;i<=pos;i++){
        if(s[i] == '1')
            first=i;
    }

    for(int i=pos+1;i < s.length();i++){
        if(s[i] == '1'){
            last=i;
            break;
        }
    }
    //cout << first << " " << last << endl;
    if(first != -1 && last != -1)
        return min(pos-first,last-pos);
    else if(first != -1 && last == -1)
        return pos-first;
    else
        return min(last-pos,m+pos-last);

}
int main(){
    cin >> n;
    cin >> m;

    for(int i=0;i<n;i++){
        cin >> a[i];
        string temp=a[i];

        for(int j=0;j<temp.length();j++){
            if(temp[j]=='1'){
                b[j]++;
            }
        }
    }
    if(n==1 && !isZero(a[0])){
        cout << "0" << endl;
    }

    for(int i=0;i<n;i++){
        if(isZero(a[i])){
            cout << "-1" ;
            return 0;
        }
    }
    int max=0;
    for(int i=0;i<m;i++){
        if(b[i] > b[max])
            max=i;
    }

    int result=0;
    for(int i=0;i<n;i++){
       result=result+indexNear(a[i],max);
    }

    cout << result;
    //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>
#include <algorithm>

char a[10005];
int n;
int m;
 int left1[10005];
int right1[10005];
int res[10005];
int min(int a,int b){
    if(a<b) return a;
    else return b;
}

int main(){
    cin >> n >> m;
    for(int run=0;run<n;run++){

        cin >> a;

          left1[0]=-1;

        if(a[0]=='1'){
            left1[0]=0;
        }else{
            for(int j=m-1;j>0;j--){
                if(a[j]=='1'){
                    left1[0]=m-j;
                    break;
                }
            }
        }

        if(left1[0]==-1){
            cout << "-1" << endl;
            return 0;
        }

        for(int j=1;j<m;j++){
            if(a[j]=='1'){
                left1[j]=0;
            }else{
                left1[j]=left1[j-1]+1;
            }
        }

        if(a[m-1]=='1'){
            right1[m-1]=0;
        }else{
            for(int j=0;j<m-1;j++){
                if(a[j]=='1'){
                    right1[m-1]=j+1;
                    break;
                }
            }
        }

        for(int j=m-2;j>=0;j--){
            if(a[j]=='1'){
                right1[j]=0;
            }else{
                right1[j]=right1[j+1]+1;
            }

        }

        for(int j=0;j<m;j++){
            res[j] += min(right1[j],left1[j]);
        }

    }

    cout << *min_element(res,res+m-1);
  //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>
#include <algorithm>

char a[10005];
int n;
int m;

int res[10005];
int min(int a,int b){
    if(a<b) return a;
    else return b;
}

int main(){
    cin >> n >> m;
    for(int run=0;run<n;run++){
        int left1[10005];
        int right1[10005];
        cin >> a;

          left1[0]=-1;

        if(a[0]=='1'){
            left1[0]=0;
        }else{
            for(int j=m-1;j>0;j--){
                if(a[j]=='1'){
                    left1[0]=m-j;
                    break;
                }
            }
        }

        if(left1[0]==-1){
            cout << "-1" << endl;
            return 0;
        }

        for(int j=1;j<m;j++){
            if(a[j]=='1'){
                left1[j]=0;
            }else{
                left1[j]=left1[j-1]+1;
            }
        }

        if(a[m-1]=='1'){
            right1[m-1]=0;
        }else{
            for(int j=0;j<m-1;j++){
                if(a[j]=='1'){
                    right1[m-1]=j+1;
                    break;
                }
            }
        }

        for(int j=m-2;j>=0;j--){
            if(a[j]=='1'){
                right1[j]=0;
            }else{
                right1[j]=right1[j+1]+1;
            }

        }

        for(int j=0;j<m;j++){
            res[j] += min(right1[j],left1[j]);
        }

    }

    cout << *min_element(res,res+m-1);
  //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>
#include <algorithm>

char a[10005];
int n;
int m;

int res[10005];
int min(int a,int b){
    if(a<b) return a;
    else return b;
}

int main(){
    cin >> n >> m;
    for(int run=0;run<n;run++){
        int left1[10005];
        int right1[10005];
        cin >> a;

          left1[0]=-1;

        if(a[0]=='1'){
            left1[0]=0;
        }else{
            for(int j=m-1;j>0;j--){
                if(a[j]=='1'){
                    left1[0]=m-j;
                    break;
                }
            }
        }

        if(left1[0]==-1){
            cout << "-1" << endl;
            return 0;
        }

        for(int j=1;j<m;j++){
            if(a[j]=='1'){
                left1[j]=0;
            }else{
                left1[j]=left1[j-1]+1;
            }
        }

        if(a[m-1]=='1'){
            right1[m-1]=0;
        }else{
            for(int j=0;j<m-1;j++){
                if(a[j]=='1'){
                    right1[m-1]=j+1;
                }
            }
        }

        for(int j=m-2;j>=0;j--){
            if(a[j]=='1'){
                right1[j]=0;
            }else{
                right1[j]=right1[j+1]+1;
            }

        }

        for(int j=0;j<m;j++){
            res[j] += min(right1[j],left1[j]);
        }

    }

    cout << *min_element(res,res+m-1);
  //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>
#include <algorithm>

char a[20005];
int n;
int m;
 int left1[20005];
int right1[20005];
int res[10005];
int min(int a,int b){
    if(a<b) return a;
    else return b;
}

int main(){
    cin >> n >> m;
    for(int run=0;run<n;run++){

        cin >> a;

          left1[0]=-1;

        if(a[0]=='1'){
            left1[0]=0;
        }else{
            for(int j=m-1;j>0;j--){
                if(a[j]=='1'){
                    left1[0]=m-j;
                    break;
                }
            }
        }

        if(left1[0]==-1){
            cout << "-1" << endl;
            return 0;
        }

        for(int j=1;j<m;j++){
            if(a[j]=='1'){
                left1[j]=0;
            }else{
                left1[j]=left1[j-1]+1;
            }
        }

        if(a[m-1]=='1'){
            right1[m-1]=0;
        }else{
            for(int j=0;j<m-1;j++){
                if(a[j]=='1'){
                    right1[m-1]=j+1;
                    break;
                }
            }
        }

        for(int j=m-2;j>=0;j--){
            if(a[j]=='1'){
                right1[j]=0;
            }else{
                right1[j]=right1[j+1]+1;
            }

        }

        for(int j=0;j<m;j++){
            res[j] += min(right1[j],left1[j]);
        }

    }

    cout << *min_element(res,res+m);
  //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>

string a[10005];
int n;
int m;
int b[10005];

int min(int a,int b){
    if(a > b) return b;
    else return a;
}

bool isZero(string test){
    for(int i=0;i<test.length();i++){
        if(test[i] == '1')
            return false;
    }
    return true;
}

int indexNear(string s1,int pos){

    if(s1[pos]=='1')
        return 0;
    int first=-1;
    int last=-1;
    string s=s1+s1;

    for(int i=0;i<=pos;i++){
        if(s[i] == '1')
            first=i;
    }

    for(int i=pos+1;i < s.length();i++){
        if(s[i] == '1'){
            last=i;
            break;
        }
    }
    //cout << first << " " << last << endl;
    if(first != -1 && last != -1)
        return min(pos-first,last-pos);
    else if(first != -1 && last == -1)
        return pos-first;
    else
        return min(last-pos,m+pos-last);

}
int main(){
    cin >> n;
    cin >> m;

    for(int i=0;i<n;i++){
        cin >> a[i];
        string temp=a[i];

        for(int j=0;j<temp.length();j++){
            if(temp[j]=='1'){
                b[j]++;
            }
        }
    }
    if(n==1 && !isZero(a[0])){
        cout << "0" << endl;
        return 0;
    }

    for(int i=0;i<n;i++){
        if(isZero(a[i])){
            cout << "-1" ;
            return 0;
        }
    }
    int max=0;
    for(int i=0;i<m;i++){
        if(b[i] > b[max])
            max=i;
    }

    int result=0;
    for(int i=0;i<n;i++){
       result=result+indexNear(a[i],max);
    }

    cout << result;
    //system("PAUSE");

    return 0;
}


#include <iostream>
using namespace std;
#include <string.h>
#include <algorithm>

char a[10005];
int n;
int m;
int left1[10005];
int right1[10005];
int res[10005];
int min(int a,int b){
    if(a<b) return a;
    else return b;
}

int main(){
    cin >> n >> m;
    for(int i=0;i<n;i++){

        cin >> a;

          left1[0]=-1;

        if(a[0]=='1'){
            left1[0]=0;
        }else{
            for(int j=m-1;j>0;j++){
                if(a[j]=='1'){
                    left1[0]=m-j;
                    break;
                }
            }
        }

        if(left1[0]==-1){
            cout << "-1" << endl;
            return 0;
        }

        for(int j=1;j<m;j++){
            if(a[j]=='1'){
                left1[j]=0;
            }else{
                left1[j]=left1[j-1]+1;
            }
        }

        if(a[m-1]=='1'){
            right1[m-1]=0;
        }else{
            for(int j=0;j<m-1;j++){
                if(a[j]=='1'){
                    right1[m-1]=j+1;
                }
            }
        }

        for(int j=m-2;j>=0;j--){
            if(a[j]=='1'){
                right1[j]=0;
            }else{
                right1[j]=right1[j+1]+1;
            }

        }

        for(int j=0;j<m;j++){
            res[j] += min(right1[j],left1[j]);
        }

    }
    cout << *min_element(res,res+m);
  //system("PAUSE");

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define int long long
string s[200];
int dp[200][20000];
main()
{
  int n, m; cin >> n >> m;
  for(int i = 0 ;  i < n; i ++)
    {
      cin >> s[i];
      if(s[i].find('1') == string::npos) { cout << "-1" << endl; return 0; }
    }
  for(int i = 0 ; i < n; i ++)
    for(int j = 0 ; j < m; j ++)
      dp[i][j] = 200000000;
  for(int i = 0 ; i < n; i ++)
    {
      for(int j = 0; j < m; j ++) if(s[i][j] == '1') dp[i][j] = 0;
      for(int j = 0; j < m; j ++) dp[i][j] = min(dp[i][j], dp[i][(j + m - 1) % m] + 1);
      for(int j = 0; j < m; j ++) dp[i][j] = min(dp[i][j], dp[i][(j + m - 1) % m] + 1);
      for(int j = m - 1; j >= 0; j --) dp[i][j] = min(dp[i][j], dp[i][(j + 1) % m] + 1);
      for(int j = m - 1; j >= 0; j --) dp[i][j] = min(dp[i][j], dp[i][(j + 1) % m] + 1);
    }
  int ans = 2000000000;
  int index = 0;
  for(int i = 0 ; i < m ;i ++)
    {
      int count = 0;
      for(int j = 0; j < n; j ++)
	count += dp[j][i];
      ans = min(count , ans);
    }
  cout << ans << endl;
  return 0 ;
}


#include <iostream>

using namespace std;

int main() {
	int n, k;
	int** arrs;
	int** pos;
	int* dp;
	cin >> n >> k;
	arrs = new int* [k];
	dp = new int[n];
	pos = new int* [k];
	for (int i = 0; i < k; i++) {
		arrs[i] = new int[n];
		pos[i] = new int[n];
	}
	for (int i = 0; i < k; i++)
		for (int j = 0; j < n; j++) {
			cin >> arrs[i][j];
			arrs[i][j]--;
			pos[i][arrs[i][j]] = j;
		}

	for (int i = 0; i < n; i++) {
		int m = 0;
		for (int j = 0; j < i; j++) {
			int p;
			int flags = 0;
			for (p = 1; p < k; p++) {
				if (pos[p][arrs[0][j]] > pos[p][arrs[0][i]]) {
					flags = 1;
					break;
				}
			};
			if (flags == 0 && dp[j] > m)
				m = dp[j];
		}
		dp[i] = m + 1;
	}
	int m = 0;
	for (int i = 0; i < n; i++) {
		if (dp[i] > m)
			m = dp[i];
	}
	cout << m << endl;
}


#include <iostream>

using namespace std;

int main() {
	int n, m;
	int* arr, * l, * r, * q;
	cin >> n >> m;
	arr = new int[n];
	l = new int[m];
	r = new int[m];
	q = new int[m];
	for (int i = 0; i < n; i++) {
		arr[i] = 0;
	}
	for (int i = 0; i < m; i++) {
		cin >> l[i] >> r[i] >> q[i];
		l[i]--;
		r[i]--;
	}

	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 31; j++) {
			if (q[i] & (1 << j)) {
				for (int k = l[i]; k <= r[i]; k++) {
					arr[k] |= (1 << j);
				}
			}
		}
	}

	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 31; j++) {
			if ((q[i] & (1 << j)) == 0) {
				int count = 0;
				for (int k = l[i]; k <= r[i]; k++) {
					if ((arr[k] & (1 << j)) == 0) {
						count++;
					}
				}
				if (count == 0) {
					cout << "NO" << endl;
					return 0;
				}
			}
		}
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}


#include <iostream>

using namespace std;

int main() {
	int n, m;
	int* arr, * l, * r, * q;
	cin >> n >> m;
	arr = new int[n];
	l = new int[m];
	r = new int[m];
	q = new int[m];
	for (int i = 0; i < n; i++) {
		arr[i] = 0;
	}
	for (int i = 0; i < m; i++) {
		cin >> l[i] >> r[i] >> q[i];
		l[i]--;
		r[i]--;
	}

	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 31; j++) {
			if ((q[i] & (1 << j))) {
				for (int k = l[i]; k <= r[i]; k++) {
					arr[k] ^= (1 << j);
				}
			}
		}
	}

	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 31; j++) {
			if (q[i] & (1 << j) == 0) {
				int count = 0;
				for (int k = l[i]; k <= r[i]; k++) {
					if ((arr[k] & (1 << j)) == 0) {
						count++;
					}
				}
				if (count == 0) {
					cout << "NO" << endl;
					return 0;
				}
			}
		}
	}
	cout << "YES" << endl;
	for (int i = 0; i < n; i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <sstream>
#include <queue>
#include <cctype>
#include <cmath>
#include <fstream>

using namespace std;

typedef long long ll;
#define REP(i,k) for(int (i)=0;(i)<(k);(i)++)
#define INF ~(1<<31)
#define CLR(a) memset((a),0,sizeof((a)))
#define EPS 1e-9
#define DBG 1
#define D(s,v) if(DBG) cout<<(s)<<": "<<(v)<<endl;
#define DVEC(s,v) if(DBG) {cout<<(s)<<": "; for(int i=0;i<(v).size();i++) cout<<(v)[i]<<" "; cout<<endl;}
#define DARR(s,v,n) if(DBG) {cout<<(s)<<": "; for(int i=0;i<n;i++) cout<<(v)[i]<<" "; cout<<endl;}

#define MOD 1000000007
#define NUMBITS 31

int N;
int L[32][200005], R[32][200005], V[32][200005];
int POS[32][100005];
vector<int> a;

void build(int pos, int l, int r, int bit){
    L[bit][pos] = l;
    R[bit][pos] = r;
    if(l==r){
        POS[bit][l] = pos;
        return;
    }
    int mid = (l+r)/2;
    build(2*pos+1, l, mid, bit);
    build(2*pos+2, mid+1, r, bit);
}

void build_tree(int n, int bits){
    N = n;
    CLR(L);
    CLR(R);
    CLR(V);

    for(int bit=0;bit<bits;bit++){
        build(0, 0, n-1, bit);
    }
}

void add(int idx, int v, int bit){
    int p = POS[bit][idx];
    while(true){
        V[bit][p] += v;
        if(p==0) break;
        p = (p-1) / 2;
    }
}

void add_range(int l, int r, int v, int bit){
    add(l, v, bit);
    if(r+1 < N)
        add(r+1, -v, bit);
}

int query(int pos, int l, int r, int bit){
    //cout<<"query: "<<pos<<" L:"<<L[bit][pos]<<" R:"<<R[bit][pos]<<" l: "<<l<<" r: "<<r<<endl;
    if(R[bit][pos] < l || L[bit][pos] > r)
        return 0;
    else if(L[bit][pos]>=l && R[bit][pos]<=r)
        return V[bit][pos];

    return query(2*pos+1, l, r, bit) +
    query(2*pos+2, l, r, bit);
}

void compute_prefix_sum(){
    for(int b=0;b<NUMBITS;b++){
        int psum = 0;

        for(int i=0;i<N;i++){
            int cur = query(0, i, i, b);
            //cout<<"b: "<<b<<" i: "<<i<<" cur: "<<cur<<endl;
            psum += cur;
            add(i, -cur, b);
            add(i, psum, b);
        }
    }
}

int main(){
    int n,m;
    while(cin>>n>>m){
        build_tree(n, NUMBITS);

        vector<int> ls,rs,qs;
        for(int i=0;i<m;i++){
            int l,r,q;
            cin>>l>>r>>q;
            l--;
            r--;
            ls.push_back(l);
            rs.push_back(r);
            qs.push_back(q);
            for(int b=0;b<NUMBITS;b++){
                if((q>>b) & 1){
                    add_range(l, r, 1, b);
                }
            }
        }
        compute_prefix_sum();

        bool good = 1;
        for(int i=0;i<m;i++){
            int l = ls[i];
            int r = rs[i];
            int q = qs[i];
            for(int b=0;b<NUMBITS;b++){
                int numset = query(0,l,r,b);
                if(!((q>>b) & 1)){
                    if(numset == r-l+1){
                        good = 0;
                    }
                }
            }
        }
        if(!good){
            cout<<"NO"<<endl;
        } else{
            cout<<"YES"<<endl;
            for(int i=0;i<n;i++){
                int res = 0;
                for(int b=0;b<NUMBITS;b++){
                    res |= (query(0,i,i,b) << b);
                }
                cout<<res<<" ";
            }
            cout<<endl;
        }
    }
}