// Author : Naresh Bharasagar
// Instt. : Indian Institute of Technology Guwahati

//--------------------------------------------------------------------------------------------------------------------------------

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

#pragma GCC target ("sse4.2")
#define loop(i,begin,end) for(__typeof(end)i=(begin)-((begin)>(end));i!=(end)-((begin)>(end));i+=1-2*((begin)>(end)))
#define loopset(it,val,dtype) for(set<dtype>::iterator it=val.begin(); it!=val.end(); it++)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vloo vector<loo>
#define vi vector<int>
#define pi pair<int,int>
#define mp make_pair
#define ploo pair<loo,loo>
#define fillz(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define outa(a,n) loop(i,0,n) cout << a[i] << " ";
#define pr(a) cout << a << "\n";
#define pb(l) push_back(l)
#define ff first
#define ss second
#define input(a) for(auto &i: a) cin >> i;
#define deb(i) cout << "[" << #i << "=" << i << "]" << endl;
#define nl cout << "\n";
#define INF 1000000000
#define M 1000000007

loo power(loo x, loo y, loo p) {
    loo res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

bool sortbysec(const pair<loo,loo> &a, const pair<loo,loo> &b) {
    return (a.second > b.second);
}

string decToBin(loo n) {
	string s = "0";
	if(n == 0) return s;
	s = "";
    while (n > 0) {
        s += (n % 2) + '0';
        n = n / 2;
    }
    reverse(s.begin(), s.end());
    return s;
}

loo binToDec(string s) {
    loo ans = 0;
    reverse(s.begin(), s.end());
    loop(i, 0, s.size()) ans += pow(2, i)*(s[i] - '0');
    return ans;
}

loo nCr(loo n, loo r) {
    loo p = 1, k = 1;
    if (n - r < r)
        r = n - r;
    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
            loo m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else p = 1;
    return p;
}

// vector<int> dist;
vector<bool> vis(100001,false);
void dfs(vector<loo> adj[], loo src) {
    vis[src]=true;
    loop(i,0,adj[src].size()) {
        loo x = adj[src][i];
        if(vis[x]==false) {
            vis[x]=true;
            // dist[x] = dist[src]+1;
            dfs(adj,x);
        }
    }
}

// vector<loo> sg(100001,-1); //prev
void bfs(vector<loo> adj[], loo src) {
    list<loo> ql;
    vis[src]=true;
    ql.pb(src);
    while(!(ql.empty())) {
        src = ql.front();
        // cout << src << " ";
        ql.pop_front();

        loop(i,0,adj[src].size()) {
            if(!vis[adj[src][i]]) {
                vis[adj[src][i]]=true;
                // sg[adj[src][i]]=src;
                ql.pb(adj[src][i]);
            }
        }
    }
}

// ----------------------------------------------------------------
void solve() {
    loo n,m,mn=INF;
    char c;
    cin >> n >> m;
    vector<vloo> grid(n,vloo(m));
    vector<vloo> closest(n,vloo(m,-1));
    loop(i,0,n) {
        loo prev = -1;
        loop(j,0,m) {
            cin >> c;
            if(c=='1') prev=j;
            if(prev!=-1) closest[i][j]=min(j-prev,m-j+prev);
            grid[i][j]=c-'0';
        }
        prev=-1;
        loop(j,m,0) {
            if(c=='1') prev=j;
            if(prev!=-1) closest[i][j]=min(closest[i][j],min(prev-j,m+j-prev));
        }
    }
    loop(j,0,m) {
        loo temp = 0;
        bool f = true;
        loop(i,0,n) {
            if(closest[i][j]==-1) {
                f=false;
                break;
            }
            else temp+=closest[i][j];
        }
        if(f) mn=min(mn,temp);
    }
    if(mn==INF) cout << -1 << "\n";
    else cout << mn << "\n";
}

int main() {
	fast_io

    // freopen("validation_input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

    loo q = 1;
    // cin >> q;
    while(q--) {
        solve();
    }

    return 0;
}


// Author : Naresh Bharasagar
// Instt. : Indian Institute of Technology Guwahati

//--------------------------------------------------------------------------------------------------------------------------------

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

#pragma GCC target ("sse4.2")
#define loop(i,begin,end) for(__typeof(end)i=(begin)-((begin)>(end));i!=(end)-((begin)>(end));i+=1-2*((begin)>(end)))
#define loopset(it,val,dtype) for(set<dtype>::iterator it=val.begin(); it!=val.end(); it++)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vloo vector<loo>
#define vi vector<int>
#define pi pair<int,int>
#define mp make_pair
#define ploo pair<loo,loo>
#define fillz(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define outa(a,n) loop(i,0,n) cout << a[i] << " ";
#define pr(a) cout << a << "\n";
#define pb(l) push_back(l)
#define ff first
#define ss second
#define input(a) for(auto &i: a) cin >> i;
#define deb(i) cout << "[" << #i << "=" << i << "]" << endl;
#define nl cout << "\n";
#define INF 1000000000
#define M 1000000007

loo power(loo x, loo y, loo p) {
    loo res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1) res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

bool sortbysec(const pair<loo,loo> &a, const pair<loo,loo> &b) {
    return (a.second > b.second);
}

string decToBin(loo n) {
	string s = "0";
	if(n == 0) return s;
	s = "";
    while (n > 0) {
        s += (n % 2) + '0';
        n = n / 2;
    }
    reverse(s.begin(), s.end());
    return s;
}

loo binToDec(string s) {
    loo ans = 0;
    reverse(s.begin(), s.end());
    loop(i, 0, s.size()) ans += pow(2, i)*(s[i] - '0');
    return ans;
}

loo nCr(loo n, loo r) {
    loo p = 1, k = 1;
    if (n - r < r)
        r = n - r;
    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
            loo m = __gcd(p, k);
            p /= m;
            k /= m;
            n--;
            r--;
        }
    }
    else p = 1;
    return p;
}

// vector<int> dist;
vector<bool> vis(100001,false);
void dfs(vector<loo> adj[], loo src) {
    vis[src]=true;
    loop(i,0,adj[src].size()) {
        loo x = adj[src][i];
        if(vis[x]==false) {
            vis[x]=true;
            // dist[x] = dist[src]+1;
            dfs(adj,x);
        }
    }
}

// vector<loo> sg(100001,-1); //prev
void bfs(vector<loo> adj[], loo src) {
    list<loo> ql;
    vis[src]=true;
    ql.pb(src);
    while(!(ql.empty())) {
        src = ql.front();
        // cout << src << " ";
        ql.pop_front();

        loop(i,0,adj[src].size()) {
            if(!vis[adj[src][i]]) {
                vis[adj[src][i]]=true;
                // sg[adj[src][i]]=src;
                ql.pb(adj[src][i]);
            }
        }
    }
}

// ----------------------------------------------------------------
void solve() {
    loo n,m,mn=INF;
    char c;
    cin >> n >> m;
    vector<vloo> grid(n,vloo(m));
    vector<vloo> closest(n,vloo(m,-1));
    loop(i,0,n) {
        loo prev = -1;
        loop(j,0,m) {
            cin >> c;
            if(c=='1') prev=j;
            if(prev!=-1) closest[i][j]=min(j-prev,m-j+prev);
            grid[i][j]=c-'0';
        }
        prev=-1;
        loop(j,m,0) {
            if(grid[i][j]) prev=j;
            if(prev!=-1) {
                if(closest[i][j]==-1) closest[i][j]=min(prev-j,m-prev+j);
                else closest[i][j]=min(closest[i][j],min(prev-j,m+j-prev));
            }
        }
    }
    loop(j,0,m) {
        loo temp = 0;
        bool f = true;
        loop(i,0,n) {
            if(closest[i][j]==-1) {
                f=false;
                break;
            }
            else temp+=closest[i][j];
        }
        if(f) mn=min(mn,temp);
    }
    if(mn==INF) cout << -1 << "\n";
    else cout << mn << "\n";
}

int main() {
	fast_io

    // freopen("validation_input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

    loo q = 1;
    // cin >> q;
    while(q--) {
        solve();
    }

    return 0;
}


// Problem : A. Shifts
// Contest : Codeforces Round #142 (Div. 1)
// URL : https://codeforces.com/problemset/problem/229/A
// Memory Limit : 256 MB
// Time Limit : 2000 ms

//kaushik.mv3
#include <climits>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <queue>
#include <cassert>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define in(n) scanf("%d",&n)
#define inl(n) scanf("%lld",&n)
#define out(n) printf("%d ",n)
#define outln(n) printf("%d\n",n)
#define outl(n) printf("%lld ",n)
#define outlln(n) printf("%lld\n",n)
#define LL long long
#define pb push_back
#define f first
#define s second
using namespace std;
int m,n;
string g[101];
int table[110][10010];
int R[20010], L[20010];
void goR(string S)
{
	S = S + S;
	int prev = 1e9;
	for(int i=S.size()-1;i>=0;i--)
	{
		if(S[i]=='1') prev = i;
		R[i] = abs(i-prev);
	}

}
void goL(string S)
{
	S = S + S;
	int prev = 1e9;
	for(int i=0;i<S.size();i++)
	{
		if(S[i]=='1') prev = i;
		L[i%m] = abs(i-prev);
	}
}
int main()
{
	in(n);in(m);
	rep(i,0,n) cin >> g[i];
	for(int i=0;i<n;i++)
	{
//		for(int j=0;j<m;j++)R[j] = L[j] = INT_MAX;
		goR(g[i]);
		goL(g[i]);
		rep(j,0,m)
		{
			table[i][j] = min(R[j],L[j]);
			if(table[i][j] > 1e8)
			{
				puts("-1");
				return 0;
			}
		}
	}
	long long res = INT_MAX;
	for(int j=0;j<m;j++)
	{
		long long tmp = 0;
		for(int i=0;i<n;i++)tmp += table[i][j];
		res = min(res,tmp);
	}
	cout<<res<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define trace(x) clog<<#x<<": "<<x<<endl
#define debug(x) cout << #x << ":" << x << ' ';
#define debugg(x) cout << #x << ":" << x << ' ' << "\n";
#define Mod 1000000007
const double PI=acos(-1);
#define lb lower_bound
#define ub upper_bound
#define mkp make_pair
#define pob pop_back
#define pof pop_front
#define pb push_back
#define pf push_front

#define pi pair<int,int>
#define pll pair<ll,ll>
#define pdb pair<db,db>
#define pldb pair<ldb,ldb>

#define vi vector<int>
#define vll vector<ll>
#define vdb vector<db>
#define vldb vector<ldb>
#define vpi vector< pi >
#define vpll vector< pll >

#define si set<int>
#define li list<int>
#define fi first
#define in insert
#define se second
#define fr(i,n) for(i = 0 ; i < n ; i++)
#define  test int t; cin>>t; while(t--)
//NOTE- log2(1e18) = 60
template<typename T, typename T1>
T mod(T x, T1 p) {
    x %= p;
    if (x < 0)
        x += p;
    return x;
}
template<typename T>
T inverse(T x, T p) {
    x = mod(x, p);
    if (x == 1)
        return x;
    return mod(1LL * (-p / x) * (inverse(p % x, p)) , p);
}
//NCR mod calculation
/*int fact[1000001];
int ifact[1000001];
int ncr(int n, int r) {
    int res = (1ll * ifact[n - r]*ifact[r]) % Mod ;
	res = (1ll*res*fact[n]) % Mod;
    return res;
}*/

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;
int32_t main()
{
    fio
    bool err = 0;
    int n,m,i,j,l,k,nl = 0,ans = INT_MAX;
    cin>>n>>m;
    int a[m]= {0};
    vector<string> v(n);

    vector<vector<int> > mt(n,vector<int>(m,INT_MAX));
    fr(i,n)
    {cin>>v[i];
    vector<bool> vis(m,0);
    l = 0;
    k = 0;
    queue<int> q;
    fr(j,m)
    {
        if(v[i][j] == '1')
            {q.push(j);
            l++;
            vis[j]=1;
            }
    }
    if(l==0)
        err = 1;
if(!err)
        {while(!q.empty())
        {
          int x = q.front();
          q.pop();
          l--;
          mt[i][x] = min(mt[i][x],k);
          if( x-1 >= 0 &&vis[x-1]==0&& mt[i][x-1] == INT_MAX)
            {q.push(x-1);
            nl++;
            vis[x-1] = 1;}
            else if(x-1 == -1)
            {
             if(vis[m-1] == 0 && mt[i][m-1] == INT_MAX)
             {
                q.push(m-1);
            nl++;
            vis[m-1] = 1;
             }
            }
          if( x+1< m &&vis[x+1]==0 && mt[i][x+1] == INT_MAX)
            {q.push(x+1);
            nl++;
            vis[x+1] = 1;
            }
            else if(x+1 == m)
            {
                if(vis[0] == 0 && mt[i][0] == INT_MAX)
            {
                q.push(0);
            nl++;
            vis[0] = 1;
             }
            }

            if(l == 0)
            {
                l = nl;
                nl = 0;
                k++;
            }
        }
        }

    }
    /*fr(i,n)
    {
        fr(j,m)
        cout<<mt[i][j]<<" ";
        cout<<endl;
    }(*/

  //  deb(a);

  if(err)
  {
      cout<<-1;
      return 0;
  }
    for(i = 0;i< m;i++)
    {
        for(j = 0;j<n;j++)
        {
            a[i]+=mt[j][i];
        }
        ans = min(ans,a[i]);
    }
/*fr(i,m)
cout<<a[i]<<" ";
cout<<endl;(*/
cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define vi vector<int>
#define vvi vector<vector<int> >
#define mii map<int,int>
#define pb push_back
#define pii pair<int,int>
#define mkp make_pair
#define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' '
#define mem(a,v) memset(a,v,sizeof(a))
#define loop(i,n) for (int i = 0; i < n; i++)
#define loop1(i,n) for (int i = 1; i < n; i++)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15)
#define PI 3.14159265
using namespace std;
int mod = 1000000007;
int inf = 1e15;
int m_inf = INT_MIN;

/*int main()
{
    FastIO;
    int n, m, a, b;
    cin>>n>>m>>a>>b;
    if(m * a > b)
    {
        int remain = (n % m) * a;
        if(remain > b)
            cout<<n/m*b+b;
        else
            cout<<n/m*b+remain;
    }
    else
        cout<<n*a;
}*/

/*int main()
{
    int l[6];
    map<int,bool> m;
    map<int,int> mp;
    int arr[6];
    int k=0,flag=0;
    for(int i=0;i<6;i++)
        cin>>l[i];
    for(int i=0;i<6;i++)
    {
        mp[l[i]]++;
    }
    for(int i=0;i<6;i++)
    {
        if(mp[l[i]]>=4)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        cout<<"Alien";
    }
    else
    {
        for(int i=0;i<6;i++)
        {
            if(m[l[i]]==false)
            {
                m[l[i]]=true;
                if(mp[l[i]]>=4)
                {
                    mp[l[i]]-=4;
                    if(mp[l[i]]==1)
                    {
                        arr[k++]=l[i];
                    }
                    else if(mp[l[i]]==2)
                    {
                        arr[k++]=l[i];
                        arr[k++]=l[i];
                    }
                }
                else
                {
                    if(mp[l[i]]==2)
                    {
                        arr[k++]=l[i];
                        arr[k++]=l[i];
                    }
                    else
                    {
                        arr[k++]=l[i];
                    }
                }
            }
        }
        if(arr[0]==arr[1])
            cout<<"Elephant";
        else
            cout<<"Bear";
    }
}*/

/*int main()
{
    FastIO;
    ll m,b;
    cin>>m>>b;
    ll sum=0,ans=0;
    for(ll y=b;y>=0;y--)
    {
        ll x=m*(b-y);
        ll sum=0;
        sum+=(x+1)*(y*(y+1)/2)+(y+1)*((x*(x+1))/2);
        ans=max(ans,sum);
    }
    cout<<ans;
}*/

/*int main()
{
    int n;
    cin>>n;
    int t[n+1];
    for(int i=1;i<=n;i++)
        cin>>t[i];
    int T;
    cin>>T;
    sort(t+1,t+n+1);
    int cnt=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            if(t[j]-t[i]>T)
                break;
            cnt++;
        }
        ans=max(ans,cnt);
        cnt=0;
    }
    cout<<ans;
}*/

/*int main()
{
    int n,b;
    cin>>n>>b;
    int a[n+1];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll ans=b,large=0;
    for(int i=0;i<n-1;i++)
    {
        large=a[i];
        for(int j=i+1;j<n;j++)
        {
            if(a[j]>large)
            {
                large=a[j];
            }
        }
        ll temp=b/a[i];
        ll extra=b%a[i];
        ll sold=temp*large;
        ll total=extra+sold;
        ans=max(ans,total);
    }
    cout<<ans;
}*/

/*map<char,bool> mp;
int n,ans=0;

string tostring(int a)
{
    stringstream ss;
    string s;
    ss<<a;
    ss>>s;
    return s;
}

void setvalue()
{
    for(char i='0';i<='9';i++)
        mp[i]=false;
}
void dfs(int k)
{
    setvalue();
    if(k>0 && k<=n)
        ans++;
    if(k>=1e8)
        return;
    for(int i=0;i<=9;i++)
    {
        setvalue();
        int num=k*10+i;
        if(num>0)
        {
            int x=num;
            string s=tostring(x);
            int cnt=0;
            for(int i=0;i<s.length();i++)
            {
                if(mp[s[i]]==false)
                {
                    mp[s[i]]=true;
                    cnt++;
                }
            }
            if(cnt<=2)
                dfs(num);
        }
    }
}

int main()
{
    FastIO;
    cin>>n;
    dfs(0);
    if(n==1e9)
        cout<<ans+1;
    else
        cout<<ans;
}*/
int a[105][10005],sum,ans=1e9;
int main()
{
    FastIO;
    int n,m;
    cin>>n>>m;
    string s[n];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
        bool check=false;
        for(int j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                check=true;
                break;
            }
        }
        if(!check)
        {
            cout<<"-1";
            return 0;
        }
        for(int j=0;j<2*m;j++)
        {
            if(s[i][j%m]=='1')
                a[i][j%m]=0;
            else
            {
                a[i][j%m]=a[i][(j-1+m)%m]+1;
            }
        }
        for(int j=2*m-1;j>=0;j--)
        {
            if(s[i][j%m]=='1')
                a[i][j%m]=0;
            else
            {
                a[i][j%m]=min(a[i][j%m],a[i][(j+1)%m]+1);
            }
        }
    }
    for(int i=0;i<m;i++)
    {
        sum=0;
        for(int j=0;j<n;j++)
        {
            sum+=a[j][i];
        }
        ans=min(ans,sum);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define repi(i,n) for(int i=0;i<(n);++i)
//#define repA(i,a,n) for(int i=a;i<=(n);++i)
//#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define ll long long int
#define endl '\n'
#define mod 1000000007
#define lld long long double
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>  >
#define err(t) cout<<"Error : "<<t<<endl;
#define all(v) v.begin(),v.end()

#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i,a,b) for(ll i = a; i <= (b); i++)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)

/*
vector<bool> sieve(long long int n){ vector<bool> s(n+1,0); s[1]=1;	for(long long int i=2;i<=n;i++){ if(s[i]==0){ for(long long int j=i*i;j<=n;j+=i) s[j]=1; }	} return s; }

long long int phi(long long int n){ auto p= sieve(n); long long int ans=n; for(int i=1;i<p.size();++i){ if(p[i]==1) continue;  if(i>n) break;  if(n%i==0) {ans/=i;ans*=(i-1);} } return ans; }

vector<int> bfs(vector<vector<int> > g,int k){
	vector<int> t(g.size(),inf);
	vector<bool> vis(g.size(),false);
	queue<int> q;
	q.push(k);
	t[k]=1;
	vis[k]=true;
	int count=1;
	while(!q.empty()){
		int j=q.front();
		q.pop();
		for(int i=0;i<g[j].size();++i){
			if(!vis[g[j][i]]) {t[g[j][i]]=t[j]+1;vis[g[j][i]]=true;q.push(g[j][i]);}
		}
	} return t;
}

*/
/*template<typename T>inline void in(T &x){x=0;register T c=inchar();while(((c<48)||(c>57))&&(c!='-'))c=inchar();bool neg=false;if(c=='-')neg=true;for(;c<48||c>57;c=inchar());for(;c>47&&c<58;c=inchar())x=(x<<3)+(x<<1)+(c&15);if(neg)x=-x;}
 template<typename T>inline void out(T n){if(n<0){outchar('-');n*=-1;}char snum[65];ll i=0;do {snum[i++]=n%10+'0';n/=10;}while(n);i=i-1;while(i>=0)outchar(snum[i--]);}
ll pr[M+1],freq[M+1];
ll fact[M+1],inv[M+1];*/

//ll md=MD;
//ll exp(ll a,ll b){ll r=1ll;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b/=2;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
//ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
//ll chose(ll n,ll k){ll r=fact[n]%md;r=(r+md)%md;r=r*inv[k]%md;r=(r+md)%md;r=r*inv[n-k]%md;r=(r+md)%md;return r;}
  ll max1(ll a,ll b){ return a<b?a:b;}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	 int n,m;cin>>n>>m;
	// string s;cin>>s;
	char c[n][m];ll po[n][m];bool pos=true;
	repi(i,n) repi(j,m) cin>>c[i][j];
	repi(i,n) repi(j,m) po[i][j]=inf;
	for (int j = 0; j < n; j += 1)
		{
				pos=true;
			for (int i = 0; i < 1; i += 1)
			{

			for (int k = 0; k <= m/2; k += 1)
			{
				if(i+k<m){if(c[j][(i+k)]=='1') {po[j][i]=k,pos=false;break;}}
				else  {if(c[j][(i+k-m)]=='1') {po[j][i]=k,pos=false;break;}}

				if(i-k>=0){if(c[j][(i-k)]=='1') {po[j][i]=k,pos=false;break;}}
				else  {if(c[j][(i-k+m)]=='1') {po[j][i]=k,pos=false;break;}}
			}

			}
			if(pos) {cout<<-1<<endl;return 0;}
			for (int i = 1; i < m; i += 1)
			{

				if(c[j][i]=='1') {po[j][i]=0;continue;}
			for (int k = po[j][i-1]-1; k <= po[j][i-1]+1; k += 1)
			{

				if(i+k<m&&i+k>=0){if(c[j][(i+k)]=='1') po[j][i]=max1(po[j][i],abs(k));}

				else if(i+k<0){if(c[j][(i+k+m)]=='1') po[j][i]=max1(po[j][i],abs(k));}
				else  {if(c[j][(i+k-m)]=='1') po[j][i]=max1(po[j][i],abs(k));}

				if(i-k<m&&i-k>=0){if(c[j][(i-k)]=='1') po[j][i]=max1(po[j][i],abs(k));}
				else if(i-k>=m){if(c[j][(i-k-m)]=='1') po[j][i]=max1(po[j][i],abs(k));}
				else  {if(c[j][(i-k+m)]=='1') po[j][i]=max1(po[j][i],abs(k));}

				if(po[j][i]==0) break;
			}

			}

		}

	ll g=inf;

	for (int i = 0; i < m; i += 1)
	{	ll cou=0;
		for (int j = 0; j < n; j += 1)
		{
		cou+=po[j][i];
		}

		g=max1(g,cou);
		if(g==0) break;
	}
	cout<<g<<endl;
	//ll n;cin>>n; vl a(n); repi(i,n) cin>>a[i];
	/*
	int q;cin>>q;while(q--){

	 }
	 //*/
return 0;
}


#include <bits/stdc++.h>

using namespace std;

/******************************MACROS******************************/

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define F first
#define S second
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue
#define m_p make_pair

/******************************TEMPLATE******************************/

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
    ~debug() { cerr << endl; }
    eni(!=) cerr << boolalpha << i; ris; }
    eni(==) ris << range(begin(i), end(i)); }
    sim, class b dor(pair < b, c > d) {
        ris << "(" << d.first << ", " << d.second << ")";
    }
    sim dor(rge<c> d) {
        *this << "[";
        for (auto it = d.b; it != d.e; ++it)
            *this << ", " + 2 * (it == d.b) << *it;
        ris << "]";
    }
#else
    sim dor(const c&) { ris; }
#endif
};
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

/******************************ALGORITHMS******************************/

int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int __pow(int x, int y) {int res = 1;while(y>0){if(y&1) res = (res*x); y>>=1; x = (x*x);}return res;}
int mi(int x, int M) {return __pow(x, M-2, M);}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}
int add(int a, int b, int M) {return (a+b) % M;}
int mul(int a, int b, int M) {return (a*b) % M;}
int sub(int a, int b, int M) {return (a-b+M) % M;}
int div(int a, int b, int M) {return (a*mi(b, M)) % M;}
void umax(int &a, int b) {a = max(a, b);}
void umin(int &a, int b) {a = min(a, b);}

/******************************main*code******************************/

const int N = 300+2, inf = 1e18, M = 1e9+7;

int Test, n, m, k, query;
double dp[N][N][N];
bool ok = 1;

void test_case()
{
    cin >> n >> m;
    string a[n];
    for(int i = 0; i<n; i++)
        cin >> a[i];
    vt<int> pos[n];
    for(int i = 0; i<n; i++)
        for(int j = 0; j<m; j++)
            if(a[i][j] == '1')
                pos[i].pb(j);
    int ans = inf;
    for(int j = 0; j<m; j++)
    {
        int x = 0;
        for(int i = 0; i<n; i++)
        {
            auto it = lower_bound(all(pos[i]), j);
            if(pos[i].empty())
            {
                cout << -1 << endl;
                return;
            }
            debug() << deb(pos[i]);
            int a1 = inf, a2 = inf;
            if(it != pos[i].end())
            {
                a1 = *it - j;
                debug() << deb(*it);
            }
            if(it != pos[i].begin())
            {
                a2 = j - *prev(it);
                debug() << deb(*prev(it));
            }
            x += min({a1, a2, m + j - pos[i].back(), m + pos[i][0] - j});
            debug() << deb(j) deb(i) deb(a1) deb(a2);
        }
        ans = min(ans, x);
    }
    cout << ans << endl;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Test = 1;
    //cin >> Test;
    while(Test--)
        test_case(); // I'm screamin' out, no regrets
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

/******************************MACROS******************************/

#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vt vector
#define pb push_back
#define F first
#define S second
#define u_s unordered_set
#define u_m unordered_map
#define p_q priority_queue
#define m_p make_pair

/******************************TEMPLATE******************************/

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
    ~debug() { cerr << endl; }
    eni(!=) cerr << boolalpha << i; ris; }
    eni(==) ris << range(begin(i), end(i)); }
    sim, class b dor(pair < b, c > d) {
        ris << "(" << d.first << ", " << d.second << ")";
    }
    sim dor(rge<c> d) {
        *this << "[";
        for (auto it = d.b; it != d.e; ++it)
            *this << ", " + 2 * (it == d.b) << *it;
        ris << "]";
    }
#else
    sim dor(const c&) { ris; }
#endif
};
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

/******************************ALGORITHMS******************************/

int __pow(int x, int y, int M) {int res = 1;while(y>0){if(y&1) res = (res*x)%M; y>>=1; x = (x*x)%M;}return res%M;}
int __pow(int x, int y) {int res = 1;while(y>0){if(y&1) res = (res*x); y>>=1; x = (x*x);}return res;}
int mi(int x, int M) {return __pow(x, M-2, M);}
int gcd(int a, int b) {if(b==0) return a; return gcd(b, a % b);}
int add(int a, int b, int M) {return (a+b) % M;}
int mul(int a, int b, int M) {return (a*b) % M;}
int sub(int a, int b, int M) {return (a-b+M) % M;}
int div(int a, int b, int M) {return (a*mi(b, M)) % M;}
void umax(int &a, int b) {a = max(a, b);}
void umin(int &a, int b) {a = min(a, b);}

/******************************main*code******************************/

const int N = 300+2, inf = 1e18, M = 1e9+7;

int Test, n, m, k, query;
double dp[N][N][N];
bool ok = 1;

void test_case()
{
    cin >> n >> m;
    string a[n];
    for(int i = 0; i<n; i++)
        cin >> a[i];
    vt<int> pos[n];
    for(int i = 0; i<n; i++)
        for(int j = 0; j<m; j++)
            if(a[i][j] == '1')
                pos[i].pb(i);
    int ans = inf;
    for(int j = 0; j<m; j++)
    {
        int x = 0;
        for(int i = 0; i<n; i++)
        {
            auto it = lower_bound(all(pos[i]), j);
            if(pos[i].empty())
            {
                cout << -1 << endl;
                return;
            }
            int a1 = inf, a2 = inf;
            if(it != pos[i].end())
                a1 = (it - pos[i].begin()) - j;
            if(it != pos[i].begin())
                a2 = j - (prev(it) - pos[i].begin());
            x += min({a1, a2, n + j - pos[i].back(), n + pos[i][0] - j});
            debug() << deb(j) deb(i) deb(a1) deb(a2);
        }
        ans = min(ans, x);
    }
    cout << abs(ans) << endl;
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    Test = 1;
    //cin >> Test;
    while(Test--)
        test_case(); // I'm screamin' out, no regrets
#ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s." << endl;
#endif
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vb vector<bool>
#define pii pair<int,int>
#define mod 1000000007
#define ss second
#define ff first
#define vpii vector<pii>
#define vvi vector<vi>
#define pb push_back
#define vs vector<string>
#define all(x) x.begin(),x.end()
#define mii map<int,int>
const int inf=0x3f3f3f3f3f3f3f3f;
void print(vi &v){
    for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
    cout<<endl;
}
int power(int a,int b){
    int res = 1;
    while(b!=0){
        if(b&1){
            res *= a;
        }
        a = (a * a);
        b = (b / 2);
    }
    return res;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    // cin>>t;
    t = 1;
    while(t--){
        int n,m,i,j;
        cin>>n>>m;
        vector<vector<char>> v(n,vector<char>(m));
        for(i=0;i<n;i++)for(j=0;j<m;j++)cin>>v[i][j];
        vvi index(n);
        for(i=0;i<n;i++){
        	for(j=0;j<m;j++){
        		if(v[i][j]=='1')index[i].pb(j);
        	}
        	if(index[i].size()==0){
        		cout<<-1<<endl;
        		return 0;
        	}
        }
        int ans = inf;
        for(j=0;j<m;j++){
        	int val = 0;
        	for(i=0;i<n;i++){
        		auto it=lower_bound(all(index[i]),j);
        		// if(it!=index[i].end())cout<<*it<<endl;
        		if(it!=index[i].end() && *it==j){
        			continue;
        		}
        		if(it==index[i].end()){
        			it--;
        			auto it2 = index[i].begin();
        			val +=min(min(m-abs(j-*it),abs(j-*it)),min(m-abs(j-*it2),abs(j-*it2)));
        			// if(j==1)cout<<1<<" "<<val<<endl;
        		}
        		else if(it==index[i].begin()){
        			auto it1=index[i].end(),it2=it;
        			it1--;
        			it2++;
        			if(it2!=index[i].end())
        			val+= min(min(m-abs(j-*it),abs(j-*it)),min(min(m-abs(j-*it1),abs(j-*it1)),min(m-abs(j-*it2),abs(j-*it2))));
        			else{
        				val+= min(min(m-abs(j-*it),abs(j-*it)),min(m-abs(j-*it1),abs(j-*it1)));
        			}
        			// if(j==1)cout<<2<<" "<<val<<endl;
        		}
        		else{
        			auto it1 = it,it2=it,it3=index[i].end();
        			it1++;
        			it2--;
        			it3--;
        			// if(j==1){
        				// if(it1!=index[i].end())cout<<*it1<<endl;
	        			// if(it2!=index[i].end())cout<<*it2<<endl;
	        			// if(it3!=index[i].end())cout<<*it3<<endl;
	        			// if(it!=index[i].end())cout<<*it<<endl;
        			// }
        			if(it1==index[i].end()){
        				val +=min(min(m-abs(j-*it),abs(j-*it)),min(m-abs(j-*it2),abs(j-*it2)));
        			}
        			else{
        				val += min(min(m-abs(j-*it3),abs(j-*it3)),min(min(m-abs(j-*it),abs(j-*it)),min(min(m-abs(j-*it1),abs(j-*it1)),min(m-abs(j-*it2),abs(j-*it2)))));
        			}
        			// if(j==1)cout<<3<<" "<<val<<endl;
        		}
        	}
        	// cout<<val<<endl;
        	ans = min(ans,val);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> v;
ll n,m,c[105][10005],x,y,ans=1e9;
string s;
int main(){
	cin>>n>>m;
	for(int i=1; i<=n; i++){
		cin>>s;
		v.clear();
		for(int j=0; j<s.size(); j++){
			if(s[j]=='1'){
				v.push_back(j);
			}
		}
		if(v.size()==0){
			cout<<-1;
			exit(0);
		}
		if(v.size()==1){
			for(int j=0; j<v[0]; j++){
				x=v[0]-j;
				y=j+m-v[0];
				c[i][j]=min(x,y);
			}
			for(int j=v[0]+1; j<m; j++){
				x=j-v[0];
				y=m-j+v[0];
				c[i][j]=min(x,y);
			}
			continue;
		}
		for(int j=1; j<v.size(); j++){
			ll a=v[j-1];
			ll b=v[j];
			for(int d=a+1; d<b; d++){
				x=d-a;
				y=b-d;
				c[i][d]=min(x,y);
			}
		}
		ll q=v[0];
		ll w=v[v.size()-1];
		for(int j=0; j<q; j++){
			x=q-j;
			y=m-w+j;
			c[i][j]=min(x,y);
		}
		for(int j=w+1; j<m; j++){
			x=j-w;
			y=-j+m+q;
			c[i][j]=min(x,y);
		}
	}
	for(int i=0; i<m; i++){
		ll sum=0;
		for(int j=1; j<=n; j++){
			sum+=c[j][i];
		}
		ans=min(ans,sum);
	}
	cout<<ans;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

int n, m;
char grid[105][10201];
vector <int> one[105];
int best[10201];

int dist (int i, int j)
{
    return min (abs (i - j), m - abs (i - j));
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) best[i] = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%s", grid[i]);
        for (int j = 0; j < m; j++)
        {
            if (grid[i][j] == '1')
            {
                one[i].push_back(j);
            }
        }
        if (one[i].empty())
        {
            printf("-1\n");
            return 0;
        }
        int left = one[i][one[i].size() - 1];
        int right = one[i][0];
        int curi = 0;
        for (int j = 0; j < m; j++)
        {
            best[j] += min (dist (j, left), dist (j, right));
            if (j == right)
            {
                left = right;
                if (curi == one[i].size() - 1)
                {
                    curi = -1;
                }
                right = one[i][++curi];
            }
        }
    }
    int very_best = 321049857;
    for (int i = 0; i < m; i++)
    {
        very_best = min(very_best, best[i]);
    }
    printf("%d\n", very_best);
}


#include "bits/stdc++.h"
using namespace std;

//Utility functions
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(c) (c).begin(),(c).end()
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } //sort array of objects(structs)[Lambda function]
//sort(begin(v), end(v), [] (int a, int b) { return a > b; });

//Constants
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define MOD 998244353 //1000000007

//Printing
#define coutRV(a,L,R) FE(i,L,R) cout<<a[i]<<" \n"[i==R] ;
#define coutV(a) coutRV(a,1,a.size()-1)

//For loops
#define F(i,a,b)   for(int i=a; i<b; i++)
#define FE(i,a,b)  for(int i=a; i<=b; i++)
#define FRE(i,b,a) for(int i=b; i>=a; i--)
#define FA(x,cont) for(auto& x : cont)

//For debug
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//Definitions
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vvi vector<vector<int> > //For UnGraph
#define vvpi vector<vector<pii> > //For DirGraph
#define vviwv(vecname, N, M, value) vector<vector<int> > vecname(N, vector<int> (M, value)) //For DP
#define vvlwv(vecname, N, M, value) vector<vector<ll> > vecname(N, vector<ll> (M, value)) //For DP
#define pii pair<int,int>
#define ll long long
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

//Directions
const int fx[4][2]  = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

//Input
#define cini(i) int i; cin>>i;
#define cini2(i,j) int i,j; cin>>i>>j;
#define cini3(i,j,k) int i,j,k; cin>>i>>j>>k;
#define cini4(i,j,k,l) int i,j,k,l; cin>>i>>j>>k>>l;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cins(s) string s; cin>>s;
#define cinv(a, n) vi a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvll(a, n) vll a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvs(s, n) vs s(n+1); FE(i,1,n) { cin>>s[i]; }
#define cinvd(a, n) vd a(n+1); FE(i,1,n) { cin>>a[i]; }

int main()
{
    fastIO;
    cini2(n,m);
    vviwv(mat,110,10010,-1);

    FE(i,1,n)
    {
        cins(s);
        FE(j,1,m)
            mat[i][j] = s[j-1]-'0';
    }

    vviwv(next,110,10010,-1);

    FE(i,1,n)
    {
        FRE(j,m,1)
        {
            if(mat[i][j]==0)
                next[i][j] = next[i][j+1];
            else
                next[i][j] = j;
        }
    }
    debug(next[1]);
    vviwv(nextl,110,10010,-1);

    FE(i,1,n)
    {
        FE(j,1,m)
        {
            if(mat[i][j]==0)
                nextl[i][j] = nextl[i][j-1];
            else
                nextl[i][j] = j;
        }
    }

    int globalans=INF;

    FE(j,1,m)
    {
        int ans=0;
        FE(i,1,n)
        {
            if(mat[i][j]!=1)
            {
                debug(i,j);
                if(next[i][j]==-1 && nextl[i][j]==-1){
                    ans=INF;
                    break;
                }
                int temp = INF;
                if(next[i][j]!=-1)
                    temp = min({temp,next[i][j]-j,m-(next[i][j]-j)});

                if(nextl[i][j]!=-1)
                    temp = min({temp,j-nextl[i][j],m-(j-nextl[i][j])});

                ans +=temp;
            }
        }
        globalans = min(ans,globalans);
    }

    globalans==INF ? cout<<-1 : cout<<globalans ;
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

//Utility functions
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(c) (c).begin(),(c).end()
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } //sort array of objects(structs)[Lambda function]
//sort(begin(v), end(v), [] (int a, int b) { return a > b; });

//Constants
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define MOD 998244353 //1000000007

//Printing
#define coutRV(a,L,R) FE(i,L,R) cout<<a[i]<<" \n"[i==R] ;
#define coutV(a) coutRV(a,1,a.size()-1)

//For loops
#define F(i,a,b)   for(int i=a; i<b; i++)
#define FE(i,a,b)  for(int i=a; i<=b; i++)
#define FRE(i,b,a) for(int i=b; i>=a; i--)
#define FA(x,cont) for(auto& x : cont)

//For debug
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//Definitions
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vvi vector<vector<int> > //For UnGraph
#define vvpi vector<vector<pii> > //For DirGraph
#define vviwv(vecname, N, M, value) vector<vector<int> > vecname(N, vector<int> (M, value)) //For DP
#define vvlwv(vecname, N, M, value) vector<vector<ll> > vecname(N, vector<ll> (M, value)) //For DP
#define pii pair<int,int>
#define ll long long
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

//Directions
const int fx[4][2]  = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

//Input
#define cini(i) int i; cin>>i;
#define cini2(i,j) int i,j; cin>>i>>j;
#define cini3(i,j,k) int i,j,k; cin>>i>>j>>k;
#define cini4(i,j,k,l) int i,j,k,l; cin>>i>>j>>k>>l;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cins(s) string s; cin>>s;
#define cinv(a, n) vi a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvll(a, n) vll a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvs(s, n) vs s(n+1); FE(i,1,n) { cin>>s[i]; }
#define cinvd(a, n) vd a(n+1); FE(i,1,n) { cin>>a[i]; }

int main()
{
    fastIO;
    cini2(n,m);
    vviwv(mat,110,10010,-1);

    FE(i,1,n)
    {
        cins(s);
        FE(j,1,m)
            mat[i][j] = s[j-1]-'0';
    }

    vviwv(next,110,10010,-1);

    FE(i,1,n)
    {
        FRE(j,m,1)
        {
            if(mat[i][j]==0)
                next[i][j] = next[i][j+1];
            else
                next[i][j] = j;
        }
    }

    vviwv(nextl,110,10010,-1);

    FE(i,1,n)
    {
        FE(j,1,m)
        {
            if(mat[i][j]==0)
                nextl[i][j] = nextl[i][j-1];
            else
                nextl[i][j] = j;
        }
    }

    int globalans=INF;

    FE(j,1,m)
    {
        int ans=0;
        FE(i,1,n)
        {
            if(mat[i][j]!=1)
            {
                debug(i,j);
                if(next[i][j]==-1 && nextl[i][j]==-1){
                    ans=INF;
                    break;
                }
                int temp = INF;
                if(next[i][j]!=-1)
                    temp = min(temp,next[i][j]-j);

                if(nextl[i][j]!=-1)
                    temp = min(temp,j-nextl[i][j]);

                ans +=temp;
            }
        }
        globalans = min(ans,globalans);
    }

    globalans==INF ? cout<<-1 : cout<<globalans ;
    return 0;
}


#include "bits/stdc++.h"
using namespace std;

//Utility functions
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(c) (c).begin(),(c).end()
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } //sort array of objects(structs)[Lambda function]
//sort(begin(v), end(v), [] (int a, int b) { return a > b; });

//Constants
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define MOD 998244353 //1000000007

//Printing
#define coutRV(a,L,R) FE(i,L,R) cout<<a[i]<<" \n"[i==R] ;
#define coutV(a) coutRV(a,1,a.size()-1)

//For loops
#define F(i,a,b)   for(int i=a; i<b; i++)
#define FE(i,a,b)  for(int i=a; i<=b; i++)
#define FRE(i,b,a) for(int i=b; i>=a; i--)
#define FA(x,cont) for(auto& x : cont)

//For debug
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

//Definitions
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vvi vector<vector<int> > //For UnGraph
#define vvpi vector<vector<pii> > //For DirGraph
#define vviwv(vecname, N, M, value) vector<vector<int> > vecname(N, vector<int> (M, value)) //For DP
#define vvlwv(vecname, N, M, value) vector<vector<ll> > vecname(N, vector<ll> (M, value)) //For DP
#define pii pair<int,int>
#define ll long long
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

//Directions
const int fx[4][2]  = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

//Input
#define cini(i) int i; cin>>i;
#define cini2(i,j) int i,j; cin>>i>>j;
#define cini3(i,j,k) int i,j,k; cin>>i>>j>>k;
#define cini4(i,j,k,l) int i,j,k,l; cin>>i>>j>>k>>l;
#define cinll(l) ll l; cin>>l;
#define cind(d) double d; cin>>d;
#define cins(s) string s; cin>>s;
#define cinv(a, n) vi a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvll(a, n) vll a(n+1); FE(i,1,n) { cin>>a[i]; }
#define cinvs(s, n) vs s(n+1); FE(i,1,n) { cin>>s[i]; }
#define cinvd(a, n) vd a(n+1); FE(i,1,n) { cin>>a[i]; }

int main()
{
    fastIO;
    cini2(n,m);
    vviwv(mat,110,10010,-1);

    FE(i,1,n)
    {
        cins(s);
        FE(j,1,m)
            mat[i][j] = s[j-1]-'0';
    }

    vviwv(next,110,10010,-1);

    FE(i,1,n)
    {
        FRE(j,m,1)
        {
            if(mat[i][j]==0)
                next[i][j] = next[i][j+1];
            else
                next[i][j] = j;
        }
    }

    int globalans=INF;

    FE(j,1,m)
    {
        int ans=0;
        FE(i,1,n)
        {
            if(mat[i][j]!=1)
            {
                debug(i,j);
                if(next[i][j]==-1){
                    ans=INF;
                }
                else
                    ans+=next[i][j]-j;
            }
        }
        globalans = min(ans,globalans);
    }

    vviwv(nextl,110,10010,-1);

    FE(i,1,n)
    {
        FE(j,1,m)
        {
            if(mat[i][j]==0)
                nextl[i][j] = nextl[i][j-1];
            else
                nextl[i][j] = j;
        }
    }

    FE(j,1,m)
    {
        int ans=0;
        FE(i,1,n)
        {
            if(mat[i][j]!=1)
            {
                if(nextl[i][j]==-1){
                    ans=INF;
                    //break;
                }
                else
                    ans+=j-nextl[i][j];
            }
        }
        globalans = min(ans,globalans);
    }

    globalans==INF ? cout<<-1 : cout<<globalans ;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define fio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ull unsigned long long
#define ll long long
#define inf INT_MAX
#define mninf INT_MIN
#define infl 1e18+5
#define mninfl -1e18-5
#define Mod 1000000007
#define newmod  998244353
int ah=911382323;
int bh=972663749;
#define endl "\n"
#define cp(x) cout<<(x)<<" "
#define cl(x) cout<<(x)<<endl
#define ct(x,y) cout<<(x)<<" "<<(y)<<endl;
#define nl   cout<<endl
//const long double pi=acos(-1);
//looping
#define int long long
#define For(i,a,b) for(int i=a;i<b;i++)
#define test(t)  int (t); cin>>(t); while((t)--)
#define array(a,n) int (n); cin>>(n); int a[n]; for(int i=0;i<(n);i++) cin>>a[i];
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define pa(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' ';  cout<<endl
#define pv(v)  for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";  cout<<endl
#define pm(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<a[i][j]<<" ";cout<<endl;}cout<<endl
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";
}
template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}
template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}
#define deb(x) cerr << #x << " = " << x << endl;
#define deb2(x,y)  cerr << #x << " = " << x <<"   "<< #y << " = " << y << endl;
//containers
#define F first
#define S second
#define pb emplace_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define all(v) (v).begin(),(v).end()
int dx[8]={1,-1,0,0,1,1,-1,-1};
int dy[8]={0,0,1,-1,1,-1,1,-1};
typedef vector<int> vi;
typedef vector< vector< int > > vvi;
typedef vector < pair <int, int> > vpii;
/*/----------------------------Code begins----------------------------------/*/
int32_t main(){
fio;
int n,m;
cin>>n>>m;
string s[n];
vector<int> v[n];
For(i,0,n)
{cin>>s[i];
int cnt=0;
for(int j=0;j<s[i].size();j++){
	if(s[i][j]=='1')
	{cnt++;
	v[i].pb(j);}
}
if(cnt==0){
	cout<<-1;
	return 0;
}}
int ans=inf;
for(int j=0;j<m;j++){
	int sum=0;
	for(int i=0;i<n;i++){
	auto x=lower_bound(all(v[i]),j);
	int dist=inf;
	if((x!=v[i].end())&&*x==j){
		dist=0;
		continue;//sum+=0
	}
	if(x==v[i].end()){
		int d=m-j+*(v[i].begin());
		dist=min(j-*v[i].rbegin(),d);
	}
	else if(x==v[i].begin()){
			int d=m-*v[i].rbegin()+j;
		dist=min(*v[i].begin()-j,d);
	}
	else {
		int d=*x-j;
		x--;
		dist=min(d,j-*x);
	}
	//cout<<i<<" "<<j<<" "<<dist<<endl;
	sum+=dist;
	}
	ans=min(ans,sum);
}
cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cerr.tie(NULL);
#define pb insert
#define PI 3.14159265
#define pii pair<int,int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

char a[102][10004];
int dpl[102][10004],dpr[102][10004];

int main()
{
	fast;
	int t=1;
	//cin >>t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin >> a[i][j];

		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++) {
				if(a[i][j]=='1') dpl[i][j]=j;
				else {
					if(j) dpl[i][j]=dpl[i][j-1];
					else dpl[i][j]=-1;
				}
			}
			for(int j=m-1;j>=0;j--) {
				if(a[i][j]=='1') dpr[i][j]=j;
				else {
					if(j!=m-1) dpr[i][j]=dpr[i][j+1];
					else dpr[i][j]=-1;
				}
			}
		}
		int mnsum=31231414;
		for(int j=0;j<m;j++) {
			int sum=0;
			for(int i=0;i<n;i++) {
				if(dpl[i][j]==dpr[i][j] && dpl[i][j]==-1) {
					cout<<-1;
					return 0;
				}
				int l,r;
				if(dpl[i][j]==-1) l=(m-dpl[i][m-1])+j;
				else l=j-dpl[i][j];

				if(dpr[i][j]==-1) r=dpr[i][0]+(m-j);
				else r=dpr[i][j]-j;
				sum+=min(l,r);
			}
			mnsum=min(mnsum,sum);
		}
		cout<<mnsum;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cerr.tie(NULL);
#define pb insert
#define PI 3.14159265
#define pii pair<int,int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

char a[313][132];

int main()
{
	fast;
	int t=1;
	//cin >>t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		for(int i=0;i<n;i++){
			bool ok = false;
		for(int j=0;j<m;j++) {
			cin >> a[i][j];
			if(a[i][j]=='1') ok=true;
		}
		if(!ok) {
			cout<<-1;
			return 0;
		}
		}
		int mnsum=31231414;
		for(int j=0;j<m;j++) {
			int sum=0;
			for(int i=0;i<n;i++) {
				int k=j,ans=4*n;
				while(a[i][k]!='1' && k<m) k++;
				if(k<m) ans=k-j;
				else {
					k=0;
					while(a[i][k]!='1') k++;
					ans=k+(m-j);
				}
				k=j;
				while(a[i][k]!='1' && k>=0) k--;
				if(k<0) {
					k=m-1;
					while(a[i][k]!='1') k--;
					ans=min(ans,j+(m-k));
				}
				else ans=min(ans,j-k);

				sum+=ans;
			}
			mnsum=min(mnsum,sum);
		}
		cout<<mnsum;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cerr.tie(NULL);
#define pb insert
#define PI 3.14159265
#define pii pair<int,int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

char a[102][10004];
int dpl[102][10004],dpr[102][10004];

int main()
{
	fast;
	int t=1;
	//cin >>t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			cin >> a[i][j];

		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++) {
				if(a[i][j]=='1') dpl[i][j]=j;
				else {
					if(j) dpl[i][j]=dpl[i][j-1];
					else dpl[i][j]=-1;
				}
			}
			for(int j=m-1;j>=0;j--) {
				if(a[i][j]=='1') dpr[i][j]=j;
				else {
					if(j!=m-1) dpr[i][j]=dpr[i][j+1];
					else dpr[i][j]=-1;
				}
			}
		}
		int mnsum=31231414;
		for(int j=0;j<m;j++) {
			int sum=0;
			for(int i=0;i<n;i++) {
				if(dpl[i][j]==dpr[i][j] && dpl[i][j]==-1) {
					cout<<-1;
					return 0;
				}
				int l,r;
				if(dpl[i][j]==-1) l=dpl[i][m-1]+j+1;
				else l=j-dpl[i][j];

				if(dpr[i][j]==-1) r=dpr[i][0]+(m-j);
				else r=dpr[i][j]-j;
				sum+=min(l,r);
			}
			mnsum=min(mnsum,sum);
		}
		cout<<mnsum;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cerr.tie(NULL);
#define pb insert
#define PI 3.14159265
#define pii pair<int,int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

char a[102][10004];

int main()
{
	fast;
	int t=1;
	//cin >>t;
	while(t--)
	{
		int n,m;
		cin >> n >> m;
		for(int i=0;i<n;i++){
			bool ok = false;
		for(int j=0;j<m;j++) {
			cin >> a[i][j];
			if(a[i][j]=='1') ok=true;
		}
		if(!ok) {
			cout<<-1;
			return 0;
		}
		}
		int mnsum=31231414;
		for(int j=0;j<m;j++) {
			int sum=0;
			for(int i=0;i<n;i++) {
				int k=j,ans=4*n;
				while(a[i][k]!='1' && k<m) k++;
				if(k<m) ans=k-j;
				else {
					k=0;
					while(a[i][k]!='1') k++;
					ans=k+(m-j);
				}
				k=j;
				while(a[i][k]!='1' && k>=0) k--;
				if(k<0) {
					k=m-1;
					while(a[i][k]!='1') k--;
					ans=min(ans,j+(m-k));
				}
				else ans=min(ans,j-k);

				sum+=ans;
			}
			mnsum=min(mnsum,sum);
		}
		cout<<mnsum;

	}

	return 0;
}


#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define ul unsigned long long
#define ll long long
#define re return
#define ldb long double
#define db double
#define str string

#define fr first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define in_ insert

#define pll pair<ll,ll>
#define pdb pair<db,db>

#define vi vector<ll>
#define vpll vector< pll >

#define mll map<ll,ll>
#define mldb map<ldb,ldb>
#define forn(i, n) for (int i = 0; i < int(n); i++)

#define forr(i,a,b) for (int i = (a); i < (b); ++i)
#define rfor(i,a,b) for (int i = (b)-1; i >= (a); --i)
char a[105][10005];

ll bins(ll ar[], ll n, ll x)
{
   ll l=0,r=n-1;
   while(l<=r)
   {
       ll mid=(l+r)/2;
       if (ar[mid]>x)
        r=mid-1;
        else if (ar[mid]<x)
        l=mid+1;
       else
        return mid+1;
   }
   return l+1;
}

bool seive(ll a)
{
    for (ll i=2;i<=sqrt(a);i++)
    {
        if (a%i==0)
            return false;
    }
    return true;
}
const int inf = (ll) 1e9;
int main()
{
    int q;
     q=1;
   //cin>>q;
    while(q--)
    {
        ll n,m;
 cin>>n>>m;
 vector<vector<ll> >v;
 v.assign(101,vi());
 for(ll i=0;i<n;i++)
 {
     for(ll j=0;j<m;j++)
     {
     cin>>a[i][j];
     if(a[i][j]=='1')
     v[i].pb(j);
     }
 }
 ll c=0;
 for(ll i=0;i<m;i++)
 {
     c=0;
     for(ll j=0;j<n;j++)
     {
         if(a[j][i]=='1')
         c++;
     }
     if(c==n)
     {
         cout<<0;
        return 0;
     }
 }
//  for(ll i=0;i<n;i++)
//  {
//      cout<<v[i].size();
//  }
 ll dd=100000000;
 ll cnt=0;
 for(ll j=0;j<m;j++)
 {
       cnt=0;
     for(ll i=0;i<n;i++)
     {
         if(v[i].size()==0)
         {
             cout<<-1;
             return 0;
         }
         ll qq;
        ll tt;
         ll rr;
         ll ans=100000;
         //cout<<v[i][0];
       ll l=0;
       ll h=v[i].size()-1;
       ll y2=v[i][0];
       ll y1=v[i][v[i].size()-1];
       ll mid;
       while(l<=h)
       {
           mid=l+(h-l)/2;
           if(v[i][mid]<=j)
           {
               y1=v[i][mid];
               l=mid+1;
           }
           else
           h=mid-1;
       }
       l=0;
        h=v[i].size()-1;
        while(l<=h)
       {
           mid=l+(h-l)/2;
           if(v[i][mid]>=j)
           {
               y2=v[i][mid];
               h=mid-1;
           }
           else
           l=mid+1;
       }
       tt=min(abs(j-y2),m-abs(j-y2));
       qq=min(abs(j-y1),m-abs(j-y1));
       rr=min(tt,qq);
       //cout<<rr<<" ";
        cnt+=rr;

     }
     //cout<<cnt;
     if(cnt<dd)
     dd=cnt;
  }
 cout<<dd;
    }
	return 0;
}


#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define ul unsigned long long
#define ll long long
#define re return
#define ldb long double
#define db double
#define str string

#define fr first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define in_ insert

#define pll pair<ll,ll>
#define pdb pair<db,db>

#define vll vector<ll>
#define vpll vector< pll >

#define mll map<ll,ll>
#define mldb map<ldb,ldb>
#define forn(i, n) for (int i = 0; i < int(n); i++)

#define forr(i,a,b) for (int i = (a); i < (b); ++i)
#define rfor(i,a,b) for (int i = (b)-1; i >= (a); --i)

ll bins(ll ar[], ll n, ll x)
{
   ll l=0,r=n-1;
   while(l<=r)
   {
       ll mid=(l+r)/2;
       if (ar[mid]>x)
        r=mid-1;
        else if (ar[mid]<x)
        l=mid+1;
       else
        return mid+1;
   }
   return l+1;
}

bool seive(ll a)
{
    for (ll i=2;i<=sqrt(a);i++)
    {
        if (a%i==0)
            return false;
    }
    return true;
}
const int inf = (ll) 1e9;
int main()
{
    int q;
     q=1;
   //cin>>q;
    while(q--)
    {
        ll n,m;
        cin>>n>>m;
        string s[n];
       // for(ll i=0;i<n;i++)
            for(int i=0;i<n;i++){
        cin>>s[i];
        s[i]=s[i]+s[i];
    }
        ll ar[n][m],br[n][m],fin[n][m],sum=0,ans=inf;
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {
                ar[i][j]=inf;
                br[i][j]=inf;
            }
        }
        for (ll i=0;i<n;i++)
        {
            if (s[i][0]=='1')
                ar[i][0]=0;
            for (ll j=1;j<m;j++)
            {
                if (s[i][j]=='1')
                ar[i][j]=0;
                else if (s[i][j]=='0' && ar[i][j-1]!=inf)
                    ar[i][j]=ar[i][j-1]+1;
            }
        }
        for (ll i=0;i<n;i++)
        {
            if (s[i][m-1]=='1')
                br[i][m-1]=0;
            for (ll j=m-2;j>=0;j--)
            {
                if (s[i][j]=='1')
                br[i][j]=0;
                else if (s[i][j]=='0' && br[i][j+1]!=inf)
                    br[i][j]=br[i][j+1]+1;
            }
        }
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {fin[i][j]=min(ar[i][j],br[i][j]);}
        }
        for (ll j=0;j<m;j++)
        {
            for (ll i=0;i<n;i++)
                sum+=fin[i][j];
            ans=min(sum,ans);
            sum=0;
        }
        if (ans>=inf)
            cout<<"-1";
        else
        cout<<ans;
    }
	return 0;
}


/*
                        ********************
			**  MANAN DIYORA  **
			**    DA-IICT     **
			**   diyora13     **
			********************
*/
#include <bits/stdc++.h>
using namespace std;

//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define setpre(x,d) fixed<<setprecision(d)<<x
#define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i]
#define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
#define maxa(a,n) *max_element(a,a+n)
#define mina(a,n) *min_element(a,a+n)
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define rf(i,s,e) for(ll i=s-1;i>=e;i--)
#define deb1(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz size()
#define len length()
#define all(v) v.begin(),v.end()
#define PI 3.141592653589793
#define MOD 1000000007
#define NN (const int)5e5+10

typedef long long int ll;
typedef long double lld;
typedef pair<long long,long long> pll;
typedef vector<long long> vll;
typedef vector<pair<long long,long long>> vpll;

// bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); }
// void fileio()	{	freopen("input.txt","r",stdin);	freopen("output.txt","w",stdout);	}
//	vll fact(NN),invfact(NN);
//	void EE(ll a,ll b,ll &x,ll &y) {	if(b==0) {	x=1,y=0; return ;	}		EE(b,a%b,x,y);	ll temp=x; x=y; y=temp-(a/b)*y;	}
//	ll inverse(ll a,ll mod) 	{	ll x,y;	EE(a,mod,x,y);	return (x+mod)%mod;	}
//	void build() { fact[0]=1; fr(i,1,NN) fact[i]=(fact[i-1]*i)%MOD;	invfact[NN-1]=inverse(fact[NN-1],MOD); rf(i,NN-1,0) invfact[i]=(invfact[i+1]*(i+1))%MOD; }
//	ll ncr(ll n,ll r)       	{	if(n<0 || r<0 || r>n) return 0;	return (fact[n]*((invfact[n-r]*invfact[r])%MOD))%MOD;	}
//	ll choose(ll n,ll k) 	  	{	if(k==0)  return 1; return (n* choose(n-1,k-1))/k;	}
ll logy(ll x,ll y)	{	return ceil((lld)(log(x)/log(y)));	}
ll powe(ll a,ll b)	{	ll re=1;	while(b){	if(b&1) re=(re*a);	a=(a*a);	b=b>>1;}	return re;	}

bool flag=0,flag1=0,flag2=0;

int main()
{
	IOS
	ll tt=1;
//		cin>>tt;

	while(tt--)
	{
		ll n,m; cin>>n>>m;
		string s[n]; inarr(s,n);
		ll a[n][m],r[n];
		fr(i,0,n) r[i]=-1;
		fr(i,0,n) fr(j,0,m) a[i][j]=MOD;
		fr(i,0,n)
		{
			fr(j,0,m)
				if(s[i][j]=='1')
				{
					r[i]=j;
					break;
				}
			if(r[i]==-1)
			{
				cout<<-1;
				return 0;
			}
		}
		fr(i,0,n)
		{
			ll c=0;
			fr(j,0,m)
			{
				if(s[i][(j+r[i])%m]=='1') c=0;
				a[i][(j+r[i])%m]=c++;
			}
			c=0;
			fr(j,0,m)
			{
				if(s[i][(r[i]-j+m)%m]=='1') c=0;
				a[i][(r[i]-j+m)%m]=min(a[i][(r[i]-j+m)%m],c++);
			}
		}
		ll ans=1e10,c=0;
		fr(j,0,m)
		{
			c=0;
			fr(i,0,n) c+=a[i][j];
			ans=min(ans,c);
		}
		cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 1000000007
#define v1d vector<int>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

int32_t main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    IOS;
    int n,m;cin>>n>>m;
    v2d v(n+1,v1d(m+1,mod));
    for(int i=1;i<=n;i++){
        string s;cin>>s;cout<<s<<endl;
        s="0"+s;
        bool flag=true;
        int x=0;
        for(int j=1;j<=m;j++){
            if(s[j]=='1'){
                x=j;
                flag=false;
                v[i][j]=0;
                int p=j-1;
                int c=0;
                while(s[p]=='0'){
                    c++;
                    if(p==0){
                        p=m;
                    }
                    v[i][p]=min(c,v[i][p]);
                    p--;
                }
            }
            else{
                if(x)
                    v[i][j]=min(j-x,v[i][j]);
            }
        }
        int p=1;
        while(s[p]!='1'){
            v[i][p]=min(m-x+p,v[i][p]);
            p++;
        }
        if(flag){
            cout<<"-1";return 0;
        }
    }

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

    int ans=100000000000;
    for(int j=1;j<=m;j++){
        int res=0;
        for(int i=1;i<=n;i++){
            res+= v[i][j];
        }
        ans= min(res,ans);
    }
    cout<<ans;

  return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 1000000007
#define v1d vector<int>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

int32_t main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    IOS;
    int n,m;cin>>n>>m;
    v2d v(n+1,v1d(m+1,mod));
    for(int i=1;i<=n;i++){
        string s;cin>>s;//cout<<s<<endl;
        s="0"+s;
        bool flag=true;
        int x=0;
        for(int j=1;j<=m;j++){
            if(s[j]=='1'){
                x=j;
                flag=false;
                v[i][j]=0;
                int p=j-1;
                int c=0;
                while(s[p]=='0'){//cout<<"Y";
                    if(p==0){
                        p=m;
                        continue;
                    }
                    c++;
                    v[i][p]=min(c,v[i][p]);
                    p--;
                }
            }
            else{
                if(x)
                    v[i][j]=min(j-x,v[i][j]);
            }
        }
        if(flag){
            cout<<"-1";return 0;
        }
        int p=1;
        while(s[p]!='1'){
            v[i][p]=min(m-x+p,v[i][p]);
            p++;
        }
    }

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

    int ans=100000000000;
    for(int j=1;j<=m;j++){
        int res=0;
        for(int i=1;i<=n;i++){
            res+= v[i][j];
        }
        ans= min(res,ans);
    }
    cout<<ans;

  return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 1000000007
#define v1d vector<int>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

int32_t main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    IOS;
    int n,m;cin>>n>>m;
    v2d v(n+1,v1d(m+1,mod));
    for(int i=1;i<=n;i++){
        string s;cin>>s;//cout<<s<<endl;
        s="0"+s;
        bool flag=true;
        int x=0;
        for(int j=1;j<=m;j++){
            if(s[j]=='1'){
                x=j;
                flag=false;
                v[i][j]=0;
                int p=j-1;
                int c=0;
                while(s[p]=='0'){
                    c++;
                    if(p==0){
                        p=m;
                    }
                    v[i][p]=min(c,v[i][p]);
                    p--;
                }
            }
            else{
                if(x)
                    v[i][j]=min(j-x,v[i][j]);
            }
        }
        int p=1;
        while(s[p]!='1'){
            v[i][p]=min(m-x+p,v[i][p]);
            p++;
        }
        if(flag){
            cout<<"-1";return 0;
        }
    }

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

    int ans=100000000000;
    for(int j=1;j<=m;j++){
        int res=0;
        for(int i=1;i<=n;i++){
            res+= v[i][j];
        }
        ans= min(res,ans);
    }
    cout<<ans;

  return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 1000000007
#define v1d vector<int>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

int32_t main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    IOS;
    int n,m;cin>>n>>m;
    v2d v(n+1,v1d(m+1,mod));
    for(int i=1;i<=n;i++){
        string s;cin>>s;//cout<<s<<endl;
        s="0"+s;
        bool flag=true;
        int x=0;
        for(int j=1;j<=m;j++){
            if(s[j]=='1'){
                x=j;
                flag=false;
                v[i][j]=0;
                int p=j-1;
                int c=0;
                while(s[p]=='0'){//cout<<"Y";
                    c++;
                    if(p==0){
                        p=m;
                        continue;
                    }
                    v[i][p]=min(c,v[i][p]);
                    p--;
                }
            }
            else{
                if(x)
                    v[i][j]=min(j-x,v[i][j]);
            }
        }
        if(flag){
            cout<<"-1";return 0;
        }
        int p=1;
        while(s[p]!='1'){
            v[i][p]=min(m-x+p,v[i][p]);
            p++;
        }
    }

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

    int ans=100000000000;
    for(int j=1;j<=m;j++){
        int res=0;
        for(int i=1;i<=n;i++){
            res+= v[i][j];
        }
        ans= min(res,ans);
    }
    cout<<ans;

  return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 1000000007
#define v1d vector<int>
#define v2d vector<vector<int>>
#define v1s vector<string>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >

int32_t main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    IOS;
    int n,m;cin>>n>>m;
    v2d v(n+1,v1d(m+1,mod));
    for(int i=1;i<=n;i++){
        string s;cin>>s;//cout<<s<<endl;
        s="0"+s;
        bool flag=true;
        int x=0;
        for(int j=1;j<=m;j++){
            if(s[j]=='1'){
                x=j;
                flag=false;
                v[i][j]=0;
                int p=j-1;
                int c=0;
                while(s[p]=='0'){
                    c++;
                    if(p==0){
                        p=m;
                    }
                    v[i][p]=min(c,v[i][p]);
                    p--;
                }
            }
            else{
                if(x)
                    v[i][j]=min(j-x,v[i][j]);
            }
        }
        if(flag){
            cout<<"-1";return 0;
        }
        int p=1;
        while(s[p]!='1'){
            v[i][p]=min(m-x+p,v[i][p]);
            p++;
        }
    }

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

    int ans=100000000000;
    for(int j=1;j<=m;j++){
        int res=0;
        for(int i=1;i<=n;i++){
            res+= v[i][j];
        }
        ans= min(res,ans);
    }
    cout<<ans;

  return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
typedef map<pi,int> mpi;
typedef map<int,vii> mivi;
typedef map<pi,vi> mpivi;
void solve()
{
    int n,m;cin>>n>>m;
    char a[n][m];fri(n)frj(m)cin>>a[i][j];
    int l[n][m],r[n][m];
    fri(n)
    {
        int j;
        for(j=0;j<m;j++){
            if(a[i][j]=='1')br;
        }
        if(j==m){cout<<-1;return;}
        if(a[i][m-1]=='0')r[i][m-1]=j+1;
        else r[i][m-1]=0;
        for(int j=m-2;j>=0;j--){
            if(a[i][j]=='0')r[i][j]=r[i][j+1]+1;
            else r[i][j]=0;
        }
        for(j=m-1;j>=0;j--){
            if(a[i][j]=='1')br;
        }
        if(a[i][0]=='0')l[i][0]=m-j;
        else l[i][0]=0;
        for(int j=1;j<m;j++){
            if(a[i][j]=='0')l[i][j]=l[i][j-1]+1;
            else l[i][j]=0;
        }
    }
    fri(m)
    {
        for(int j=1;j<n;j++){
            l[j][i]+=l[j-1][i];
            r[j][i]+=r[j-1][i];
        }
    }
    int res[m];
    fri(m)
    {
        res[i]=min(l[n-1][i],r[n-1][i]);
    }
    cout<<*min_element(res,res+n);
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //T(t)
    {
        solve();
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
typedef map<pi,int> mpi;
typedef map<int,vii> mivi;
typedef map<pi,vi> mpivi;
void solve()
{
    int n,m;cin>>n>>m;
    char a[n][m];fri(n)frj(m)cin>>a[i][j];
    int l[n][m],r[n][m];
    fri(n)
    {
        int j;
        for(j=0;j<m;j++){
            if(a[i][j]=='1')br;
        }
        if(j==m){cout<<-1;return;}
        if(a[i][m-1]=='0')r[i][m-1]=j+1;
        else r[i][m-1]=0;
        for(int j=m-2;j>=0;j--){
            if(a[i][j]=='0')r[i][j]=r[i][j+1]+1;
            else r[i][j]=0;
        }
        for(j=m-1;j>=0;j--){
            if(a[i][j]=='1')br;
        }
        if(a[i][0]=='0')l[i][0]=m-j;
        else l[i][0]=0;
        for(int j=1;j<m;j++){
            if(a[i][j]=='0')l[i][j]=l[i][j-1]+1;
            else l[i][j]=0;
        }
    }
    int res[n][m],ans=INT_MAX;
    fri(n)frj(m)res[i][j]=min(l[i][j],r[i][j]);
    fri(m)
    {
        for(int j=1;j<n;j++){
            res[j][i]+=res[j-1][i];
        }
        ans=min(ans,res[n-1][i]);
    }
    cout<<ans;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //T(t)
    {
        solve();
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
typedef map<pi,int> mpi;
typedef map<int,vii> mivi;
typedef map<pi,vi> mpivi;
void solve()
{
    int n,m;cin>>n>>m;
    char a[n][m];fri(n)frj(m)cin>>a[i][j];
    int l[n][m],r[n][m];
    fri(n)
    {
        int j;
        for(j=0;j<m;j++){
            if(a[i][j]=='1')br;
        }
        if(j==m){cout<<-1;return;}
        if(a[i][m-1]=='0')r[i][m-1]=j+1;
        else r[i][m-1]=0;
        for(int j=m-2;j>=0;j--){
            if(a[i][j]=='0')r[i][j]=r[i][j+1]+1;
            else r[i][j]=0;
        }
        for(j=m-1;j>=0;j--){
            if(a[i][j]=='1')br;
        }
        if(a[i][0]=='0')l[i][0]=m-j;
        else l[i][0]=0;
        for(int j=1;j<m;j++){
            if(a[i][j]=='0')l[i][j]=l[i][j-1]+1;
            else l[i][j]=0;
        }
    }
    fri(m)
    {
        for(int j=1;j<n;j++){
            l[j][i]+=l[j-1][i];
            r[j][i]+=r[j-1][i];
        }
    }
    int res[m];
    fri(m)
    {
        res[i]=min(l[n-1][i],r[n-1][i]);
    }
    cout<<*min_element(res,res+m);
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //T(t)
    {
        solve();
    }
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
mt19937 rng(high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	vector<string> a(n);
	copy_n(istream_iterator<string>(cin), n, a.begin());
	vector<int> res(m);
	for(auto s: a){
		vector<int> cur(m, -1);
		deque<int> q;
		for(int i = 0; i < m; ++ i){
			if(s[i] == '1'){
				q.push_back(i);
				cur[i] = 0;
			}
		}
		if(q.empty()){
			cout << -1;
			return 0;
		}
		while(!q.empty()){
			int u = q.front();
			q.pop_front();
			int prev = (u - 1 + m) % m, next = (u + 1) % m;
			if(cur[prev] == -1){
				cur[prev] = cur[u] + 1;
				q.push_back(prev);
			}
			if(cur[next] == -1){
				cur[next] = cur[u] + 1;
				q.push_back(next);
			}
		}
		for(int i = 0; i < m; ++ i){
			res[i] += cur[i];
		}
	}
	cout << *min_element(res.begin(), res.end());
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
//                                   Coded by Aeren                                   //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////////


#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>

using namespace std;
#define ll long long
#define pb push_back
#define mkp make_pair
int main()
{
    ll i,j,k,l,n,m,t;
    t=1;
    //cin>>t;
    while(t--)
    {
        cin>>n;
    int m;
    cin>>m;
    string s[n];
    for(int i=0;i<n;i++){
        cin>>s[i];
        s[i]=s[i]+s[i];
    }
    m=2*m;
    int ans[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            ans[i][j]=INT_MAX;
        }
    }
    int ans1=INT_MAX;
    for(int i=0;i<n;i++){
        int x=INT_MIN/2,y=0;
        for(int j=0;j<m;j++){

            if(s[i][j]=='1')x=j;
            if(x!=INT_MIN/2)
            ans[i][j]=min(ans[i][j],min(j-x,m-(j-x)));

        }
        x=INT_MAX/2;
        for(int j=m-1;j>=0;j--){
            if(s[i][j]=='1')x=j;
            if(x!=INT_MAX/2)
            ans[i][j]=min(ans[i][j],min(x-j,m-(x-j)));
            y+=ans[i][j];
        }

    }
    //cout<<ans[0][0];
    for(int i=0;i<m;i++){
        int x=0;
        for(int j=0;j<n;j++)x+=ans[j][i];
        ans1=min(ans1,x);
    }
    if(ans1>INT_MAX/4 || ans1<=-1)
        cout<<-1;
    else cout<<ans1;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>

using namespace std;
#define ll long long
#define pb push_back
#define mkp make_pair
int main()
{
    ll i,j,k,l,n,m,t;
    t=1;
    //cin>>t;
    while(t--)
    {
        cin>>n;
    int m;
    cin>>m;
    string s[n];
    for(int i=0;i<n;i++){
        cin>>s[i];
        s[i]=s[i]+s[i];
    }
    m=2*m;
    int ans[n][m];
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            ans[i][j]=INT_MAX;
        }
    }
    int ans1=INT_MAX;
    for(int i=0;i<n;i++){
        int x=INT_MIN/2,y=0;
        for(int j=0;j<m;j++){

            if(s[i][j]=='1')x=j;
            if(x!=INT_MIN/2)
            ans[i][j]=min(ans[i][j],min(j-x,m-(j-x)));

        }
        x=INT_MAX/2;
        for(int j=m-1;j>=0;j--){
            if(s[i][j]=='1')x=j;
            if(x!=INT_MAX/2)
            ans[i][j]=min(ans[i][j],min(x-j,m-(x-j)));
            y+=ans[i][j];
        }

    }
    //cout<<ans[0][0];
    for(int i=0;i<m;i++){
        int x=0;
        for(int j=0;j<n;j++)x+=ans[j][i];
        ans1=min(ans1,x);
    }
    if(ans1>INT_MAX/4)cout<<-1;
    else cout<<ans1;
    }
    return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>

using namespace std;
#define ll long long
#define pb push_back
#define mkp make_pair
int main()
{
    ll i,j,k,l,n,m,t;
    t=1;
    //cin>>t;
    while(t--)
    {
        cin>>n>>m;
        vector<string> s;
        vector<vector<ll> > a(n,vector<ll> (m,INT_MAX));
        for(k=0;k<n;k++)
        {
            string p;
            cin>>p;
            string q;
            q=p;
            s.pb(p);
            ll ct=0;
            //cout<<p;
            for(i=0;i<m;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(p[j]=='1')
                        a[k][j]=min(a[k][j],ct);
                }

                ct++;
                //if(i==m-1)
                //{
                   // q=p;
                    //cout<<p<<"FDF"<<endl;
               // }

                string z;
                z=p[m-1];
                p[m-1]='\0';
                string b;
                b+=z;
                b+=p;
                p=b;
                //cout<<p<<endl;
            }
            //for(i=0;i<m;i++)
            //{
              //  cout<<a[0][i]<<" ";
            //}
            //cout<<endl;
            //reverse(q.begin(),q.end());
            //cout<<"DFDF"<<q<<endl;
            p=q;
            ct=0;
            for(i=0;i<m;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(p[j]=='1')
                        a[k][j]=min(a[k][j],ct);
                }
                ct++;
                q.clear();
                for(j=1;j<m;j++)
                {
                    q+=p[j];
                }
                q+=p[0];
                p=q;
              //cout<<p<<endl;
            }
           // for(i=0;i<m;i++)
            //{
              //  cout<<a[0][i]<<" ";
            //}
        }
        ll sum=INT_MAX;
        for(i=0;i<m;i++)
        {
            ll sm=0;
            for(j=0;j<n;j++)
            {
                sm+=a[j][i];
                if(a[j][i]==INT_MAX)
                {
                    cout<<-1<<endl;
                    return 0;
                }
            }
            sum=min(sm,sum);
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long ll;
struct Node
{
    map<int,int> hash;
    vector<int> rel,dis;
}P[111111];

int N, M;
int dis[111111];
queue<int> q;
bool inq[111111];

void spfa() {
	int i, j, k, u, v;
	for (u = 1; u <= N; ++u)
		dis[u] = 0x3fffffff;
	memset(inq, 0, sizeof inq);
	dis[1] = 0;
	q.push(1);
	inq[1] = 1;
	while (!q.empty()) {
		u = q.front();
		q.pop();
		inq[u] = 0;
		for (i = 0; i < P[u].rel.size(); ++i) {
			v = P[u].rel[i];
			int d = P[u].hash.find(dis[u])==P[u].hash.end()?dis[u]:P[u].hash[dis[u]];
			if (dis[v]>d+P[u].dis[i]) {
				dis[v] = d+P[u].dis[i];
				if (!inq[v]) {
					q.push(v);
					inq[v] = 1;
				}
			}
		}
	}
}
int V[111111];
int main()
{
    int x, y, i, j, c;
    while (~scanf("%d%d",&N,&M))
    {
        for (i=1;i<=N;i++)
        {
            P[i].hash.clear();
            P[i].rel.clear();
            P[i].dis.clear();
        }
        for (i=0;i<M;i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            P[x].rel.push_back(y);
            P[y].rel.push_back(x);
            P[x].dis.push_back(c);
            P[y].dis.push_back(c);
        }
        for (i=1;i<=N;i++)
        {
            scanf("%d",&x);
            for (j=0;j<x;j++)
                scanf("%d",&V[j]);
            if (x>0)
            {
                P[i].hash[V[x-1]]=V[x-1]+1;
                for (j=x-2;j>=0;j--)
                {
                    if (V[j]+1==V[j+1])
                        P[i].hash[V[j]]=P[i].hash[V[j+1]];
                    else P[i].hash[V[j]]=V[j]+1;
                }
            }
        }
        spfa();
		printf("%d\n", dis[N]<0x3fffffff?dis[N]:-1);
    }
}


#include <bits/stdc++.h>
using namespace std;

char a[100][10000];
int dist[100][10000];

int vals[10000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> a[i][j];

    for(int i = 0; i < n; i++)
    {
        int lastOne = -1, firstOne = -1;
        for(int j = 0; j < m; j++)
            if(a[i][j] == '1')
            {
                firstOne = j;
                break;
            }

        if(firstOne == -1)
        {
            cout << -1;
            return 0;
        }

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

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

        curOne = -1;
        for(int j = m-1; j >= 0; j--)
        {
            if(a[i][j] == '1')
                curOne = j;
            if(curOne == -1)
                dist[i][j] = min(m - j + firstOne, dist[i][j]);
            else
                dist[i][j] = min(dist[i][j], curOne - j);
        }
    }

//    for(int i = 0; i < n; i++)
//    {
//        for(int j = 0; j < m; j++)
//            cout << dist[i][j];
//        cout << '\n';
//    }

    for(int j = 0; j < m; j++)
    {
        for(int i = 0; i < n; i++)
        {
            vals[j] += dist[i][j];
        }
    }

    int answer = vals[0];
    for(int i = 0; i < m; i++)
        answer = min(answer, vals[i]);

    cout << answer;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char a[100][1000];
int dist[100][1000];

int vals[1000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> a[i][j];

    for(int i = 0; i < n; i++)
    {
        int lastOne = -1, firstOne = -1;
        for(int j = 0; j < m; j++)
            if(a[i][j] == '1')
            {
                firstOne = j;
                break;
            }

        if(firstOne == -1)
        {
            cout << -1;
            return 0;
        }

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

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

        curOne = -1;
        for(int j = m-1; j >= 0; j--)
        {
            if(a[i][j] == '1')
                curOne = j;
            if(curOne == -1)
                dist[i][j] = min(m - j + firstOne, dist[i][j]);
            else
                dist[i][j] = min(dist[i][j], curOne - j);
        }
    }

//    for(int i = 0; i < n; i++)
//    {
//        for(int j = 0; j < m; j++)
//            cout << dist[i][j];
//        cout << '\n';
//    }

    for(int j = 0; j < m; j++)
    {
        for(int i = 0; i < n; i++)
        {
            vals[j] += dist[i][j];
        }
    }

    int answer = vals[0];
    for(int i = 0; i < m; i++)
        answer = min(answer, vals[i]);

    cout << answer;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
vector<set<int> > vs1;
vector<set<int, greater<int> > > vs2;
vs1.assign(n, set<int>());
vs2.assign(n, set<int, greater<int> >());
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
for(int i=0; i<n; i++){
    for(int j=0; j<3; j++){
        for(int k=0; k<m; k++){
            if(j==0 && arr[i][k]=='1'){
                vs1[i].insert(k);
                vs1[i].insert(k-m);
                vs1[i].insert(k+m);
                vs2[i].insert(k);
                vs2[i].insert(k-m);
                vs2[i].insert(k+m);
            }
        }
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=*vs1[i].lower_bound(j), second=*vs2[i].lower_bound(j);
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}
cout<<mini;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int lef[500][50000], righ[500][50000];
int main(){
int n, m;
cin>>n>>m;
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
cout<<"HI"<<'\n';
for(int i=0; i<n; i++){
    int d=0;
    for(int j=m-1; j>=0; j--){
        d--;
        if(arr[i][j]=='1'){
            break;
        }
    }
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            d=j;
        lef[i][j]=d;
    }
    d=m-1;
    for(int j=0; j<m; j++){
        d++;
        if(arr[i][j]=='1')
            break;
    }
    for(int j=m-1; j>=0; j--){
        if(arr[i][j]=='1')
            d=j;
        righ[i][j]=d;
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=lef[i][j], second=righ[i][j];
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}//
cout<<mini;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int lef[500][50000], righ[500][50000];
int main(){
int n, m;
cin>>n>>m;
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
for(int i=0; i<n; i++){
    int d=0;
    for(int j=m-1; j>=0; j--){
        d--;
        if(arr[i][j]=='1'){
            break;
        }
    }
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            d=j;
        lef[i][j]=d;
    }
    d=m-1;
    for(int j=0; j<m; j++){
        d++;
        if(arr[i][j]=='1')
            break;
    }
    for(int j=m-1; j>=0; j--){
        if(arr[i][j]=='1')
            d=j;
        righ[i][j]=d;
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=lef[i][j], second=righ[i][j];
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}//
cout<<mini;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int main(){
int n, m;
cin>>n>>m;
vector<set<int> > vs1;
vector<set<int, greater<int> > > vs2;
vs1.assign(n, set<int>());
vs2.assign(n, set<int, greater<int> >());
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
for(int i=0; i<n; i++){
    for(int j=0; j<3; j++){
        for(int k=0; k<m; k++){
            if(j==0 && arr[i][k]=='1'){
                vs1[i].insert(k);
                vs1[i].insert(k-m);
                vs1[i].insert(k+m);
                vs2[i].insert(k);
                vs2[i].insert(k-m);
                vs2[i].insert(k+m);
            }
        }
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=*vs1[i].lower_bound(j), second=*vs2[i].lower_bound(j);
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}
cout<<mini;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
vector<set<int> > vs1;
vector<set<int, greater<int> > > vs2;
vs1.assign(n, set<int>());
vs2.assign(n, set<int, greater<int> >());
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1'){
            vs1[i].insert(j);
            vs1[i].insert(j-m);
            vs1[i].insert(j+m);
            vs2[i].insert(j);
            vs2[i].insert(j-m);
            vs2[i].insert(j+m);
        }
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=*vs1[i].lower_bound(j), second=*vs2[i].lower_bound(j);
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}//
cout<<mini;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
string arr[1000000];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
vector<set<int> > vs1;
vector<set<int, greater<int> > > vs2;
vs1.assign(n, set<int>());
vs2.assign(n, set<int, greater<int> >());
for(int i=0; i<n; i++)
    cin>>arr[i];
for(int i=0; i<n; i++){
    bool is=false;
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1')
            is=true;
    }
    if(!is){
        cout<<-1;
        return 0;
    }
}
for(int i=0; i<n; i++){
    for(int j=0; j<m; j++){
        if(arr[i][j]=='1'){
            vs1[i].insert(j);
            vs1[i].insert(j-m);
            vs1[i].insert(j+m);
            vs2[i].insert(j);
            vs2[i].insert(j-m);
            vs2[i].insert(j+m);
        }
    }
}
int mini=2000000000;
for(int j=0; j<m; j++){
    int tot=0;
    for(int i=0; i<n; i++){
        int first=*vs1[i].lower_bound(j), second=*vs2[i].lower_bound(j);
        first=abs(first-j);
        second=abs(second-j);
        tot+=min(first, second);
    }
    if(mini>tot)
        mini=tot;
}
cout<<mini;
return 0;
}