/*
___ is a raging sadist.
*/

//Destroy all WA at test 2

#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define PI 3.14159265
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
using namespace std;

ll power(ll x, ll y)
{
   ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

ll a[100][20000];
string s[10000];
ll pre[100][10000];
ll suf[100][10000];

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

    ll n;
    cin>>n;

    ll m;
    cin>>m;

    For(i,0,n)
    {

            cin>>s[i];
    }

    For(i,0,n)
    {
        For(j,0,m)
        {
            a[i][j]=int(s[i][j]-'0');
            a[i][j+m]=a[i][j];
            pre[i][j]=INF;
            suf[i][j]=INF;
        }
    }

    //cout<<a[0][0]<<endl;

    For(i,0,n)
    {

        ll last=INF;
        for(int j=0;j<2*m;j++)
        {
            if (a[i][j]==1)
                last=j;
            pre[i][j%m]=abs(j-last);
        }

        last=INF;

        for(int j=2*m-1;j>=0;j--)
        {
            if (a[i][j]==1)
                last=j;
            suf[i][j%m]=abs(j-last);
        }
    }

    ll ans=INF;

    for(int j=0;j<m;j++)
    {
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            if (pre[i][j]>=1e8&&suf[i][j]>=1e8)
            {
                cout<<-1;
                return 0;

            } else {

                sum+=min(pre[i][j],suf[i][j]);

            }
        }
        ans=min(sum,ans);
    }

    cout<<ans;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int mxN =150000;
/*
   read question properly before start
   try some edge case

*/
int32_t main(){
  ios_base::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);

		int n,m; cin>>n>>m;
		char mat[n][m];
		int dp[n][m];
		memset(dp,0,sizeof(dp));
		bool ok=false;
		for(int i=0;i<n;i++)
		{
		   int cnt=0;
		  for(int j=0;j<m;j++)
		   {
		    cin>>mat[i][j];
		    if(mat[i][j]=='1')
		    cnt++;

		   }
		   if(cnt==0)
		   ok=true;
	    }

	    if(ok){
	    cout<<"-1"<<endl; return 0;}

	    int res=INT_MAX;

	    for(int i=0;i<n;i++)
	    {
	    	     int pre=0;
	    	     for(int j=0;j<m;j++)
	    	     {
	    	     	  if(mat[i][j]=='1')
	    	     	  pre=1;
	    	     	  else
	    	     	  dp[i][j]=pre++;
				 }

				 for(int j=0;j<n and mat[i][j]=='0';j++)
				 {
				 	 dp[i][j]=pre++;
				 }
				 pre=-1;
	    	     for(int j=m-1;j>=0;j--)
	    	     {    if(mat[i][j]=='0' and pre==-1)
	    	          continue;
	    	     	  else if(mat[i][j]=='1')
	    	     	  pre=1;
	    	     	  else
	    	     	  dp[i][j]=min(dp[i][j],pre++);
				 }

				 for(int j=m-1;j>=0 and mat[i][j]=='0';j--)
				 {
				 	 dp[i][j]=min(dp[i][j],pre++);
				 }

		}

		for(int i=0;i<m;i++)
		{
		   int cnt=0;
		   for(int j=0;j<n;j++)
		   cnt+=dp[j][i];
		   res=min(res,cnt);

		}

		cout<<res<<endl;

 return 0;}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int mxN =150000;
/*
   read question properly before start
   try some edge case

*/
int32_t main(){
  ios_base::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);

		int n,m; cin>>n>>m;
		char mat[n][m];
		int dp[n][m];
		memset(dp,0,sizeof(dp));
		bool ok=false;
		for(int i=0;i<n;i++)
		{
		   int cnt=0;
		  for(int j=0;j<m;j++)
		   {
		    cin>>mat[i][j];
		    if(mat[i][j]=='1')
		    cnt++;

		   }
		   if(cnt==0)
		   ok=true;
	    }

	    if(ok){
	    cout<<"-1"<<endl; return 0;}

	    int res=INT_MAX;

	    for(int i=0;i<n;i++)
	    {
	    	     int pre=0;
	    	     for(int j=0;j<m;j++)
	    	     {
	    	     	  if(mat[i][j]=='1')
	    	     	  pre=1;
	    	     	  else
	    	     	  dp[i][j]=pre++;
				 }

				 for(int j=0;j<m and mat[i][j]=='0';j++)
				 {
				 	 dp[i][j]=pre++;
				 }
				 pre=-1;
	    	     for(int j=m-1;j>=0;j--)
	    	     {    if(mat[i][j]=='0' and pre==-1)
	    	          continue;
	    	     	  else if(mat[i][j]=='1')
	    	     	  pre=1;
	    	     	  else
	    	     	  dp[i][j]=min(dp[i][j],pre++);
				 }

				 for(int j=m-1;j>=0 and mat[i][j]=='0';j--)
				 {
				 	 dp[i][j]=min(dp[i][j],pre++);
				 }

		}
		for(int i=0;i<m;i++)
		{
		   int cnt=0;
		   for(int j=0;j<n;j++)
		   cnt+=dp[j][i];
		   res=min(res,cnt);

		}

		cout<<res<<endl;

 return 0;}


#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=20010;
int n,m,k;
int tail,head;
int stk;
int ans,tmp;
int a;
vector<int>vc[maxn];
vector<int>::iterator it,itt;
char s[205][maxn];
int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(int i=0;i<=n;i++) vc[i].clear();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='1') vc[i].push_back(j);
            }
            if(vc[i].empty()) ans=-1;
        }
        if(ans<0) {printf("%d\n",ans);continue;}
        ans=inf;
        for(int j=0;j<m;j++)
        {
            tmp=0;
            for(int i=0;i<n;i++)
            {
                if(s[i][j]=='1') continue;
                it=lower_bound(vc[i].begin(),vc[i].end(),j);
                if(it==vc[i].begin()) tmp+=(*it)-j;
                else if(it==vc[i].end()) {it--;tmp+=j-(*it);}
                else {itt=it;itt--;tmp+=min((*it)-j,j-(*itt));}
            }
            ans=min(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=20010;
int n,m,k;
int tail,head;
int stk;
int ans,tmp;
int a;
vector<int>vc[maxn];
vector<int>::iterator it,itt;
char s[205][maxn];
int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(int i=0;i<=n;i++) vc[i].clear();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='1') {vc[i].push_back(j);vc[i].push_back(m-j);}
            }
            if(vc[i].empty()) ans=-1;
        }
        if(ans<0) {printf("%d\n",ans);continue;}
        ans=inf;
        for(int j=0;j<m;j++)
        {
            tmp=0;
            for(int i=0;i<n;i++)
            {
                if(s[i][j]=='1') continue;
                it=lower_bound(vc[i].begin(),vc[i].end(),j);
                if(it==vc[i].begin()) tmp+=(*it)-j;
                else if(it==vc[i].end()) {it--;tmp+=j-(*it);}
                else {itt=it;itt--;tmp+=min((*it)-j,j-(*itt));}
            }
            ans=min(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=20010;
int n,m,k;
int tail,head;
int stk;
int ans,tmp;
int a;
vector<int>vc[maxn],vv[maxn];
vector<int>::iterator it,itt;
char s[205][maxn];
int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(int i=0;i<=n;i++)
        {
            vc[i].clear();
            vv[i].clear();
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<m;j++)
            {
                if(s[i][j]=='1') {vc[i].push_back(j);vv[i].push_back(j);}
            }
            if(vc[i].empty()) ans=-1;
            sort(vv[i].begin(),vv[i].end());
        }
        if(ans<0) {printf("%d\n",ans);continue;}
        ans=inf;
        for(int j=0;j<m;j++)
        {
            tmp=0;
            int k=inf;
            for(int i=0;i<n;i++)
            {
                if(s[i][j]=='1') continue;
                k=inf;
                it=lower_bound(vc[i].begin(),vc[i].end(),j);
                if(it==vc[i].begin()) k=min(k,(*it)-j);
                else if(it==vc[i].end()) {it--;k=min(k,j-(*it));}
                else {k=min(k,(*it)-j);it--;k=min(k,j-(*it));}
                it=vv[i].begin();
                k=min(k,(*it)+m-j);
                it=vv[i].end();
                it--;
                k=min(k,m-(*it)+j);
                tmp+=k;
            }
            ans=min(ans,tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define pb push_back
#define si second
#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define  mod 1e18
#define double long double
#define all(o) o.begin(),o.end()
using namespace std;
string s[105];
int a[105][10005];
int32_t main()
{
	int i,j,n,m;
	cin>>n>>m;
	for(i=0;i<n;i++)
	{	cin>>s[i];
		for(j=0;j<m;j++)
			if(s[i][j]=='1')
				break;
		if(j==m)
		{	cout<<"-1\n";
			return 0;
		}
		s[i]=s[i]+s[i]+s[i];
	}

	for(i=0;i<n;i++)
	{
		int f,l;
		for(j=0;j<m;j++)
		{	a[i][j]=mod;
			if(s[i][j]=='1')
				f=j;
		}
		for(j=m;j<2*m;j++)
		{
			if(s[i][j]=='1')
				f=j;
			a[i][j-m]=j-f;
		}
		for(j=2*m;;j++)
			if(s[i][j]=='1')
			{	f=j;
				break;
			}
		for(j=2*m-1;j>=m;j--)
		{	if(s[i][j]=='1')
				f=j;
			a[i][j-m]=min(a[i][j-m],f-j);
		}/*
		for(j=0;j<m;j++)
			cout<<a[i][j]<<" ";
		cout<<endl;*/
	}
	int ans=mod;
	for(i=0;i<m;i++)
	{
		int sum=0;
		for(j=0;j<n;j++)
			sum+=a[j][i];
		ans=min(ans,sum);
	}
	cout<<ans<<endl;

}


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

#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define INF 1e9
typedef long long LL;

void solve()
{
    int n,m;
    cin>>n>>m;
    string a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int ans=1e9;
    for(int j=0;j<m;j++)
    {
        int sum=0;
        for(int i=0;i<n;i++)
        {
            int temp=1e9;
            int k=0;
            while(k<m)
            {
                int in=(j+k)%m;
                if(a[i][in]=='1')
                {
                    temp=min(temp,k);
                    break;
                }
                k++;
            }
            k=0;
            while(k<m)
            {
                int in=j-k;
                in+=m;
                in%=m;
                if(a[i][in]=='1')
                {
                    temp=min(temp,k);
                    break;
                }
                k++;
            }
            if(temp==1e9)
            {
                cout<<-1<<'\n';
                return;
            }
            sum+=temp;

        }
        ans=min(ans,sum);
    }
    cout<<ans<<'\n';
}
int main()
{
    fast;
        solve();
}


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

#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define INF 1e9
typedef long long LL;

void solve()
{
    int n,m;
    cin>>n>>m;
    string a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int dp[n][m];
    for(int i=0;i<n;i++)
    {
        int left[m];
        int right[m];
        int lc=-1;
        int rc=-1;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
            {
                lc=j;
                break;
            }
        }
        for(int j=m-1;j>=0;j--)
        {
            if(a[i][j]=='1')
            {
                rc=j;
                break;
            }
        }
        if(lc==-1)
        {
            cout<<-1<<'\n';
            return;
        }
        int curr1=m-rc;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
                curr1=0;
            left[j]=curr1;
            curr1++;
        }
        int curr2=lc+1;
        for(int j=m-1;j>=0;j--)
        {
            if(a[i][j]=='1')
                curr2=0;
            right[j]=curr2;
            curr2++;
            dp[i][j]=min(left[j],right[j]);
        }
    }
    int ans=INF;
    for(int j=0;j<m;j++)
    {
        int temp=0;
        for(int i=0;i<n;i++)
            temp+=dp[i][j];
        ans=min(ans,temp);

    }
    cout<<ans<<'\n';
}
int main()
{
    fast;
        solve();
}


//void __(){
//	_(int,n);
//	_(int,m);
//	_(vs,a,n);
//	vvi d(n,vi(m,MOD));
//	rep(i,n){
//		int last = -MOD;
//		rep(j,2*m){
//			if(a[i][j%m]=='1')
//				last = j;
//			d[i][j%m] = min(d[i][j%m],abs(last-j));
//		}
//		per(j,2*m){
//			if(a[i][j%m]=='1')
//				last = j;
//			d[i][j%m] = min(d[i][j%m],abs(last-j));
//		}
//	}
//	ll ans = MOD;
//	rep(j,m){
//		ll cur = 0;
//		rep(i,n)
//			cur += d[i][j];
//		ans = min(ans,cur);
//	}
//	print ans > 1e6 ? -1 : ans;
//}
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_ZERO_INT(i,r) for(int i = 0; i < r; ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = r-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout)
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }
    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
void __(){
	_(int,n);
	_(int,m);
	_(vs,a,n);
	vvi d(n,vi(m,MOD));
	rep(i,n){
		int last = -MOD;
		rep(j,2*m){
			if(a[i][j%m]=='1')
				last = j;
			d[i][j%m] = min(d[i][j%m],abs(last-j));
		}
		per(j,2*m){
			if(a[i][j%m]=='1')
				last = j;
			d[i][j%m] = min(d[i][j%m],abs(last-j));
		}
	}
	ll ans = MOD;
	rep(j,m){
		ll cur = 0;
		rep(i,n)
			cur += d[i][j];
		ans = min(ans,cur);
	}
	print ans > 1e6 ? -1 : ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}


//void __(){
//	_(int,n);
//	_(int,m);
//	_(vs,a,n);
//	vvi d(n,vi(m,MOD));
//	rep(i,n){
//		a[i] = a[i]+a[i]+a[i];
//		int last = -MOD*2;
//		rep(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = j-last;
//		}
//		last = MOD*2;
//		per(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = min(d[i][j-m],last-j);
//		}
//	}
//	ll ans = MOD;
//	rep(j,m){
//		ll cur = 0;
//		rep(i,n)
//			cur += d[i][j];
//		ans = min(ans,cur);
//	}
//	print ans == MOD ? -1 : ans;
//}
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_ZERO_INT(i,r) for(int i = 0; i < r; ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = r-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout)
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }
    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
void __(){
	_(int,n);
	_(int,m);
	_(vs,a,n);
	vvi d(n,vi(m,MOD));
	rep(i,n){
		a[i] = a[i]+a[i]+a[i];
		int last = -MOD*2;
		rep(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = j-last;
		}
		last = MOD*2;
		per(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = min(d[i][j-m],last-j);
		}
	}
	ll ans = MOD;
	rep(j,m){
		ll cur = 0;
		rep(i,n)
			cur += d[i][j];
		ans = min(ans,cur);
	}
	print ans == MOD ? -1 : ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}


//void __(){
//	_(int,n);
//	_(int,m);
//	_(vs,a,n);
//	vvl d(n,vl(m));
//	rep(i,n){
//		a[i] = a[i]+a[i]+a[i];
//		int last = -MOD;
//		rep(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = j-last;
//		}
//		last = MOD;
//		per(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = last-j;
//		}
//	}
//	ll ans = MOD;
//	rep(j,m){
//		ll cur = 0;
//		rep(i,n)
//			cur += d[i][j];
//		ans = min(ans,cur);
//	}
//	print ans == MOD ? -1 : ans;
//}
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_ZERO_INT(i,r) for(int i = 0; i < r; ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = r-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout)
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }
    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
void __(){
	_(int,n);
	_(int,m);
	_(vs,a,n);
	vvl d(n,vl(m));
	rep(i,n){
		a[i] = a[i]+a[i]+a[i];
		int last = -MOD;
		rep(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = j-last;
		}
		last = MOD;
		per(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = last-j;
		}
	}
	ll ans = MOD;
	rep(j,m){
		ll cur = 0;
		rep(i,n)
			cur += d[i][j];
		ans = min(ans,cur);
	}
	print ans == MOD ? -1 : ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}


//void __(){
//	_(int,n);
//	_(int,m);
//	_(vs,a,n);
//	vvl d(n,vl(m));
//	rep(i,n){
//		a[i] = a[i]+a[i]+a[i];
//		int last = -MOD*2;
//		rep(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = j-last;
//		}
//		last = MOD*2;
//		per(j,3*m){
//			if(a[i][j]=='1')
//				last = j;
//			if(m <= j && j < 2*m)
//				d[i][j-m] = last-j;
//		}
//	}
//	ll ans = MOD;
//	rep(j,m){
//		ll cur = 0;
//		rep(i,n)
//			cur += d[i][j];
//		ans = min(ans,cur);
//	}
//	print ans == MOD ? -1 : ans;
//}
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_ZERO_INT(i,r) for(int i = 0; i < r; ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = r-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout)
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }
    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
void __(){
	_(int,n);
	_(int,m);
	_(vs,a,n);
	vvl d(n,vl(m));
	rep(i,n){
		a[i] = a[i]+a[i]+a[i];
		int last = -MOD*2;
		rep(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = j-last;
		}
		last = MOD*2;
		per(j,3*m){
			if(a[i][j]=='1')
				last = j;
			if(m <= j && j < 2*m)
				d[i][j-m] = last-j;
		}
	}
	ll ans = MOD;
	rep(j,m){
		ll cur = 0;
		rep(i,n)
			cur += d[i][j];
		ans = min(ans,cur);
	}
	print ans == MOD ? -1 : ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}


//void __(){
//	_(int,n);
//	_(int,m);
//	_(vs,a,n);
//	vvi d(n,vi(m,MOD));
//	rep(i,n){
//		int last = -MOD;
//		rep(j,2*m){
//			if(a[i][j%m]=='1')
//				last = j;
//			d[i][j-m] = min(d[i][j-m],abs(last-j));
//		}
//		per(j,2*m){
//			if(a[i][j%m]=='1')
//				last = j;
//			d[i][j-m] = min(d[i][j-m],abs(last-j));
//		}
//	}
//	ll ans = MOD;
//	rep(j,m){
//		ll cur = 0;
//		rep(i,n)
//			cur += d[i][j];
//		ans = min(ans,cur);
//	}
//	print ans > 1e6 ? -1 : ans;
//}
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_ZERO_INT(i,r) for(int i = 0; i < r; ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = r-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout)
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }
    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
void __(){
	_(int,n);
	_(int,m);
	_(vs,a,n);
	vvi d(n,vi(m,MOD));
	rep(i,n){
		int last = -MOD;
		rep(j,2*m){
			if(a[i][j%m]=='1')
				last = j;
			d[i][j-m] = min(d[i][j-m],abs(last-j));
		}
		per(j,2*m){
			if(a[i][j%m]=='1')
				last = j;
			d[i][j-m] = min(d[i][j-m],abs(last-j));
		}
	}
	ll ans = MOD;
	rep(j,m){
		ll cur = 0;
		rep(i,n)
			cur += d[i][j];
		ans = min(ans,cur);
	}
	print ans > 1e6 ? -1 : ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[105][10031];
int closest[104][10031];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)cin>>a[i][j];
    for (int i=1;i<=n;i++){
        int last = 0;
        int first=-1;
        for (int j=1;j<=m;j++) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            closest[i][j]=1e9;
            if (last!=0)
            closest[i][j]=min(closest[i][j], j-last);
            if (first!=-1)
            closest[i][j]=min(closest[i][j], first+m-j);
        }
        last=0,first=-1;
        for (int j=m;j>=1;j--) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            if (last!=0)
                closest[i][j]=min(closest[i][j], last-j);
            if (first!=-1)
                closest[i][j]=min(closest[i][j], m-first+j);
        }
    }
    ll ret=1e18;
    for (int i=1;i<=m;i++){
        ll cost=0;
        for (int j=1;j<=n;j++)cost+=closest[j][i];
        ret=min(ret,cost);
    }
    if (ret>=1e9)cout<<-1<<endl;
    else cout<<ret<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[105][10031];
int closest[104][10031];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)cin>>a[i][j];
    for (int i=1;i<=n;i++){
        int last = 0;
        int first=-1;
        for (int j=1;j<=m;j++) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            closest[i][j]=1e9;
            if (last!=0)
            closest[i][j]=min(closest[i][j], j-last);
            if (first!=-1)
            closest[i][j]=min(closest[i][j], first+m-j);
        }
        last=0,first=-1;
        for (int j=m;j>=1;j--) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            closest[i][j]=1e9;
            if (last!=0)
                closest[i][j]=min(closest[i][j], last-j);
            if (first!=-1)
                closest[i][j]=min(closest[i][j], m-first+j);
        }
    }
    ll ret=1e18;
    for (int i=1;i<=m;i++){
        ll cost=0;
        for (int j=1;j<=n;j++)cost+=closest[j][i];
        ret=min(ret,cost);
    }
    if (ret>=1e9)cout<<-1<<endl;
    else cout<<ret<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[105][10031];
int closest[104][10031];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)cin>>a[i][j];
    for (int i=1;i<=n;i++){
        int last = 0;
        int first=-1;
        for (int j=1;j<=m;j++) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            closest[i][j]=1e9;
            if (last!=0)
            closest[i][j]=min(closest[i][j], j-last);
            if (first!=-1)
            closest[i][j]=min(closest[i][j], first+m-j);
        }
        last=0,first=-1;
        for (int j=m;j>=1;j--) {
            if (a[i][j]=='1') last = j;
            if (a[i][j]=='1' && first==-1)first = j;
            closest[i][j]=1e9;
            if (last!=0)
                closest[i][j]=min(closest[i][j], last-j);
            if (first!=-1)
                closest[i][j]=min(closest[i][j], m-first+j);
        }
    }
    ll ret=1e18;
    for (int i=1;i<=m;i++){
        ll cost=0;
        for (int j=1;j<=n;j++)cost+=closest[j][i];
        ret=min(ret,cost);
    }
    if (ret>=1e10)cout<<-1<<endl;
    else cout<<ret<<endl;
}


#include <bits/stdc++.h>

using namespace std;

int a[110][10010],n,m;
vector <int> ke[10010];

int main()
{
    cin >> n >> m;
    char c;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            cin >> c;
            if (c=='1')
            {
                a[i][j]=1;
                ke[i].push_back(j);
            }
            else
                a[i][j]=0;
        }
        if (ke[i].size()==0)
        {
            cout << -1;
            return 0;
        }
    }
    long res=1e9+7,tmp;
    for (int j=1;j<=m;j++)
    {
        tmp=0;
        for (int i=1;i<=n;i++)
            if (a[i][j]==0)
            {
                if (ke[i][0]>j)
                {
                    tmp+=ke[i][0]-j;
                    continue;
                }
                if (ke[i][ke[i].size()-1]<j)
                {
                    tmp+=j-ke[i][ke[i].size()-1];
                    continue;
                }
                long d=0,c=ke[i].size()-1,g,vt;
                while (d<=c)
                {
                    g=(d+c)/2;
                    if (ke[i][g]<j)
                    {
                        vt=g;
                        d=g+1;
                    }
                    else
                        c=g-1;
                }
                tmp+=min(abs(ke[i][vt]-j),abs(ke[i][vt+1]-j));
            }
        res=min(res,tmp);
    }
    cout << res;
}


#include <bits/stdc++.h>

using namespace std;

int a[110][10010],n,m;
vector <int> ke[10010];

int main()
{
    cin >> n >> m;
    char c;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            cin >> c;
            if (c=='1')
            {
                a[i][j]=1;
                ke[i].push_back(j);
            }
            else
                a[i][j]=0;
        }
        if (ke[i].size()==0)
        {
            cout << -1;
            return 0;
        }
    }
    long res=1e9+7,tmp;
    for (int j=1;j<=m;j++)
    {
        tmp=0;
        for (int i=1;i<=n;i++)
            if (a[i][j]==0)
            {
                if (ke[i][0]>j)
                {
                    tmp+=min(ke[i][0]-j,abs(ke[i][ke[i].size()-1]-(j+m)));
                    continue;
                }
                if (ke[i][ke[i].size()-1]<j)
                {
                    tmp+=min(j-ke[i][ke[i].size()-1],abs(ke[i][0]+m-j));
                    continue;
                }
                long d=0,c=ke[i].size()-1,g,vt;
                while (d<=c)
                {
                    g=(d+c)/2;
                    if (ke[i][g]<j)
                    {
                        vt=g;
                        d=g+1;
                    }
                    else
                        c=g-1;
                }
                tmp+=min(abs(ke[i][vt]-j),min(abs(ke[i][vt+1]-j),min(abs(ke[i][vt]-(j+m)),abs(ke[i][vt+1]-(j+m)))));
            }
        res=min(res,tmp);
    }
    cout << res;
}


#include <bits/stdc++.h>

using namespace std;

int a[110][10010],n,m;
vector <int> ke[10010];

int main()
{
    cin >> n >> m;
    char c;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            cin >> c;
            if (c=='1')
            {
                a[i][j]=1;
                ke[i].push_back(j);
            }
            else
                a[i][j]=0;
        }
        if (ke[i].size()==0)
        {
            cout << -1;
            return 0;
        }
    }
    long res=1e9+7,tmp;
    for (int j=1;j<=m;j++)
    {
        tmp=0;
        for (int i=1;i<=n;i++)
            if (a[i][j]==0)
            {
                if (ke[i][0]>j)
                {
                    tmp+=min(ke[i][0]-j,abs(ke[i][ke[i].size()-1]-(j+n)));
                    continue;
                }
                if (ke[i][ke[i].size()-1]<j)
                {
                    tmp+=min(j-ke[i][ke[i].size()-1],abs(ke[i][0]+n-j));
                    continue;
                }
                long d=0,c=ke[i].size()-1,g,vt;
                while (d<=c)
                {
                    g=(d+c)/2;
                    if (ke[i][g]<j)
                    {
                        vt=g;
                        d=g+1;
                    }
                    else
                        c=g-1;
                }
                tmp+=min(abs(ke[i][vt]-j),min(abs(ke[i][vt+1]-j),min(abs(ke[i][vt]-(j+n)),abs(ke[i][vt+1]-(j+n)))));
            }
        res=min(res,tmp);
    }
    cout << res;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>

#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define N 105
#define M 10005
#define K 17

using namespace std;

int i,j,m,n,p,k,dis[N][M],w[M*3],l[M*3];

char c[N][M];

int main()
{
		scanf("%d%d",&n,&m);
		for (i=0;i<n;++i)
		{
				scanf("%s",c[i]);
				for (j=0;j<m;++j) if(c[i][j]=='1') break;
				if (j==m)
				{
						puts("-1");
						return 0;
				}
		}
		for (i=0;i<n;++i)
		{
				for (j=0;j<m;++j) w[j]=w[j+m]=w[j+2*m]=c[i][j]-'0';
				int len=3*m;
				for (j=0;j<len;++j)
				{
						if (w[j]||!j) l[j]=j;
						else l[j]=l[j-1];
				}
				for (j=0;j<m;++j) dis[i][j]=j+m-l[j+m];
				for (j=len-1;j>=0;--j)
				{
						if (w[j]) l[j]=j; else l[j]=l[j+1];
				}
				for (j=0;j<m;++j) dis[i][j]=min(dis[i][j],l[j+m]-(j+m));
		}
		int ans=(int)1e9;
		for (i=0;i<m;++i)
		{
				int s=0;
				for (j=0;j<n;++j) s+=dis[j][i];
				ans=min(ans,s);
		}
		printf("%d\n",ans);
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        //seen from editorial
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int brr[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(arr[i][j]=='0') brr[i][j] = 1e9;
                else brr[i][j] = 0;
            }
        }
        for(int i=0;i<n;i++)
        {
            ///left to right
            int j=0,last=-1;
            for(    ;j<m;j++)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=0;j<m;j++)
            {
                if(arr[i][j]=='0' && brr[i][j]==1e9)
                {
                    brr[i][j]=min(brr[i][j],m-(abs(last-j)));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }

            j=m-1,last=-1;
            for(    ;j>=0;j--)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=m-1;j>=0;j--)
            {
                if(arr[i][j]=='0' && brr[i][j]==1e9)
                {
                    brr[i][j]=min(brr[i][j],m-(abs(last-j)));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
        }
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<m;j++) cout<<brr[i][j]<<" ";
        //     cout<<endl;
        // }
        int mn = 1e9;
        bool g = 1;
        for(int i=0;i<n;i++)
        {
            int cnt=0;
            for(int j=0;j<m;j++)
            {
                cnt+=(arr[i][j]=='0');
            }
            if(cnt==m)
            {
                g =0;
                break;
            }
        }
        for(int j=0;j<m;j++)
        {
            int sum=0,cntz=0;
            for(int i=0;i<n;i++)
            {
                sum+=brr[i][j];
            }
            //cout<<"sum:"<<sum<<endl;
            mn = min(mn,sum);
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        //seen from editorial
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int brr[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(arr[i][j]=='0') brr[i][j] = 1e9;
                else brr[i][j] = 0;
            }
        }
        for(int i=0;i<n;i++)
        {
            ///left to right
            int j=0,last=-1;
            for(    ;j<m;j++)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=0;j<m;j++)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]=min(brr[i][j],m-(abs(last-j)));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }

            j=m-1,last=-1;
            for(    ;j>=0;j--)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=m-1;j>=0;j--)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]=min(brr[i][j],m-(abs(last-j)));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
        }
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<m;j++) cout<<brr[i][j]<<" ";
        //     cout<<endl;
        // }
        int mn = 1e9;
        bool g = 1;
        for(int i=0;i<n;i++)
        {
            int cnt=0;
            for(int j=0;j<m;j++)
            {
                cnt+=(arr[i][j]=='0');
            }
            if(cnt==m)
            {
                g =0;
                break;
            }
        }
        for(int j=0;j<m;j++)
        {
            int sum=0,cntz=0;
            for(int i=0;i<n;i++)
            {
                sum+=brr[i][j];
            }
            //cout<<"sum:"<<sum<<endl;
            mn = min(mn,sum);
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        //seen from editorial
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int brr[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++) brr[i][j] = 0;
        }
        for(int i=0;i<n;i++)
        {
            ///left to right
            int j=0,last=-1;
            for(    ;j<m;j++)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]+=j-last;
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=0;j<m;j++)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]+=m-(abs(last-j));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }

            j=m-1,last=-1;
            for(    ;j>=0;j--)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]+=j-last;
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=m-1;j>=0;j--)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]+=m-(abs(last-j));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
        }
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<m;j++) cout<<brr[i][j]<<" ";
        //     cout<<endl;
        // }
        int mn = 1e9;
        bool g = 1;
        for(int i=0;i<n;i++)
        {
            int cnt=0;
            for(int j=0;j<m;j++)
            {
                cnt+=(arr[i][j]=='1');
            }
            if(cnt==m)
            {
                g =0;
                break;
            }
        }
        for(int j=0;j<m;j++)
        {
            int sum=0,cntz=0;
            for(int i=0;i<n;i++)
            {
                sum+=brr[i][j];
            }
            //cout<<"sum:"<<sum<<endl;
            mn = min(mn,sum);
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int foo1(vector<int> &v,int key,int m)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }
    return ans;
}
int foo11(vector<int> &v,int key,int m)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(m-abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }
    return ans;
}

int foo2(vector<int> &v,int key,int m)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            low = mid+1;
        }
        else
        {
            high = mid-1;
        }
    }
    return ans;
}
int foo22(vector<int> &v,int key,int m)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(m-abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            low = mid+1;
        }
        else
        {
            high = mid-1;
        }
    }
    return ans;
}

vector<int> v[2*1000+1];
int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        bool g = 1;

        for(int i=0;i<n;i++)
        {
            //cout<<"i:"<<i<<endl;
            for(int j=0;j<arr[i].size();j++)
            {
                if(arr[i][j]=='1')
                {
                    //cout<<"j:"<<j<<endl;
                    v[i].pb(j);
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                }
            }
            if(v[i].size()==0)
            {
                g = 0;
                break;
            }
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        // for(int i=0;i<n;i++)
        // {
        //     cout<<arr[i]<<endl;
        //     cout<<"i:"<<i<<endl;
        //     PrintArray(v[i]);
        // }
        vector<int> :: iterator it1,it2;
        int mn = 1e9;
        for(int j=0;j<m;j++)
        {
            int cnt = 0;
            for(int i=0;i<n;i++)
            {
                if(arr[i][j]=='0')
                {
                    //cout<<"i:"<<i<<endl;
                    // cout<<"printing:";
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                    // it1 = std::upper_bound(v[i].begin(),v[i].end(),j);
                    // it2 = std::lower_bound(v[i].begin(),v[i].end(),j);
                    // int indx1 = it2-v[i].begin();
                    // int indx2 = it2-v[i].begin();
                    int indx1 = foo1(v[i],j,m);
                    int indx11 = foo11(v[i],j,m);

                    int indx2 = foo2(v[i],j,m);
                    int indx22 = foo22(v[i],j,m);

                    int dif1 = min(abs(j-v[i][indx1]),m-abs(j-v[i][indx1]));
                    int dif11 = min(abs(j-v[i][indx11]),m-abs(j-v[i][indx11]));

                    int dif2 = min(abs(v[i][indx2]-j),m-abs(v[i][indx2]-j));
                    int dif22 = min(abs(v[i][indx22]-j),m-abs(v[i][indx22]-j));
                    //cout<<indx1<<" "<<indx2<<endl;
                    // cout<<"dif1:"<<dif1<<endl;
                    // cout<<"dif2:"<<dif2<<endl;
                    int temp1=0,temp2=0;
                    if(indx1>=0 && indx1<v[i].size() && indx2>=0 && indx2<v[i].size())
                    {
                        //cout<<v[i][indx1]<<" "<<v[i][indx2]<<endl;

                        if(dif1<dif2)
                        {
                            temp1+=dif1;
                        }
                        else
                        {
                            temp1+=dif2;
                        }
                    }
                    else if(indx1>=0 && indx1<v[i].size() && !(indx2>=0 && indx2<v[i].size()))
                    {
                        temp1+=dif1;
                    }
                    else if(!(indx1>=0 && indx1<v[i].size()) && (indx2>=0 && indx2<v[i].size()))
                    {
                        temp1+=dif2;
                    }

                    ///
                    if(indx11>=0 && indx11<v[i].size() && indx22>=0 && indx22<v[i].size())
                    {
                        //cout<<v[i][indx1]<<" "<<v[i][indx2]<<endl;

                        if(dif11<dif22)
                        {
                            temp2+=dif11;
                        }
                        else
                        {
                            temp2+=dif22;
                        }
                    }
                    else if(indx11>=0 && indx11<v[i].size() && !(indx22>=0 && indx22<v[i].size()))
                    {
                        temp2+=dif11;
                    }
                    else if(!(indx11>=0 && indx11<v[i].size()) && (indx22>=0 && indx22<v[i].size()))
                    {
                        temp2+=dif22;
                    }
                    cnt+=min(temp1,temp2);
                }
            }
            //cout<<"cnt:"<<cnt<<endl;
            mn = min(mn,cnt);
            //cout<<"\n";
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int foo1(vector<int> &v,int key)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }
    return ans;
}

int foo2(vector<int> &v,int key)
{
    int low=0,high = v.size()-1,mid,ans=-1,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            low = mid+1;
        }
        else
        {
            high = mid-1;
        }
    }
    return ans;
}

vector<int> v[2*1000+1];
int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        bool g = 1;

        for(int i=0;i<n;i++)
        {
            //cout<<"i:"<<i<<endl;
            for(int j=0;j<arr[i].size();j++)
            {
                if(arr[i][j]=='1')
                {
                    //cout<<"j:"<<j<<endl;
                    v[i].pb(j);
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                }
            }
            if(v[i].size()==0)
            {
                g = 0;
                break;
            }
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        // for(int i=0;i<n;i++)
        // {
        //     cout<<arr[i]<<endl;
        //     cout<<"i:"<<i<<endl;
        //     PrintArray(v[i]);
        // }
        vector<int> :: iterator it1,it2;
        int mn = 1e9;
        for(int j=0;j<m;j++)
        {
            int cnt = 0;
            for(int i=0;i<n;i++)
            {
                if(arr[i][j]=='0')
                {
                    //cout<<"i:"<<i<<endl;
                    // cout<<"printing:";
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                    // it1 = std::upper_bound(v[i].begin(),v[i].end(),j);
                    // it2 = std::lower_bound(v[i].begin(),v[i].end(),j);
                    // int indx1 = it2-v[i].begin();
                    // int indx2 = it2-v[i].begin();
                    int indx1 = foo1(v[i],j);
                    int indx2 = foo2(v[i],j);
                    int dif1 = min(abs(j-v[i][indx1]),m-abs(j-v[i][indx1]));
                    int dif2 = min(abs(v[i][indx2]-j),m-abs(v[i][indx2]-j));
                    //cout<<indx1<<" "<<indx2<<endl;
                    // cout<<"dif1:"<<dif1<<endl;
                    // cout<<"dif2:"<<dif2<<endl;
                    if(indx1>=0 && indx1<v[i].size() && indx2>=0 && indx2<v[i].size())
                    {
                        //cout<<v[i][indx1]<<" "<<v[i][indx2]<<endl;

                        if(dif1<dif2)
                        {
                            cnt+=dif1;
                        }
                        else
                        {
                            cnt+=dif2;
                        }
                    }
                    else if(indx1>=0 && indx1<v[i].size() && !(indx2>=0 && indx2<v[i].size()))
                    {
                        cnt+=dif1;
                    }
                    else if(!(indx1>=0 && indx1<v[i].size()) && (indx2>=0 && indx2<v[i].size()))
                    {
                        cnt+=dif2;
                    }
                }
            }
            //cout<<"cnt:"<<cnt<<endl;
            mn = min(mn,cnt);
            //cout<<"\n";
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        //seen from editorial
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int brr[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++) brr[i][j] = 0;
        }
        for(int i=0;i<n;i++)
        {
            ///left to right
            int j=0,last=-1;
            for(    ;j<m;j++)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]+=j-last;
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=0;j<m;j++)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]+=m-(abs(last-j));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }

            j=m-1,last=-1;
            for(    ;j>=0;j--)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]+=j-last;
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=m-1;j>=0;j--)
            {
                if(arr[i][j]=='0' && brr[i][j]==0)
                {
                    brr[i][j]+=m-(abs(last-j));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
        }
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<m;j++) cout<<brr[i][j]<<" ";
        //     cout<<endl;
        // }
        int mn = 1e9;
        bool g = 1;
        for(int j=0;j<m;j++)
        {
            int sum=0;
            for(int i=0;i<n;i++)
            {
                sum+=brr[i][j];
            }
            //cout<<"sum:"<<sum<<endl;
            if(sum==0)
            {
                g = 0;
                break;
            }
            mn = min(mn,sum);
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int foo1(vector<int> &v,int key)
{
    int low=0,high = v.size()-1,mid,ans,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            high = mid-1;
        }
        else
        {
            low = mid+1;
        }
    }
    return ans;
}

int foo2(vector<int> &v,int key)
{
    int low=0,high = v.size()-1,mid,ans,mn = 1e9;
    while(low<=high)
    {
        mid = low+(high-low)/2;
        if(abs(v[mid]-key)<mn)
        {
            ans = mid;
            mn = abs(v[mid]-key);
            low = mid+1;
        }
        else
        {
            high = mid-1;
        }
    }
    return ans;
}

vector<int> v[2*1000+1];
int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        bool g = 1;

        for(int i=0;i<n;i++)
        {
            //cout<<"i:"<<i<<endl;
            for(int j=0;j<arr[i].size();j++)
            {
                if(arr[i][j]=='1')
                {
                    //cout<<"j:"<<j<<endl;
                    v[i].pb(j);
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                }
            }
            if(v[i].size()==0)
            {
                g = 0;
                break;
            }
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        // for(int i=0;i<n;i++)
        // {
        //     cout<<arr[i]<<endl;
        //     cout<<"i:"<<i<<endl;
        //     PrintArray(v[i]);
        // }
        vector<int> :: iterator it1,it2;
        int mn = 1e9;
        for(int j=0;j<m;j++)
        {
            int cnt = 0;
            for(int i=0;i<n;i++)
            {
                if(arr[i][j]=='0')
                {
                    //cout<<"i:"<<i<<endl;
                    // cout<<"printing:";
                    // for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
                    // cout<<endl;
                    // it1 = std::upper_bound(v[i].begin(),v[i].end(),j);
                    // it2 = std::lower_bound(v[i].begin(),v[i].end(),j);
                    // int indx1 = it2-v[i].begin();
                    // int indx2 = it2-v[i].begin();
                    int indx1 = foo1(v[i],j);
                    int indx2 = foo2(v[i],j);
                    int dif1 = min(abs(j-v[i][indx1]),m-abs(j-v[i][indx1]));
                    int dif2 = min(abs(v[i][indx2]-j),m-abs(v[i][indx2]-j));
                    //cout<<indx1<<" "<<indx2<<endl;
                    // cout<<"dif1:"<<dif1<<endl;
                    // cout<<"dif2:"<<dif2<<endl;
                    if(indx1>=0 && indx1<v[i].size() && indx2>=0 && indx2<v[i].size())
                    {
                        //cout<<v[i][indx1]<<" "<<v[i][indx2]<<endl;

                        if(dif1<dif2)
                        {
                            cnt+=dif1;
                        }
                        else
                        {
                            cnt+=dif2;
                        }
                    }
                    else if(indx1>=0 && indx1<v[i].size() && !(indx2>=0 && indx2<v[i].size()))
                    {
                        cnt+=dif1;
                    }
                    else if(!(indx1>=0 && indx1<v[i].size()) && (indx2>=0 && indx2<v[i].size()))
                    {
                        cnt+=dif2;
                    }
                }
            }
            //cout<<"cnt:"<<cnt<<endl;
            mn = min(mn,cnt);
            //cout<<"\n";
        }
        cout<<mn<<endl;
    }
    return 0;
}


#include <iostream>
#include <limits>
#include <cstdint>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <deque>
#include <stack>
#include <queue>
#include <utility>
using namespace std;
typedef long long int l1;
typedef long long unsigned int lu1;
typedef long double ld;
#define FIO std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// #define __gcd(a,b) __algo_gcd(a,b)
#define PI acos(-1.0)
#define pb push_back
#define INF 0x3f3f3f3f
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define PrintArray(arr) for(int i=0;i<(int)arr.size();i++)cout<<arr[i]<<" ";cout<<endl;
#define pii pair<int,int>
#define pll pair<long long int , long long int>
const int MOD = 1e9+7;

// vector<bool> prime(1e6+5,true);
// void SieveOfEratosthenes(l1 n)
// {
//     prime[0]=false;
//     prime[1]=false;
//     for (lu1 p=2; p*p<=n; p++)
//     {
//         if (prime[p] == true)
//         {
//             for (lu1 i=p*p; i<=n; i+= p)
//                 prime[i] = false;
//         }
//     }
// }

// // vector<int> v;
// lu1 countDivisors(lu1 n)
// {
//     lu1 cnt=0;
//     for (lu1 i=1; i<=sqrt(n); i++)
//     {
//         if (n%i == 0)
//         {
//             if (n/i == i)
//             {
//                 ++cnt;
//                 v.pb(i);
//             }

//             else
//             {
//                 cnt+=2;
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     return cnt;
// }

//vector<int> v;
int Search(string pat,string txt)
{
    int n = txt.size();
    int m = pat.size();
    for(int i=0;i<=n-m;i++)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(txt[i+j]!=pat[j])
            {
                break;
            }
        }
        if(j==m)
        {
            return i;
        }
    }
    return -1;
}

int findFirst(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size()-1;
  int ans=-1;
  while (low <= high)
  {
    int mid = low + (high-low)/2;
    if (key == array[mid])
    {
        ans = mid;
        high = mid-1;
    }
    else if (key > array[mid])
        low = mid+1;
    else
        high = mid-1;
  }
  return ans;
}
int findLast(const std::vector<l1> &array, int key,int low,int high)
{
  //int low = 0;
  //int high = array.size() - 1;
  int ans = -1;
  while (low <= high)
  {
    int mid = low + (high - low) / 2;
    if (key == array[mid])
    {
      ans = mid;
      low = mid + 1;
    }
    else if (key > array[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ans;
}

int d,x,y;
void extendedEuclid(int a,int b)
{
    if(b==0)
    {
        d=1;
        x=1;
        y=0;
    }
    else
    {
        extendedEuclid(b,a%b);
        int temp = x;
        x = y;
        y = temp - (a/b)*y;
    }

}

bool isPowerOfTwo(l1 x)
{
    return (x && !(x & (x - 1)));
}
// const int N = 2*(2e5+5);
// vector<int> adj[N];
// // vector<int> level(N,0);
// vector<bool> vis(N,0);
/*void BFS(int s)
{
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    level[s] = 0;
    while(!q.empty())
    {
        int v = q.front();q.pop();
        for(int j=0;j<adj[v].size();j++)
        {
            int w = adj[v][j];
            if(!vis[w])
            {
                q.push(w);
                vis[w] = 1;
                //if all edges in graph have same weight then this will give min. distance b/w two edges.
                level[w] = 1+level[v];
            }
        }
    }
}*/

/*void DFS_recursive(int s)
{
    vis[s] = 1;
    for(int j=0;j<adj[s].size();j++)
    {
        int w = adj[s][j];
        if(vis[w]==0)
        {
            DFS_recursive(w);
        }
    }
}*/

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        //seen from editorial
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int brr[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(arr[i][j]=='0') brr[i][j] = 1e9;
                else brr[i][j] = 0;
            }
        }
        for(int i=0;i<n;i++)
        {
            ///left to right
            int j=0,last=-1;
            for(    ;j<m;j++)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=0;j<m;j++)
            {
                if(arr[i][j]=='0' && brr[i][j]==1e9)
                {
                    brr[i][j]=min(brr[i][j],abs(m-(abs(last-j))));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }

            j=m-1,last=-1;
            for(    ;j>=0;j--)
            {
                if(arr[i][j]=='0' && last!=-1)
                {
                    brr[i][j]=min(brr[i][j],abs(j-last));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
            for(j=m-1;j>=0;j--)
            {
                if(arr[i][j]=='0' && brr[i][j]==1e9)
                {
                    brr[i][j]=min(brr[i][j],abs(m-(abs(last-j))));
                }
                else if(arr[i][j] == '1')
                {
                    last = j;
                }
            }
        }
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<m;j++) cout<<brr[i][j]<<" ";
        //     cout<<endl;
        // }
        int mn = 1e9;
        bool g = 1;
        for(int i=0;i<n;i++)
        {
            int cnt=0;
            for(int j=0;j<m;j++)
            {
                cnt+=(arr[i][j]=='0');
            }
            if(cnt==m)
            {
                g =0;
                break;
            }
        }
        for(int j=0;j<m;j++)
        {
            int sum=0;
            for(int i=0;i<n;i++)
            {
                sum+=brr[i][j];
            }
            //cout<<"sum:"<<sum<<endl;
            mn = min(mn,sum);
        }
        if(!g)
        {
            cout<<-1<<endl;
            continue;
        }
        cout<<mn<<endl;
    }
    return 0;
}