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

string s[107] = {""};
int sR[107][10007], sL[107][10007], minS[107][10007];
int main() {
	int row, column;
	cin >> row >> column;
	for (int i = 0; i < row; ++i)
	{
		cin >> s[i];
	}
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	// traverse right
	for (int i = 0; i < row; ++i)
	{
		if (s[i][0] == '1')
		{
			sR[i][0] = 0;
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
					sR[i][j] = 0;
				else
					sR[i][j] = sR[i][j-1]+1;
			}
		}
		// get the right most 1
		else
		{
			for (int j = column - 1; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					sR[i][0] = column - j; // number of shifts to move to s[i][0]
					break;
				}

			}
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
					sR[i][j] = 0;
				else
					sR[i][j] = sR[i][j-1] + 1;
			}
		}
	}
	//traverse left
	for (int i = 0; i < row; ++i)
	{
		if (s[i][column-1] == '1')
		{
			sL[i][column - 1] = 0;
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
		else // get left most 1
		{
			for (int j = 0; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sL[i][column-1] = j+1; // amount of shifts to s[i][column-1]
					break;
				}
			}
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
	}
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			minS[i][j] = min(sR[i][j], sL[i][j]);
//			cout << minS[i][j] << " ";
		}
//		cout << endl;
	}
	int out = INT_MAX;
	for (int i = 0; i < column; ++i)
	{
		int sum = 0;
		for (int j = 0; j < row; ++j)
		{
			sum += minS[j][i];
		}
		out = min(sum,out);
	}
	cout << out << endl;
	return 0;
}


//============================================================================
// Name        : TEST2.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; wanted < s[i].size(); ++j)
			{
//				cout << "Wanted = " << j << endl;
				if (s[i][j] == '1')
				{
					int temp = abs(column - abs(wanted - j));
					x = min(abs(wanted-j), temp);
//					cout << "x = " << x << endl;
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{

					int temp = abs(column - abs(wanted - j));
					y = min(abs(wanted-j), temp);
//					cout << "y = " << y << endl;
					break;
				}
			}

			int moves = min(x, y);
			sum += moves;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


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

string s[100] = {""};
int sR[100][10000], sL[100][10000], minS[100][10000];
int main() {
	int row, column;
	cin >> row >> column;
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
				possible = true;
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	// traverse right
	for (int i = 0; i < row; ++i)
	{
		if (s[i][0] == '1')
		{
			sR[i][0] = 0;
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sR[i][j] = 0;
					continue;
				}
				sR[i][j] = sR[i][j-1]+1;
			}
		}
		else
		{
			for (int j = column - 1; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					sR[i][0] = column - j;
					break;
				}

			}
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sR[i][j] = 0;
					continue;
				}
				sR[i][j] = sR[i][j-1] + 1;
			}
		}
	}
	//traverse left
	for (int i = 0; i < row; ++i)
	{
		if (s[i][column-1] == '1')
		{
			sL[i][column - 1] = 0;
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
		else
		{
			for (int j = 0; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sL[i][column-1] = column - j;
					break;
				}
			}
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
	}
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			minS[i][j] = min(sR[i][j], sL[i][j]);
//			cout << minS[i][j] << " ";
		}
//		cout << endl;
	}
	int out = INT_MAX;
	for (int i = 0; i < column; ++i)
	{
		int sum = 0;
		for (int j = 0; j < row; ++j)
		{
			sum += minS[j][i];
		}
		out = min(sum,out);
	}
	cout << out << endl;
	return 0;
}


//============================================================================
// Name        : TEST2.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; wanted < s[i].size(); ++j)
			{
				if (s[i][j] == '1')
				{
					x = abs(wanted -  j);
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					y = abs(wanted - j);
					break;
				}
			}

			int moves = min(x, y);
			sum += moves;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; j < s[i].size(); ++j)
			{
//				cout << "Wanted = " << j << endl;
				if (s[i][j] == '1')
				{
					int temp = abs(column - abs(wanted - j));
					x = min(abs(wanted-j), temp);
//					cout << "x = " << x << endl;
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{

					int temp = abs(column - abs(wanted - j));
					y = min(abs(wanted-j), temp);
//					cout << "y = " << y << endl;
					break;
				}
			}

			int moves = min(x, y);
			sum += moves;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


//============================================================================
// Name        : TEST2.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; wanted < s[i].size(); ++j)
			{
//				cout << "Wanted = " << j << endl;
				if (s[i][j] == '1')
				{
					int temp = abs((int)s[i].size() - abs(wanted - j));
					x = min(abs(wanted-j), temp);
//					cout << "x = " << x << endl;
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{

					int temp = abs((int)s[i].size() - abs(wanted - j));
					y = min(abs(wanted-j), temp);
//					cout << "y = " << y << endl;
					break;
				}
			}

			int moves = min(x, y);
			sum += moves;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int nearestOne = INT_MAX;
			for (int j = 0; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					if (abs(j-wanted) < abs(nearestOne - wanted))
					{
						nearestOne = j;
					}
				}

			}

			int moves = min(abs(wanted-nearestOne), column - abs(wanted - nearestOne));
			sum += moves;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


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

string s[107] = {""};
int sR[107][10007], sL[107][10007], minS[107][10007];
int main() {
	int row, column;
	cin >> row >> column;
	for (int i = 0; i < row; ++i)
	{
		cin >> s[i];
	}
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	// traverse right
	for (int i = 0; i < row; ++i)
	{
		if (s[i][0] == '1')
		{
			sR[i][0] = 0;
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sR[i][j] = 0;
					continue;
				}
				sR[i][j] = sR[i][j-1]+1;
			}
		}
		else
		{
			for (int j = column - 1; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					sR[i][0] = column - j;
					break;
				}

			}
			for (int j = 1; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sR[i][j] = 0;
					continue;
				}
				sR[i][j] = sR[i][j-1] + 1;
			}
		}
	}
	//traverse left
	for (int i = 0; i < row; ++i)
	{
		if (s[i][column-1] == '1')
		{
			sL[i][column - 1] = 0;
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
		else
		{
			for (int j = 0; j < column; ++j)
			{
				if (s[i][j] == '1')
				{
					sL[i][column-1] = column - j;
					break;
				}
			}
			for (int j = column - 2; j >= 0; --j)
			{
				if (s[i][j] == '1')
					sL[i][j] = 0;
				else
					sL[i][j] = sL[i][j+1] + 1;
			}
		}
	}
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < column; ++j)
		{
			minS[i][j] = min(sR[i][j], sL[i][j]);
//			cout << minS[i][j] << " ";
		}
//		cout << endl;
	}
	int out = INT_MAX;
	for (int i = 0; i < column; ++i)
	{
		int sum = 0;
		for (int j = 0; j < row; ++j)
		{
			sum += minS[j][i];
		}
		out = min(sum,out);
	}
	cout << out << endl;
	return 0;
}


//============================================================================
// Name        : TEST2.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; wanted < s[i].size(); ++j)
			{
				if (s[i][j] == '1')
				{
					x = j;
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					y = j;
					break;
				}
			}

			int cur = min(x, y);
			int temp = abs(wanted - cur);
			sum += temp;
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


//============================================================================
// Name        : TEST2.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main()
{
	int row, column;
	cin >> row >> column;
	string s[10000] = {""};
	for (int i = 0; i < row; ++i)
	{
		bool possible = false;
		cin >> s[i];
		for (int j = 0; j < column; ++j)
		{
			if (s[i][j] == '1')
			{
				possible = true;
			}
		}
		if (!possible)
		{
			cout << -1 << endl;
			return 0;
		}
	}
	int out = INT_MAX;
	for (int wanted = 0; wanted < column; ++wanted)
	{
		int sum = 0;
		for (int i = 0; i < row; ++i)
		{
			int x = INT_MAX, y = INT_MAX;
			for (int j = wanted; wanted < s[i].size(); ++j)
			{
				if (s[i][j] == '1')
				{
					x = j;
					break;
				}
			}
			for (int j = wanted; j >= 0; --j)
			{
				if (s[i][j] == '1')
				{
					y = j;
					break;
				}
			}

			int cur = min(x, y);
			int temp = abs(wanted - cur);
			sum += min(temp, column - temp);
		}
		out = min(out, sum);
	}
	cout << out << endl;
	return 0;
}


/*author @dhanush*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                         \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define se second
#define fi first
void peace()
{

 ll n,m;
 cin>>n>>m;
 string arr[n];
 for(int i=0;i<n;i++)
 {
	 cin>>arr[i];
 }
 vector<ll>rows[n];
 for(int i=0;i<n;i++)
 {
	 ll cnt=0;
	 for(int j=0;j<m;j++)
	 {
		 if(arr[i][j]=='1')
		 {
			 cnt++;
			 rows[i].pb(j);
		 }
	 }
	 if(cnt==0)
	 {
		 cout<<"-1";
		 return;
	 }
 }
 ll dist[n][m];
 for(int i=0;i<n;i++)
 {
	 for(int j=0;j<m;j++)
	 {
		 dist[i][j]=0;
		 if(arr[i][j]=='1')continue;
		 else
		 {
			  ll a,b,c;
			 ll xx=upper_bound(rows[i].begin(),rows[i].end(),j)-rows[i].begin();
			 if(xx==rows[i].size())
			 {
			  a=INT_MAX;
			 }
			 else
			 {
				 a=rows[i][xx]-j;
			 }
			 if(xx>0)
			 {
                c=j-rows[i][xx-1];
			 }
			 else
			 {
				  ll yy=rows[i][rows[i].size()-1];
				 c=m-yy+j;
			 }

             dist[i][j]=min(a,c);
		 }

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

//  }
 ll ans=INT_MAX;
 for(int j=0;j<m;j++)
 {
	 ll xx=0;
	 for(int i=0;i<n;i++)
	 {
		 xx+=dist[i][j];
	 }
	 ans=min(ans,xx);
 }
 cout<<ans<<endl;

}
int main()
{
	boost;
	ll t;
	t = 1;
	//----------
	// cin >> t;
	//----------
	while (t--)
		peace();
}


/*author @dhanush*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost                         \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define se second
#define fi first
void peace()
{

 ll n,m;
 cin>>n>>m;
 string arr[n];
 for(int i=0;i<n;i++)
 {
	 cin>>arr[i];
 }
 vector<ll>rows[n];
 for(int i=0;i<n;i++)
 {
	 ll cnt=0;
	 for(int j=0;j<m;j++)
	 {
		 if(arr[i][j]=='1')
		 {
			 cnt++;
			 rows[i].pb(j);
		 }
	 }
	 if(cnt==0)
	 {
		 cout<<"-1";
		 return;
	 }
 }
ll ans=INT_MAX;
 for(int j=0;j<m;j++)
 {
	 ll xx=0;
	 for(int i=0;i<n;i++)
	 {
	   if(arr[i][j]=='1')continue;
	   ll a=INT_MAX;
	   ll ind=upper_bound(rows[i].begin(),rows[i].end(),j)-rows[i].begin();
	   if(ind==rows[i].size())
	   {
		   a=min(a,j-rows[i][ind-1]);
           a=min(a,m-j+rows[i][0]);
	   }
	   else
	   {
		   a=min(a,rows[i][ind]-j);
		   if(ind>0)
		   {
             a=min(a,j-rows[i][ind-1]);
		   }
		   else
		   {
			a=min(a,m-rows[i][rows[i].size()-1]+j);
		   }

	   }

	   xx+=a;
	 }

	//  cout<<xx<<endl;
	 ans=min(ans,xx);
 }
 cout<<ans<<endl;

}
int main()
{
	boost;
	ll t;
	t = 1;
	//----------
	// cin >> t;
	//----------
	while (t--)
		peace();
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;

typedef tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define sz(a) (ll)(a.size())
#define endl "\n"

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x)
{
    os << "{ ";
    for(auto& y : x)
    {
        os << y << " ";
    }
    return os << "}";
}

template <class X, class Y>
ostream & operator << (ostream & os, pair <X, Y> const& p)
{
    return os << "[" << p.ff << ", " << p.ss << "]";
}

ll gcd(ll a, ll b)
{
    if(b==0)
    {
        return a;
    }

    return gcd(b, a%b);
}

ll modexp(ll a, ll b, ll c)
{
    a%=c;

    ll ans = 1;

    while(b)
    {
        if(b&1)
        {
            ans = (ans*a)%c;
        }

        a = (a*a)%c;
        b >>= 1;
    }

    return ans;
}

ll nxt()
{
    ll x;
    cin >> x;
    return x;
}

const ll L = 1e4+5;
ll a[L];
ll b[L];

void solve()
{
    ll n = nxt(), m = nxt();

    for(ll i=0; i<n; i++)
    {
        string s;
        cin >> s;

        ll cnt = 0;

        for(ll j=0; j<m; j++)
        {
            if(s[j] == '1')
            {
                a[cnt] = j;
                cnt++;
            }
        }

        if(!cnt)
        {
            cout << -1 << endl;
            return;
        }

        ll cur = 0;

        for(ll j=0; j<m; j++)
        {
            while(cur < cnt && a[cur] < j)
            {
                cur++;
            }

            ll add = 0;

            if(cur < cnt)
            {
                add = a[cur] - j;
            }

            else
            {
                add = a[0] - j + m;
            }

            if(cur)
            {
                add = min(add, j - a[cur - 1]);
            }

            else
            {
                add = min(add, j - a[cnt - 1] + m);
            }

            b[j] += add;

            // cout << j << " " << add << " " << b[j] << endl;
        }
    }

    ll ans = 1e18;

    for(ll i=0; i<m; i++)
    {
        ans = min(ans, b[i]);
    }

    cout << ans << endl;
}

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

    ll T = 1;
    // T = nxt();

    while(T--)
    {
        solve();
    }

    return 0;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    ll dis[101][N]={};
    int n,m;
    cin>>n>>m;
    forn(i,n) {
       string t;
       cin>>t;
       int l = t.find('1');
       ll d  = INT_MAX;
       if(l==-1 ){
           cout<<l;
           return  0;
       }

       forn(r,m){
            if(t[r]=='1')d = 0;
            else d++;
            dis[i][r] = d;
       }
        d= INT_MAX;
        for (int j = m-1; j >-1 ; --j) {
            if(t[j]=='1')d  =0;
            else d++;
            dis[i][j] = min(dis[i][j],min(d,abs(m-d)));
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }
    cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    int dis[101][N]={};
    int n,m;
    cin>>n>>m;
    vector<vi>o(101),z(101);
    forn(i,n) {
        forn(j, m) {
            char ch;
            cin >> ch;
            if (ch - '0')o[i].pb(j);
            else {
                z[i].pb(j);
                dis[i][j] = INT_MAX;
            }

        }
    }
    forn(i,n){
        forn(j,z[i].size()){
            forn(k,o[i].size())dis[i][z[i][j]] = min(dis[i][z[i][j]],
                                                         min(abs(o[i][k]-z[i][j]),abs(m-o[i][k]-z[i][j])));
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }if(ans ==INT_MAX)cout<<-1;
    else cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    ll dis[101][N]={};
    int n,m;
    cin>>n>>m;
    forn(i,n) {
        string t;
        cin>>t;
        int l = t.find('1');
        ll d  = INT_MAX;
        if(l==-1 ){
            cout<<l;
            return  0;
        }

        forn(r,m){
            if(t[r]=='1')d = 0;
            else d++;
            dis[i][r] = d;
        }
        d= INT_MAX;
        for (int j = m-1; j >-1 ; --j) {
            if(t[j]=='1')d  =0;
            else d++;
            if(d != INT_MAX)
            dis[i][j] = min(dis[i][j],min(d,abs(m-d)));
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }
    cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    ll dis[101][N]={};
    int n,m;
    cin>>n>>m;
    forn(i,n) {
        string t;
        cin>>t;
        int l = t.find('1');
        ll d  = INT_MAX;
        if(l==-1 ){
            cout<<l;
            return  0;
        }

        forn(r,m){
            if(t[r]=='1')d = 0;
            else d++;
            dis[i][r] = min(d,abs(m-d));
        }
        d= INT_MAX;
        for (int j = m-1; j >-1 ; --j) {
            if(t[j]=='1')d  =0;
            else d++;
            if(d != INT_MAX)
            dis[i][j] = min(dis[i][j],min(d,abs(m-d)));
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }
    cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    int dis[101][N]={};
    int n,m;
    cin>>n>>m;
    vector<vi>o(101),z(101);
    forn(i,n) {
        forn(j, m) {
            char ch;
            cin >> ch;
            if (ch - '0')o[i].pb(j);
            else {
                z[i].pb(j);
                dis[i][j] = INT_MAX;
            }

        }
    }
    forn(i,n){
        forn(j,z[i].size()){
            int d = INT_MAX;
            forn(l,o[i].size()){
                 d  = min(d,abs(o[i][l]-z[i][j]));
                 d = min(d,abs(m+z[i][j]-o[i][l]));
            }
            dis[i][z[i][j]] = d;
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }if(ans ==INT_MAX)cout<<-1;
    else cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    int dis[101][N]={};
    int n,m;
    cin>>n>>m;
    vector<vi>o(101),z(101);
    forn(i,n) {
        forn(j, m) {
            char ch;
            cin >> ch;
            if (ch - '0')o[i].pb(j);
            else {
                z[i].pb(j);
                dis[i][j] = INT_MAX;
            }

        }
    }

    for (int k = 0; k <n ; ++k) {
        int  j=0;
        int i =0;
        if(o[k].empty()){
            cout<<-1;
            return 0;
        }
        while (i<z[k].size()){
            if(j<o[k].size() -1 && abs(o[k][j+1]-z[k][i])<abs(o[k][j]-z[k][i]))j++;
            if(j==o[k].size())break;
            dis[k][z[k][i]] = min(abs(o[k][j]-z[k][i]),dis[k][z[k][i]]);
            i++;
        }
        j =o[k].size() -1;
        i =0;
        while (i<z[k].size()){
            if(j>1 && abs(m - o[k][j-1]+z[k][i])<abs(m - o[k][j]+z[k][i]))j--;
            dis[k][z[k][i]] = min(abs(m - o[k][j]+z[k][i]),dis[k][z[k][i]]);i++;
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }
     cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    ll dis[101][N]={};
    int n,m;
    cin>>n>>m;
    forn(i,n) {
        string t;
        cin>>t;
        int l = t.find('1');
        ll d  = INT_MAX;
        if(l==-1 ){
            cout<<l;
            return  0;
        }

        forn(r,2*m){
            if(t[r%m]=='1')d = 0;
            else d++;
            dis[i][r%m] = d;
        }
        d= INT_MAX;
        for (int j = 2*m-1; j >-1 ; --j) {
            if(t[j%m]=='1')d  =0;
            else d++;
            dis[i][j%m] = min(dis[i][j%m],d);

        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }
    cout<<ans;
}


// And upon Allah let the believers rely.
//MRMD14
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int >ii;
typedef vector < int >vi;
typedef set < int >si;
#define forn(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define  io ios_base::sync_with_stdio(false),cin.tie(),cout.tie()
#define all(a) (a).begin(), (a).end()
////////////////////////////
const int N = 10007;
int main ()
{
    io;

    int dis[101][N]={};
    int n,m;
    cin>>n>>m;
    vector<vi>o(101),z(101);
    forn(i,n) {
        forn(j, m) {
            char ch;
            cin >> ch;
            if (ch - '0')o[i].pb(j);
            else {
                z[i].pb(j);
                dis[i][j] = INT_MAX;
            }

        }
    }
    forn(i,n){
        forn(j,z[i].size()){
            int d = INT_MAX;
            forn(l,o[i].size()){
                 d  = min(d,abs(o[i][l]-z[i][j]));
                 d = min(d,abs(m-z[i][j]+o[i][l]));
            }
            dis[i][z[i][j]] = d;
        }
    }
    ll s,ans=INT_MAX;
    forn(i,m){
        s = 0;
        forn(j,n)s+= dis[j][i];
        ans = min(ans,s);
    }if(ans ==INT_MAX)cout<<-1;
    else cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
		int final=INT_MAX;
	cin>>n>>m;
	char ar[n+1][m+1];
	int ans[m+1]={0};
	string s;
	vector< vector<int> >ones;
	for(int i=0;i<n;i++)
	{
		cin>>s;
		vector<int>temp;
		for(int j=0;j<m;j++)
		{
			if(s[j]=='1')
			temp.push_back(j);
		}
		if(temp.size()==0)
	{
		cout<<-1;
		exit(0);
		}
		else
		ones.push_back(temp);
	}
vector<int>::iterator it,it1;
int segans=INT_MAX;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
segans = INT_MAX;
it=lower_bound(ones[i].begin(),ones[i].end(),j);  //finds immediate next
if(it!=ones[i].end())
{
segans=(*it)-j; //if exists
}
if(it!=ones[i].begin())
segans=min(segans,j-(*(it-1)));
segans=min(segans,m-j+(*(ones[i].begin())));
segans=min(segans,m-(*(ones[i].rbegin()))+j);
ans[j]+=segans; //for a column
}
}

    for(int j=0;j<m;j++)
	{
        final=min(final,ans[j]);//minimum among all columns
    }
	cout<<final;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,m;
		int final=INT_MAX;
	cin>>n>>m;
	char ar[n+1][m+1];
	int ans[m+1]={0};
	string s;
	vector< vector<int> >ones;
	for(int i=0;i<n;i++)
	{
		cin>>s;
		vector<int>temp;
		for(int j=0;j<m;j++)
		{
			if(s[j]=='1')
			temp.push_back(j);
		}
		if(temp.size()==0)
	{
		cout<<-1;
		exit(0);
		}
		else
		ones.push_back(temp);
	}
vector<int>::iterator it,it1;
int segans=INT_MAX;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
segans = INT_MAX;
it=lower_bound(ones[i].begin(),ones[i].end(),j);  //finds immediate next
if(it!=ones[i].end())
{
segans=(*it)-j; //if exists
}
if(it!=ones[i].begin())
segans=min(segans,j-(*(it-1)));

ans[j]+=segans; //for a column
}
}

    for(int j=0;j<m;j++)
	{
        final=min(final,ans[j]);//minimum among all columns
    }
	cout<<final;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i , j , n , m  , k , l , p  ,q;
    scanf("%d %d",&n,&m);
    string str[n+5] , s = "";
    for( i = 0 ; i < m ; i++)
    {
        s += "0";
    }
    int left[n+5][m+5] , right[n+5][m+5];
    bool a , b = false;
    for( i = 0 ; i < n ; i++)
    {
        cin >> str[i];
        if(str[i] == s)
        {
            a = 1;
        }
    }
    if(a)
    {
        printf("-1\n");
        return 0;
    }

    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = 0 ; j < m ; j++)
        {
            if(!a && str[i][j] == '0')
            {
                left[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                a = true;
                k = j;
                left[i][j] = 0;
            }
            else
            {
                p = left[i][j-1] + 1;
                q = k + m - j;
                left[i][j] = min(p,q);
            }
        }
    }
    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = m -1 ; j >= 0 ; j--)
        {
            if(!a && str[i][j] == '0')
            {
                right[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                right[i][j] = 0;
                k = j;
                a = true;
            }
            else
            {
                p = right[i][j+1] + 1;
                q = m - k + j;
                right[i][j] = min(p,q);
            }
        }
    }

    long long pq , ans = INT_MAX;
    for( j = 0 ; j < m ; j++)
    {
        pq = 0;
        for( i = 0 ; i < n ; i++)
        {
            pq += min(left[i][j],right[i][j]);
        }
        ans = min(ans,pq);
    }
    if(ans >= INT_MAX)
    {
        ans = -1;
    }
    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i , j , n , m  , k , l , p  ,q;
    scanf("%d %d",&n,&m);
    string str[n+5];
    int left[n+5][m+5] , right[n+5][m+5];
    bool a , b;
    for( i = 0 ; i < n ; i++)
    {
        cin >> str[i];
    }

    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = 0 ; j < m ; j++)
        {
            if(!a && str[i][j] == '0')
            {
                left[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                a = true;
                k = j;
                left[i][j] = 0;
            }
            else
            {
                p = left[i][j-1] + 1;
                q = k + m - j;
                left[i][j] = min(p,q);
            }
        }
    }
    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = m -1 ; j >= 0 ; j--)
        {
            if(!a && str[i][j] == '0')
            {
                right[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                right[i][j] = 0;
                k = j;
                a = true;
            }
            else
            {
                p = right[i][j+1] + 1;
                q = m - k + j;
                right[i][j] = min(p,q);
            }
        }
    }

    long long pq , ans = INT_MAX;
    for( j = 0 ; j < m ; j++)
    {
        pq = 0;
        for( i = 0 ; i < n ; i++)
        {
            pq += min(left[i][j],right[i][j]);
        }
        ans = min(ans,pq);
    }
    if(ans >= INT_MAX)
    {
        ans = -1;
    }
    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i , j , n , m  , k , l , p  ,q;
    scanf("%d %d",&n,&m);
    string str[n+5] , s = "";
    for( i = 0 ; i < m ; i++)
    {
        s += "0";
    }
    int left[n+5][m+5] , right[n+5][m+5];
    bool a = false , b = false;
    for( i = 0 ; i < n ; i++)
    {
        cin >> str[i];
        if(str[i] == s)
        {
            a = 1;
        }
    }
    if(a)
    {
        printf("-1\n");
        return 0;
    }

    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = 0 ; j < m ; j++)
        {
            if(!a && str[i][j] == '0')
            {
                left[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                a = true;
                k = j;
                left[i][j] = 0;
            }
            else
            {
                p = left[i][j-1] + 1;
                q = k + m - j;
                left[i][j] = min(p,q);
            }
        }
    }
    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = m -1 ; j >= 0 ; j--)
        {
            if(!a && str[i][j] == '0')
            {
                right[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                right[i][j] = 0;
                k = j;
                a = true;
            }
            else
            {
                p = right[i][j+1] + 1;
                q = m - k + j;
                right[i][j] = min(p,q);
            }
        }
    }

    long long pq , ans = INT_MAX;
    for( j = 0 ; j < m ; j++)
    {
        pq = 0;
        for( i = 0 ; i < n ; i++)
        {
            pq += min(left[i][j],right[i][j]);
        }
        ans = min(ans,pq);
    }
    if(ans >= INT_MAX)
    {
        ans = -1;
    }
    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i , j , n , m  , k , l , p  ,q;
    scanf("%d %d",&n,&m);
    string str[n+5] , s = "";
    for( i = 0 ; i < m ; i++)
    {
        s += "0";
    }
    int left[n+5][m+5] , right[n+5][m+5];
    bool a = false , b = false;
    for( i = 0 ; i < n ; i++)
    {
        cin >> str[i];
        if(str[i] == s)
        {
            a = 1;
        }
    }
    if(a)
    {
        printf("-1\n");
        return 0;
    }

    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = 0 ; j < m ; j++)
        {
            if(!a && str[i][j] == '0')
            {
                left[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                a = true;
                k = j;
                left[i][j] = 0;
            }
            else
            {
                p = left[i][j-1] + 1;
                q = k + m - j;
                left[i][j] = min(p,q);
            }
        }
    }
    for( i = 0 ; i < n ; i++)
    {
        a = false;
        for( j = m -1 ; j >= 0 ; j--)
        {
            if(!a && str[i][j] == '0')
            {
                right[i][j] = INT_MAX;
            }
            else if(str[i][j] == '1')
            {
                right[i][j] = 0;
                k = j;
                a = true;
            }
            else
            {
                p = right[i][j+1] + 1;
                q = m - k + j;
                right[i][j] = min(p,q);
            }
        }
    }

    long long pq , ans = 1e11;
    for( j = 0 ; j < m ; j++)
    {
        pq = 0;
        for( i = 0 ; i < n ; i++)
        {
            pq += min(left[i][j],right[i][j]);
        }
        ans = min(ans,pq);
    }

    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int i , j , n , m  , k , l , p  ,q;
    scanf("%d %d",&n,&m);
    string str[n+5] , s = "";
    for( i = 0 ; i < m ; i++)
    {
        s += "0";
    }
    int left[n+5][m+5] , right[n+5][m+5];
    bool a = false , b = false;
    for( i = 0 ; i < n ; i++)
    {
        cin >> str[i];
        if(str[i] == s)
        {
            a = 1;
        }
    }
    if(a)
    {
        printf("-1\n");
        return 0;
    }

    for( i = 0 ; i < n ; i++)
    {
        a = false;
        b = false;
        for( j = 0 ; j < m ; j++)
        {
            if(!a && str[i][j] == '0')
            {
                left[i][j] = m+1;
            }
            else if(str[i][j] == '1')
            {
                a = true;
                if(!b)
                {
                    k = j;
                    b = b ^ 1;
                }
                left[i][j] = 0;
            }
            else
            {
                p = left[i][j-1] + 1;
                q = k + m - j;
                left[i][j] = min(p,q);
            }
        }
    }
    for( i = 0 ; i < n ; i++)
    {
        a = false;
        b = false;
        for( j = m-1 ; j >= 0 ; j--)
        {
            if(!a && str[i][j] == '0')
            {
                right[i][j] = m+1;
            }
            else if(str[i][j] == '1')
            {
                right[i][j] = 0;
                if(!b)
                {
                    k = j;
                    b = b ^ 1;
                }
                a = true;
            }
            else
            {
                p = right[i][j+1] + 1;
                q = m - k + j;
                right[i][j] = min(p,q);
            }
        }
    }

    long long pq , ans = 1e11;
    for( j = 0 ; j < m ; j++)
    {
        pq = 0;
        for( i = 0 ; i < n ; i++)
        {
            pq += min(left[i][j],right[i][j]);
        }
        ans = min(ans,pq);
    }

    printf("%lld\n",ans);

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;

  cin>>n>>m;
  char arr[n][m];
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e9;
          nearright[i][j]=1e9;
          scanf(" %c",&arr[i][j]);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearright[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearright[i][j]=last-j;
                nearright[i][j]=min(nearright[i][j],m-nearright[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

  }
  long long res=1e9;
  for (int i=0;i<m;i++)
  {
      long long ans=0;
      for (int j=0;j<n;j++)
      {
        ans+=min(nearleft[j][i],nearright[j][i]);
      }
      res=min(res,ans);

  }
  if (res==1e9)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e6;
          nearright[i][j]=1e6;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

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

  }
  if (res==1e6)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e6;
          nearright[i][j]=1e6;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

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

  }
  if (res==1e6)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e6;
          nearright[i][j]=1e6;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

  }
  int res=1e6;
  for (int i=0;i<m;i++)
  {
      int ans=0;
      for (int j=0;j<n;j++)
      {
        ans+=nearleft[j][i];
      }
      if (ans!=0)
      res=min(res,ans);

  }
  if (res==1e6)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;

  cin>>n>>m;
  char arr[n][m];
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e9;
          nearright[i][j]=1e9;
          scanf(" %c",&arr[i][j]);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

  }
  long long res=1e9;
  for (int i=0;i<m;i++)
  {
      long long ans=0;
      for (int j=0;j<n;j++)
      {
        ans+=nearleft[j][i];
      }
      res=min(res,ans);

  }
  if (res==1e9)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;

  cin>>n>>m;
  char arr[n][m];
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e9;
          nearright[i][j]=1e9;
          scanf(" %c",&arr[i][j]);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearright[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearright[i][j]=last-j;
                nearright[i][j]=min(nearright[i][j],m-nearright[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

  }
  long long res=1e9;
  for (int i=0;i<m;i++)
  {
      long long ans=0;
      for (int j=0;j<n;j++)
      {
        ans+=min(nearleft[j][i],nearright[j][i]);
      }
      res=min(res,ans);

  }
  if (res==1e9)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
  for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
         scanf(" %c",&arr[i][j]);
        // cin>>arr[i][j];
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

          int mini=1e6;
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          int ans=0;
          if (arr[i][j]=='1')
          {

              for (int k=0;k<n;k++)
              { if (vec[k].size()==0){ans=1e6;break;}
                  int miniii=1e6;
                   if (i==k)continue;
                 // cout<<i<<" "<<j<<" "<<k<<" ";
                l=(vec[k].lower_bound(j));
                u=(vec[k].upper_bound(j));
                if (l!=vec[k].end())lo=*l;
                if (u!=vec[k].end())up=*u;
                if (lo!=j&&vec[k].size()>0){l--;lo=*l;}

                 if (up!=-1)miniii=abs(up-j);
                 if (lo!=-1)miniii=min(miniii,abs(j-lo));
              //  cout<<lo<<" "<<up<<endl;
                if (miniii!=1e6)ans+=miniii;
                else {ans=1e6;break;}
                 lo=-1,up=-1;
               //  cout<<miniii<<endl;
              }
             // cout<<ans<<endl;
                  mini=min(mini,ans);

          }
      }
  }
  if (mini==1e6)cout<<-1;
  else cout<<mini;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e9;
          nearright[i][j]=1e9;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

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

  }
  if (res==1e9)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;
 int lo,up;
  cin>>n>>m;
  char arr[n][m];
  vector< set<int> >vec;
  set<int>::iterator l,u;
  vec.resize(n+5);
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e6;
          nearright[i][j]=1e6;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')vec[i].insert(j);
      }
  }

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=j-last;
            }
        }
         //   cout<<nearleft[i][j]<<" ";
      }
     // cout<<endl;
       last=-1;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1'){last=j;nearleft[i][j]=0;}
        else {
            if (last!=-1)
            {
                nearleft[i][j]=min(last-j,nearleft[i][j]);
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
               // cout<<endl<<i<<j<<endl;
            }

        }
        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;

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

  }
  if (res==1e9)cout<<-1;
  else cout<<res;

    return 0;
}


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;

void HoH()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}

int main()
{
//HoH();
  int n,m;

  cin>>n>>m;
  char arr[n][m];
  bool ch=1;
   int nearleft[n][m],nearright[n][m];
   for (int i=0;i<n;i++)
  {
      int ones=0;
      for (int j=0;j<m;j++)
      {
          nearleft[i][j]=1e9;
          nearright[i][j]=1e9;
          scanf(" %c",&arr[i][j]);
          if (arr[i][j]=='1')ones++;
      }
      if (!ones)ch=0;
  }
  if (!ch){cout<<-1;return 0;}

   for (int i=0;i<n;i++)
  {
      int last=-1;
      for (int j=0;j<m;j++)if (arr[i][j]=='1')last=j;
      for (int j=0;j<m;j++)
      {
             if (arr[i][j]=='1')last=j;
                nearleft[i][j]=abs(j-last);
                nearleft[i][j]=min(nearleft[i][j],m-nearleft[i][j]);
      }
         //   cout<<nearleft[i][j]<<" ";

     // cout<<endl;
       last=-1;
       for (int j=m-1;j>=0;j--)if (arr[i][j]=='1')last=j;
      for (int j=m-1;j>=0;j--)
      {
        if (arr[i][j]=='1')last=j;

                nearright[i][j]=abs(j-last);
                nearright[i][j]=min(nearright[i][j],m-nearright[i][j]);
               // cout<<endl<<i<<j<<endl;

        //cout<<nearleft[i][j]<<" ";
      }
      //cout<<endl;
  }

  long long res=1e9;
  for (int i=0;i<m;i++)
  {
      long long ans=0;
      for (int j=0;j<n;j++)
      {
        ans+=min(nearleft[j][i],nearright[j][i]);
      }
      res=min(res,ans);

  }

   cout<<res;

    return 0;
}


using namespace std;
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define fast ios_base::sync_with_stdio(false),in.tie(0),cout.tie(0);
#define input freopen("input.in","right_interval",stdin);
#define output freopen("output.out","w",stdout);
#define e printf("\n");
#define all(it,v) for(it=(v).begin();it!=(v).end();it++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define si(x) scanf("%d",&x)
#define sf(x) scanf("%f",&x)
#define sl(x) scanf("%lld",&x)
#define pi(x) printf("%d",x);
#define pf(x) printf("%f",x);
#define pl(x) printf("%lld",x);
typedef vector<vector<ll> > mat;
const ll mx=100001;
const ll mod=1000000007;
const ll inf=0x3f3f3f3f;
//ifstream in("input.txt");
//ofstream out("output.txt");
void solve();
int main()
{
  int t=1;
//  si(t);
  rep(i,1,t)
  {
    solve();
    if(i!=t) e
  }
  return 0;
}
string s[101];
int L[101][10001],R[101][10001];
void solve()
{
  int i,j,k,m,n,mn=1e9;
  si(n); si(m);
  rep(i,0,n-1) cin>>s[i];
  for(i=0;i<n;i++)
  {
    if(s[i][0]=='0') L[i][0]=-1; else L[i][0]=0;
    for(j=1;j<m;j++)
    {
      if(s[i][j]=='1') L[i][j]=j;
      else L[i][j]=L[i][j-1];
    }
    if(s[i][m-1]=='0') R[i][m-1]=-1; else R[i][m-1]=m-1;
    for(j=m-2;j>=0;j--)
      if(s[i][j]=='1') R[i][j]=j;
      else
        R[i][j]=R[i][j+1];
  }
  for(j=0;j<m;j++)
  {
    int C=0,nn=1e9;
    for(i=0;i<n;i++)
    {
      nn=1e9;
      if(L[i][j]==-1 && R[i][j]==-1)
      {
        cout<<"-1"; return;
      }
      if(L[i][j]!=-1)
      {
        nn=min(nn,min(j-L[i][j],L[i][j]+m-j));
      }
      if(R[i][j]!=-1)
      {
        nn=min(nn,min(R[i][j]-j,j+m-R[i][j]));
      }
      C+=nn;
    }
  //  cout<<C<<" ";
    mn=min(mn,C);
  }
  cout<<mn;
}


using namespace std;
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define fast ios_base::sync_with_stdio(false),in.tie(0),cout.tie(0);
#define input freopen("input.in","right_interval",stdin);
#define output freopen("output.out","w",stdout);
#define e printf("\n");
#define all(it,v) for(it=(v).begin();it!=(v).end();it++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define si(x) scanf("%d",&x)
#define sf(x) scanf("%f",&x)
#define sl(x) scanf("%lld",&x)
#define pi(x) printf("%d",x);
#define pf(x) printf("%f",x);
#define pl(x) printf("%lld",x);
typedef vector<vector<ll> > mat;
const ll mx=100001;
const ll mod=1000000007;
const ll inf=0x3f3f3f3f;
//ifstream in("input.txt");
//ofstream out("output.txt");
void solve();
int main()
{
  int t=1;
//  si(t);
  rep(i,1,t)
  {
    solve();
    if(i!=t) e
  }
  return 0;
}
string s[101];
int L[101][10001],R[101][10001];
void solve()
{
  int i,j,k,m,n,mn=1e9;
  si(n); si(m);
  rep(i,0,n-1) cin>>s[i];
  for(i=0;i<n;i++)
  {
    if(s[i][0]=='0') L[i][0]=-1; else L[i][0]=0;
    for(j=1;j<m;j++)
    {
      if(s[i][j]=='1') L[i][j]=j;
      else L[i][j]=L[i][j-1];
    }
    if(s[i][m-1]=='0') R[i][m-1]=-1; else R[i][m-1]=m-1;
    for(j=m-2;j>=0;j--)
      if(s[i][j]=='1') R[i][j]=j;
      else
        R[i][j]=R[i][j+1];
  }
/*  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
      cout<<L[i][j]<<" ";
    e
  }
  e
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
      cout<<R[i][j]<<" ";
    e
  }
  e*/
  for(j=0;j<m;j++)
  {
    int C=0,nn=1e9;
    for(i=0;i<n;i++)
    {
      nn=1e9;
      if(L[i][j]==-1 && R[i][j]==-1)
      {
        cout<<"-1"; return;
      }
      if(L[i][j]!=-1) nn=min(nn,j-L[i][j]);
      if(L[i][m-1]!=-1) nn=min(nn,m-L[i][m-1]+j);
      if(R[i][j]!=-1) nn=min(nn,R[i][j]-j);
      if(R[i][0]!=-1) nn=min(nn,R[i][0]+m-j);
      C+=nn;
    }
  //  cout<<C<<" ";
    mn=min(mn,C);
  }
  cout<<mn;
}


using namespace std;
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define fast ios_base::sync_with_stdio(false),in.tie(0),cout.tie(0);
#define input freopen("input.in","right_interval",stdin);
#define output freopen("output.out","w",stdout);
#define e printf("\n");
#define all(it,v) for(it=(v).begin();it!=(v).end();it++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define si(x) scanf("%d",&x)
#define sf(x) scanf("%f",&x)
#define sl(x) scanf("%lld",&x)
#define pi(x) printf("%d",x);
#define pf(x) printf("%f",x);
#define pl(x) printf("%lld",x);
typedef vector<vector<ll> > mat;
const ll mx=100001;
const ll mod=1000000007;
const ll inf=0x3f3f3f3f;
//ifstream in("input.txt");
//ofstream out("output.txt");
void solve();
int main()
{
  int t=1;
//  si(t);
  rep(i,1,t)
  {
    solve();
    if(i!=t) e
  }
  return 0;
}
string s[101];
int L[101][1001],R[101][1001];
void solve()
{
  int i,j,k,m,n,mn=1e9;
  si(n); si(m);
  rep(i,0,n-1) cin>>s[i];
  for(i=0;i<n;i++)
  {
    if(s[i][0]=='0') L[i][0]=-1; else L[i][0]=0;
    if(s[i][m-1]=='0') R[i][m-1]=-1; else R[i][m-1]=m-1;
    for(j=1;j<m;j++)
    {
      if(s[i][j]=='1') L[i][j]=j;
      else L[i][j]=L[i][j-1];
      if(s[i][m-j-1]=='1') R[i][m-j-1]=m-j-1;
      else R[i][m-j-1]=R[i][m-j];
    }
  }
  for(j=0;j<m;j++)
  {
    int C=0,nn=1e9;
    for(i=0;i<n;i++)
    {
      nn=1e9;
      if(L[i][j]==-1 && R[i][j]==-1)
      {
        cout<<"-1"; return;
      }
      if(L[i][j]!=-1)
      {
        nn=min(nn,min(j-L[i][j],L[i][j]+m-j));
      }
      if(R[i][j]!=-1)
      {
        nn=min(nn,min(R[i][j]-j,j+m-R[i][j]));
      }
      C+=nn;
    }
  //  cout<<C<<" ";
    mn=min(mn,C);
  }
  cout<<mn;
}


using namespace std;
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define fast ios_base::sync_with_stdio(false),in.tie(0),cout.tie(0);
#define input freopen("input.in","right_interval",stdin);
#define output freopen("output.out","w",stdout);
#define e printf("\n");
#define all(it,v) for(it=(v).begin();it!=(v).end();it++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define si(x) scanf("%d",&x)
#define sf(x) scanf("%f",&x)
#define sl(x) scanf("%lld",&x)
#define pi(x) printf("%d",x);
#define pf(x) printf("%f",x);
#define pl(x) printf("%lld",x);
typedef vector<vector<ll> > mat;
const ll mx=100001;
const ll mod=1000000007;
const ll inf=0x3f3f3f3f;
//ifstream in("input.txt");
//ofstream out("output.txt");
void solve();
int main()
{
  int t=1;
//  si(t);
  rep(i,1,t)
  {
    solve();
    if(i!=t) e
  }
  return 0;
}
string s[101];
int L[101][1001],R[101][1001];
void solve()
{
  int i,j,k,m,n,mn=1e9;
  si(n); si(m);
  rep(i,0,n-1) cin>>s[i];
  for(i=0;i<n;i++)
  {
    if(s[i][0]=='0') L[i][0]=-1; else L[i][0]=0;
    if(s[i][m-1]=='0') R[i][m-1]=-1; else R[i][m-1]=m-1;
    for(j=1;j<m;j++)
    {
      if(s[i][j]=='1') L[i][j]=j;
      else L[i][j]=L[i][j-1];
      if(s[i][m-j-1]=='1') R[i][m-j-1]=m-j-1;
      else R[i][m-j-1]=R[i][m-j];
    }
  }
  for(j=0;j<m;j++)
  {
    int C=0,nn=1e9;
    for(i=0;i<n;i++)
    {
      nn=1e9;
      if(L[i][j]==-1 && R[i][j]==-1)
      {
        cout<<"-one"; return;
      }
      if(L[i][j]!=-1)
      {
        nn=min(nn,min(j-L[i][j],L[i][j]+m-j));
      }
      if(R[i][j]!=-1)
      {
        nn=min(nn,min(R[i][j]-j,j+m-R[i][j]));
      }
      C+=nn;
    }
  //  cout<<C<<" ";
    mn=min(mn,C);
  }
  cout<<mn;
}


using namespace std;
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define fast ios_base::sync_with_stdio(false),in.tie(0),cout.tie(0);
#define input freopen("input.in","right_interval",stdin);
#define output freopen("output.out","w",stdout);
#define e printf("\n");
#define all(it,v) for(it=(v).begin();it!=(v).end();it++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define si(x) scanf("%d",&x)
#define sf(x) scanf("%f",&x)
#define sl(x) scanf("%lld",&x)
#define pi(x) printf("%d",x);
#define pf(x) printf("%f",x);
#define pl(x) printf("%lld",x);
typedef vector<vector<ll> > mat;
const ll mx=100001;
const ll mod=1000000007;
const ll inf=0x3f3f3f3f;
//ifstream in("input.txt");
//ofstream out("output.txt");
void solve();
int main()
{
  int t=1;
//  si(t);
  rep(i,1,t)
  {
    solve();
    if(i!=t) e
  }
  return 0;
}
string s[101];
int L[101][10001],R[101][10001];
void solve()
{
  int i,j,k,m,n,mn=1e9;
  si(n); si(m);
  rep(i,0,n-1) cin>>s[i];
  for(i=0;i<n;i++)
  {
    if(s[i][0]=='0') L[i][0]=-1; else L[i][0]=0;
    if(s[i][m-1]=='0') R[i][m-1]=-1; else R[i][m-1]=m-1;
    for(j=1;j<m;j++)
    {
      if(s[i][j]=='1') L[i][j]=j;
      else L[i][j]=L[i][j-1];
      if(s[i][m-j-1]=='1') R[i][m-j-1]=m-j-1;
      else R[i][m-j-1]=R[i][m-j];
    }
  }
  for(j=0;j<m;j++)
  {
    int C=0,nn=1e9;
    for(i=0;i<n;i++)
    {
      nn=1e9;
      if(L[i][j]==-1 && R[i][j]==-1)
      {
        cout<<"-1"; return;
      }
      if(L[i][j]!=-1)
      {
        nn=min(nn,min(j-L[i][j],L[i][j]+m-j));
      }
      if(R[i][j]!=-1)
      {
        nn=min(nn,min(R[i][j]-j,j+m-R[i][j]));
      }
      C+=nn;
    }
  //  cout<<C<<" ";
    mn=min(mn,C);
  }
  cout<<mn;
}


#include <bits/stdc++.h>
#define ll long long
#define MAXM 10010
#define MAXN 110
using namespace std;

int min_s[MAXN][MAXM], col[MAXM], lf[MAXM], rt[MAXM];

int main(){
	int n,m;

	cin>>n>>m;

	for(int i=0; i<n; i++){
		int f=999999999, l=-1;
		int last_left=-1;
		string col;
		cin>>col;

		for(int j=0; j<m; j++){

			if(col[j] == '1'){
				last_left=j;
				f = min(f,j);
				l = max(l,j);
			}

			lf[j]=last_left;
		}

		int last_right = -1;

		for(int j=m-1; j>=0; j--){
			if(col[j] == '1')
				last_right=j;

			rt[j]=last_right;
		}

		for(int j=m-1; j>=0; j--){

			min_s[i][j] = 999999999;

			if(lf[j] != -1) min_s[i][j] =  min(min_s[i][j], j - lf[j]);
			if(rt[j] != -1) min_s[i][j] =  min(min_s[i][j], rt[j]-j);

			min_s[i][j] =  min(min_s[i][j], f + m-j);
			if(l != -1) min_s[i][j] =  min(min_s[i][j], m-l + j);
		}
	}

	int res = 99999999;

	for(int j=0; j<m; j++){
		int mv=0;
		for(int i=0; i<n; i++){
			mv += min_s[i][j];
			if(mv > 9999999) break;
		}

		res = min(res,mv);
	}

	cout << (res < 999999 ? res : -1) <<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define MAXM 10010
#define MAXN 110
using namespace std;

int min_s[MAXN][MAXM], col[MAXM], lf[MAXM], rt[MAXM];

int main(){
	int n,m;

	cin>>n>>m;

	for(int i=0; i<n; i++){
		int f=999999999, l=-1;
		int last_left=-1;
		string col;
		cin>>col;

		for(int j=0; j<m; j++){

			if(col[j] == '1'){
				last_left=j;
				f = min(f,j);
				l = max(l,j);
			}

			lf[j]=last_left;
		}

		int last_right = -1;

		for(int j=m-1; j>=0; j--){
			if(col[j] == '1')
				last_right=j;

			rt[j]=last_right;
		}

		for(int j=m-1; j>=0; j--){

			min_s[i][j] = 999999999;

			if(lf[j] != -1) min_s[i][j] =  min(min_s[i][j], j - lf[j]);
			if(rt[j] != -1) min_s[i][j] =  min(min_s[i][j], rt[j]-j);

			min_s[i][j] =  min(min_s[i][j], f + m-j);
			if(l != -1) min_s[i][j] =  min(min_s[i][j], m-l + j);
		}
	}

	int res = 999999999;

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

		res = min(res,mv);
	}

	cout << (res < 999999 ? res : -1) <<endl;
}


#include <bits/stdc++.h>

#ifdef PRINTERS
#include "printers.hpp"
using namespace printers;
#define tr(a)		cerr<<#a<<" : "<<a<<endl;
#else
#define tr(a)
#endif

#define ll          long long
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define hell        1000000007
#define endl        '\n'
#define rep(i,a,b)	for(int i=a;i<b;i++)
#define rep1(i,b)	for(int i=1;i<=b;i++)
using namespace std;
#define MAXN 1000005

void sieve(bool prime[],int n){
    for(int i=2;i*i<=n;i++)
    {
        if(prime[i])
        for(int j=i*2;j<=n;j+=i)
                prime[j] =false;
    }
}

void solve(){
	int n,m;
	cin>>n>>m;
	int a[n][m];
	memset(a,0,sizeof a);
	rep(i,0,n){

		string s;
		cin>>s;
		size_t pos =s.find('1');
		if(pos==string::npos){cout<<"-1";return;}
		int dis =0;
		rep(j,0,m)
		{
			if(s[(j+pos)%m]=='1') dis =0;
			else dis++;
			a[i][(j+pos)%m] =dis;
		}
		for(int j=m;j>0;j--){
			if(s[(j+pos)%m] =='1') dis =0;
			else dis++;
			a[i][(j+pos)%m] =min(dis,a[i][(j+pos)%m]);
		}
	}

	int ans =int(1e9);
	rep(i,0,m)
	{
		int s =0;
		rep(j,0,n)
		s+=a[j][i];
	ans =min(ans,s);

	}
	cout<<ans;

}
	int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
	solve();
}
	return 0;
}


#include <bits/stdc++.h>
#define pb(a)           push_back(a)
#define ll              long long int
#define scd(a)          scanf("%d",&a)
#define mp(a,b)         make_pair(a,b)
#define scl(w)          scanf("%I64d",&w)
#define scdd(a,b)       scanf("%d %d",&a,&b)
#define srt(a)          sort(a.begin(),a.end())
#define rsrt(a)         sort(a.rbegin(),a.rend())
#define scll(a,b)       scanf("%I64d %I64d",&a,&b)
#define input(v,n)      for (ll i=0;i<n;i++){ll a;cin >> a;v.push_back(a);}
#define input2(v1,v2,n) for (ll i=0;i<n;i++){ll a,b;cin >> a>>b;v1.push_back(a);v2.pb(b);}
#define rep(a,n)        for(ll i=a;i<n;i++)
#define vll             vector<ll>

using namespace std;

int main()
{
    int n,res=0,t1,t2,t3=0;
    map<int,map<int,int> >m;
    vector<pair<int,int> >v;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t1>>t2;
        v.pb(mp(t1,t2));
        if(m[t1][t2]>0)
        {
            cout<<m[t1][t2]<<" "<<(i+1)<<endl;
            return 0;
        }
        m[t1][t2]=i+1;
    }
    rsrt(v);
    for(int i=1;i<n;i++)
    {
        if(v[i-1].first==v[i].first)
        {
            if(v[i-1].second<=v[i].second)cout<<m[v[i-1].first][v[i-1].second]<<" "<<m[v[i].first][v[i].second]<<endl;
            else cout<<m[v[i].first][v[i].second]<<" "<<m[v[i-1].first][v[i-1].second]<<endl;
            //cout<<"if";
            return 0;

        }
        if(v[i-1].second<=v[i].second)
        {
            cout<<m[v[i-1].first][v[i-1].second]<<" "<<m[v[i].first][v[i].second]<<endl;
            return 0;

        }
    }
    cout<<-1<<" "<<-1<<endl;

}


#include <bits/stdc++.h>
#define pb(a)           push_back(a)
#define ll              long long int
#define scd(a)          scanf("%d",&a)
#define mp(a,b)         make_pair(a,b)
#define scl(w)          scanf("%I64d",&w)
#define scdd(a,b)       scanf("%d %d",&a,&b)
#define srt(a)          sort(a.begin(),a.end())
#define rsrt(a)         sort(a.rbegin(),a.rend())
#define scll(a,b)       scanf("%I64d %I64d",&a,&b)
#define input(v,n)      for (ll i=0;i<n;i++){ll a;cin >> a;v.push_back(a);}
#define input2(v1,v2,n) for (ll i=0;i<n;i++){ll a,b;cin >> a>>b;v1.push_back(a);v2.pb(b);}
#define rep(a,n)        for(ll i=a;i<n;i++)
#define vll             vector<ll>

using namespace std;

int main()
{
    int n,res=0,t1,t2,t3=0;
    map<int,map<int,int> >m;
    vector<pair<int,int> >v;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t1>>t2;
        v.pb(mp(t1,t2));
        if(m[t1][t2]>0)
        {
            cout<<m[t1][t2]<<" "<<(i+1)<<endl;
            return 0;
        }
        m[t1][t2]=i+1;
    }
    srt(v);
    for(int i=1;i<n;i++)
    {
        if(v[i-1].first>=v[i-1].first&&v[i-1].second<=v[i].second)
        {
            cout<<m[v[i-1].first][v[i-1].second]<<" "<<m[v[i].first][v[i].second]<<endl;
            return 0;

        }
    }
    cout<<-1<<" "<<-1<<endl;

}


#include <bits/stdc++.h>
#define pb(a)           push_back(a)
#define ll              long long int
#define scd(a)          scanf("%d",&a)
#define mp(a,b)         make_pair(a,b)
#define scl(w)          scanf("%I64d",&w)
#define scdd(a,b)       scanf("%d %d",&a,&b)
#define srt(a)          sort(a.begin(),a.end())
#define rsrt(a)         sort(a.rbegin(),a.rend())
#define scll(a,b)       scanf("%I64d %I64d",&a,&b)
#define input(v,n)      for (ll i=0;i<n;i++){ll a;cin >> a;v.push_back(a);}
#define input2(v1,v2,n) for (ll i=0;i<n;i++){ll a,b;cin >> a>>b;v1.push_back(a);v2.pb(b);}
#define rep(a,n)        for(ll i=a;i<n;i++)
#define vll             vector<ll>

using namespace std;

int main()
{
    int n,res=0,t1,t2,t3=0;
    map<int,map<int,int> >m;
    vector<pair<int,int> >v;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t1>>t2;
        v.pb(mp(t1,t2));
        if(m[t1][t2]>0)
        {
            cout<<m[t1][t2]<<" "<<(i+1)<<endl;
            return 0;
        }
        m[t1][t2]=i+1;
    }
    rsrt(v);
    for(int i=1;i<n;i++)
    {
        if(v[i-1].first==v[i].first)t3=min(v[i-1].second,v[i].second);
        else t3=1e9+1;
        if(v[i-1].second<=v[i].second)
        {
            cout<<m[v[i-1].first][v[i-1].second]<<" "<<m[v[i].first][v[i].second]<<endl;
            return 0;

        }
        else if(t3<=v[i].second)
        {
            cout<<m[t3][v[i-1].second]<<" "<<m[t3][v[i].second]<<endl;
            return 0;
        }
    }
    cout<<-1<<" "<<-1<<endl;

}


#include <bits/stdc++.h>
#define pb(a)           push_back(a)
#define ll              long long int
#define scd(a)          scanf("%d",&a)
#define mp(a,b)         make_pair(a,b)
#define scl(w)          scanf("%I64d",&w)
#define scdd(a,b)       scanf("%d %d",&a,&b)
#define srt(a)          sort(a.begin(),a.end())
#define rsrt(a)         sort(a.rbegin(),a.rend())
#define scll(a,b)       scanf("%I64d %I64d",&a,&b)
#define input(v,n)      for (ll i=0;i<n;i++){ll a;cin >> a;v.push_back(a);}
#define input2(v1,v2,n) for (ll i=0;i<n;i++){ll a,b;cin >> a>>b;v1.push_back(a);v2.pb(b);}
#define rep(a,n)        for(ll i=a;i<n;i++)
#define vll             vector<ll>

using namespace std;

int main()
{
    int n,res=0,t1,t2,t3=0;
    map<int,map<int,int> >m;
    vector<pair<int,int> >v;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>t1>>t2;
        v.pb(mp(t1,t2));
        if(m[t1][t2]>0)
        {
            cout<<m[t1][t2]<<" "<<(i+1)<<endl;
            return 0;
        }
        m[t1][t2]=i+1;
    }
    srt(v);
    for(int i=1;i<n;i++)
    {
        if(v[i-1].first<=v[i-1].first&&v[i-1].second>=v[i].second)
        {
            cout<<m[v[i-1].first][v[i-1].second]<<" "<<m[v[i].first][v[i].second]<<endl;
            return 0;

        }
    }
    cout<<-1<<" "<<-1<<endl;

}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAXN 1
#define ll long long

int Prime[MAXN];
bool NotPrime[MAXN];
int prime_total;

void generate_prime(int n){
    //prime_total = 0;
    for(ll i = 2; i <= n; i++){
        if(!NotPrime[i]){
            Prime[prime_total++] = i;
            for(ll j = i*i; j <=n; j+=i){
                NotPrime[j]=true;
            }
        }
    }
}

void solve(int number){
    for(int i = 0; i < prime_total;i++){
        int a = Prime[i];
        int b = *lower_bound(Prime,Prime+prime_total,number-a);
        if(a+b==number){
            cout << number << " = " << a << " + " << b << endl;
            break;
        }
    }
    return ;
}

int main()
{
    cout<<2<<' '<<1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
#include<ctime>
using namespace std;
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;int time=clock();
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
        if(m[j].size()>1){
            auto a=m[j].begin();a++;
            cout<<m[j].begin()->second<<" "<<a->second<<endl;return 0;
        }
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;if(clock()-time>1900){
            cout<<-1<<' '<<-1<<endl;return 0;
        }
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second<<endl;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[d].begin()->second<<' '<<m[b].begin()->second<<endl;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct segment{
    int l,r,i;
}sg[300005];
bool cmp(segment a,segment b)
{
    if(a.l==b.l)return a.r>b.r;
    return a.l<b.l;
}
int main()
{
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&sg[i].l,&sg[i].r);sg[i].i=i;
    }
    sort(sg+1,sg+1+n,cmp);
    for(i=2;i<=n;i++){
        if(sg[i].r<=sg[i-1].r){
            cout<<sg[i].i<<' '<<sg[i-1].i<<endl;return 0;
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
//struct segment{
//    int l,r;
//}num[300009];
//bool operator<(segment a,segment b)
//{
//    return a.l<b.l;
//}
//bool cmp(segment a,segment b)
//{
//    if(a.l==b.l)return a.r<b.r;
//    return a.l<b.l;
//}
//int main()
//{
//    int n,i,j,k;
//    cin>>n;
//    set<int>s;
//    for(i=1;i<=n;i++){
//        scanf("%d%d",&num[i].l,&num[i].r);s.insert(num[i].l);
//    }
//    sort(num+1,num+1+n,cmp);
//    for(auto a=s.begin();a!=s.end();a++){
//
//    }
//}
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second<<endl;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[b].begin()->second<<' '<<m[d].begin()->second<<endl;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct segment{
    int l,r,i;
}sg[300005];
bool cmp(segment a,segment b)
{
    if(a.l==b.l)return a.r>b.r;
    return a.l<b.l;
}
int main()
{
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&sg[i].l,&sg[i].r);sg[i].i=i;
    }
    sort(sg+1,sg+1+n,cmp);
    for(i=2;i<=n;i++){
        if(sg[i].r<=sg[i-1].r){
            cout<<sg[i-1].i<<' '<<sg[i].i<<endl;return 0;
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second<<endl;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[d].begin()->second<<' '<<m[b].begin()->second<<endl;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
        if(m[j].size()>1){
            auto a=m[j].begin();a++;
            cout<<m[j].begin()->second<<" "<<a->second<<endl;return 0;
        }
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second<<endl;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[d].begin()->second<<' '<<m[b].begin()->second<<endl;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second<<endl;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[b].begin()->second<<' '<<m[d].begin()->second<<endl;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    typedef multiset<pair<int,int> >S;
    map<int,S>m;set<int>s;
    int n,i,j,k;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&j,&k);m[j].insert(pair<int,int>(k,i));s.insert(j);
    }
    for(auto a=s.begin();a!=s.end();a++){
        int b=*a;
        if(m[b].size()>1){
            auto c=m[b].begin();cout<<c->second<<' ';c++;cout<<c->second;return 0;
        }
        auto c=a;c++;
        for(;c!=s.end();c++){
            int d=*c;
            if(m[d].begin()->first<=m[b].begin()->first){
                cout<<m[b].begin()->second<<' '<<m[d].begin()->second;return 0;
            }
        }
    }
    cout<<-1<<' '<<-1<<endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

using INT = long long;

const int NN = 303030;

int L[NN], R[NN];
int id[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; cin >> n;
	for(int i=1; i<=n; i++) {
		scanf("%d %d", L+i, R+i);
		id[i] = i;
	}
	sort(id+1, id+n+1, [&] (int u, int v) {
		if(L[u] == L[v]) return R[u] > R[v];
		return L[u] < L[v];
	});
	for(int i=1; i<n; i++) {
		if(R[id[i]] >= R[id[i+1]]) {
			cout << id[i+1] << ' ' << id[i] << endl;
			return 0;
		}
	}
	cout << -1 << ' ' << -1 << endl;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 300100
using namespace std;

using INT = long long;
using pii = pair<int, int>;

pii p[NN];
int id[NN];

bool cmp(int u, int v){
	if(p[u].x == p[v].x) return p[u].y > p[v].y;
	return p[u].x < p[v].x;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
	for(int i = 1; i <= n; i++) id[i] = i;
	sort(id + 1, id + n + 1, cmp);
	int mx = 0;
	int ID = 0;
	for(int i = 1; i <= n; i++){
		if(mx >= p[id[i]].y) {
			cout << id[i] <<" "<<ID<<endl;
			return 0;
		}
		else {
			mx = p[id[i]].y;
			ID = id[i];
		}
	}
	cout <<"-1 -1"<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 300010
int L[NN], R[NN];
int id[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base :: sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	for(int i=1; i<=n; i++) cin >> L[i] >> R[i];
	for(int i=1; i<=n; i++) id[i] = i;
	sort(id + 1, id + n + 1, [&] (int i, int j){
		return R[i] < R[j];
	});
	int mx = 0, ID = 0;
	for(int i=1; i<=n; i++){
		int x = L[id[i]];
		if(mx >= x){
			cout << ID << ' ' << id[i] << endl;
			return 0;
		}
		if(mx < x){
			mx = x;
			ID = id[i];
		}
	}
	cout << -1 << ' ' << -1 << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 300010
int l[NN], r[NN];
int id[NN];
int n;

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for(int i=1; i<=n; i++) cin >> l[i] >> r[i];
	for(int i=1; i<=n; i++) id[i] = i;
	sort(id+1, id+n+1, [&] (int u, int v) {
		if(l[u] == l[v]) return r[u] > r[v];
		return l[u] < l[v];
	});
	int mx = 0;
	int ID = -1, JD = -1;
	for(int i=1; i<=n; i++) {
		if(mx >= r[id[i]]) {
			cout << id[i] << " " << ID << endl;
			return 0;
		}
		mx = r[id[i]], ID = id[i];
	}

	cout << -1 << " " << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
#define NN 300010
int l[NN], r[NN];
int n;
int L[NN], R[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> n;
	for(int i=1; i<=n; i++) cin >> l[i] >> r[i];
	for(int i=1; i<=n; i++) L[i] = l[i], R[i] = r[i];
	sort(L+1, L+n+1);
	sort(R+1, R+n+1);
	int ID = -1, JD = -1;
	for(int i=1; i<=n; i++) {
		int cnt = 0;
		cnt =n - (lower_bound(L+1, L+n+1, r[i]) - L);
		cnt += lower_bound(R+1, R+n+1, l[i]) - R;
		if(n - cnt > 1) {ID = i; break;}
	}
	for(int i=1; i<=n; i++) {
		if(i == ID) continue;
		if(l[ID] <= l[i] and r[ID] >= r[i]) {JD = i; break;}
	}

	if(ID == -1 or JD == -1) return puts("-1 -1");
	cout << ID << " " << JD << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using pii=pair<int, int>;
using pi3=pair<pii, int>;

const int NN = 303030;

pi3 p[NN];
set<pii> s;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; cin>>n;
	for(int i=1; i<=n; i++) {
		scanf("%d %d", &p[i].first.first, &p[i].first.second);
		p[i].second=i;
	}
	sort(p+1, p+n+1);
	s.insert(pii(0, 0));
	s.insert(pii(2e9, 0));
	for(int i=1; i<=n; i++) {
		auto it=s.lower_bound(pii(p[i].first.second, 0));
//		it--;
		if(it->second) {
			cout<<p[i].second<<" ";
			cout<<it->second<<endl;
			return 0;
		}
		s.insert(pii(p[i].first.second, p[i].second));
	}
	puts("-1");

    return 0;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define NN 300100
using namespace std;

using INT = long long;
using pii = pair<int, int>;

pii p[NN];
int id[NN];

bool cmp(int u, int v){
	return p[u] < p[v];
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
	for(int i = 1; i <= n; i++) id[i] = i;

	int mx = 0;
	int ID = 0;
	for(int i = 1; i <= n; i++){
		if(mx >= p[id[i]].y) {
			cout << id[i] <<" "<<ID<<endl;
			return 0;
		}
		else {
			mx = p[id[i]].y;
			ID = id[i];
		}
	}
	cout <<"-1 -1"<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define NN 300100
#define inf 1000000020

using namespace std;
using INT=long long;
using pii=pair<int, int>;
using pi3=pair<pii, int>;
pi3 p[NN];
int mn[NN], l[NN], r[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)	{
		int l, r;
		scanf("%d %d", &l, &r);
		p[i]=pi3(pii(l, -r), i);
	}
	for(int i=1; i<=n+1; i++) mn[i]=inf;
	sort(p+1, p+n+1);
	for(int i=n; i>=1; i--) {
		mn[i]=min(mn[i+1], -p[i].first.second);
	}
	for(int i=1; i<n; i++) {
		if(mn[i+1]<=-p[i].first.second) {
			for(int j=i+1; j<=n; j++) if(p[j].first.second>=p[i].first.second) {
				cout<<p[j].second<<" "<<p[i].second<<endl;
				return 0;
			}
		}
	}
	puts("-1 -1");

	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
const int NN=300300;
pii p[NN];
int id[NN];
bool cmp(int u,int v){
	return p[u]<p[v];
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y),id[i]=i;
	sort(id+1,id+n+1,cmp);
	for(int i=1;i<n;i++){
		int u=id[i],v=id[i+1];
		if(p[u].y>=p[v].y){
			cout<<v<<' '<<u<<endl;
			return 0;
		}
	}
	cout<<-1<<' '<<-1<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define NN 300100

using namespace std;
using INT=long long;
using pii=pair<int, int>;

pii p[NN];
int mn[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	memset(mn, 0x3f, sizeof mn);
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)	{
		int l, r;
		scanf("%d %d", &l, &r);
		p[i]=pii(l, -r);
	}
	sort(p+1, p+n+1);
	for(int i=n; i>=1; i--) {
		mn[i]=min(mn[i+1], -p[i].second);
	}
	for(int i=1; i<n; i++) {
		if(mn[i+1]<=-p[i].second) {
			for(int j=i+1; j<=n; j++) if(p[j].second>=p[i].second) {
				cout<<j<<" "<<i<<endl;
				return 0;
			}
		}
	}
	puts("-1 -1");

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 300010
int L[NN], R[NN];
int id[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base :: sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	int n;
	cin >> n;
	for(int i=1; i<=n; i++) cin >> L[i] >> R[i];
	for(int i=1; i<=n; i++) id[i] = i;
	sort(id + 1, id + n + 1, [&] (int i, int j){
		if(R[i] == R[j]) return L[i] > L[j];
		return R[i] < R[j];
	});
	int mx = 0, ID = 0;
	for(int i=1; i<=n; i++){
		int x = L[id[i]];
		if(mx >= x){
			cout << ID << ' ' << id[i] << endl;
			return 0;
		}
		if(mx < x){
			mx = x;
			ID = id[i];
		}
	}
	cout << -1 << ' ' << -1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
int id[N], L[N], R[N];
int main() {
	int n; cin>>n;
	for(int i=1; i<=n; i++) scanf("%d %d", L+i, R+i), id[i] = i;
	sort(id+1, id+n+1, [&](int i, int j) {return L[i] != L[j] ? L[i] < L[j] : R[i] > R[j];});
	for(int i=2; i<=n; i++) if(R[id[i]] <= R[id[i-1]]) return cout<<id[i]<<' '<<id[i-1]<<endl, 0;
	puts("-1 -1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using pii=pair<int, int>;
using pi3=pair<pii, int>;

const int NN = 303030;

pi3 p[NN];
set<pii> s;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; cin>>n;
	for(int i=1; i<=n; i++) {
		scanf("%d %d", &p[i].first.first, &p[i].first.second);
		p[i].second=i;
	}
	sort(p+1, p+n+1, [&](pi3 u, pi3 v) {
		if(u.first.first!=v.first.first) return u.first.first<v.first.first;
		return u.first.second > v.first.second;
	});
	s.insert(pii(0, 0));
	s.insert(pii(2e9, 0));
	for(int i=1; i<=n; i++) {
		auto it=s.lower_bound(pii(p[i].first.second, 0));
		if(it->second) {
			cout<<p[i].second<<" ";
			cout<<it->second<<endl;
			return 0;
		}
		s.insert(pii(p[i].first.second, p[i].second));
	}
	puts("-1 -1");

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using pii=pair<int, int>;
using pi3=pair<pii, int>;

const int NN = 303030;

pi3 p[NN];
set<pii> s;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; cin>>n;
	for(int i=1; i<=n; i++) {
		scanf("%d %d", &p[i].first.first, &p[i].first.second);
		p[i].second=i;
	}
	sort(p+1, p+n+1);
	s.insert(pii(0, 0));
	s.insert(pii(2e9, 0));
	for(int i=1; i<=n; i++) {
		auto it=s.lower_bound(pii(p[i].first.second, 0));
//		it--;
		if(it->second) {
			cout<<p[i].second<<" ";
			cout<<it->second<<endl;
			return 0;
		}
		s.insert(pii(p[i].first.second, p[i].second));
	}
	puts("-1 -1");

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

using INT = long long;

const int NN = 303030;

int L[NN], R[NN];
int id[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n; cin >> n;
	for(int i=1; i<=n; i++) {
		scanf("%d %d", L+i, R+i);
		id[i] = i;
	}
	sort(id+1, id+n+1, [&] (int u, int v) {
		return L[u] < L[v];
	});
	for(int i=1; i<n; i++) {
		if(R[id[i]] >= R[id[i+1]]) {
			cout << id[i+1] << ' ' << id[i] << endl;
			return 0;
		}
	}
	cout << -1 << ' ' << -1 << endl;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
const int NN=300300;
pii p[NN];
int id[NN];
bool cmp(int u,int v){
	if(p[u].x==p[v].x) return p[u].y>p[v].y;
	return p[u].x<p[v].x;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y),id[i]=i;
	sort(id+1,id+n+1,cmp);
	for(int i=1;i<n;i++){
		int u=id[i],v=id[i+1];
		if(p[u].y>=p[v].y){
			cout<<v<<' '<<u<<endl;
			return 0;
		}
	}
	cout<<-1<<' '<<-1<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
#define NN 300010
int l[NN], r[NN];
int n;
int L[NN], R[NN];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> n;
	for(int i=1; i<=n; i++) cin >> l[i] >> r[i];
	for(int i=1; i<=n; i++) L[i] = l[i], R[i] = r[i];
	sort(L+1, L+n+1);
	sort(R+1, R+n+1);
	int ID = -1, JD = -1;
	for(int i=1; i<=n; i++) {
		int cnt = 0;
		cnt =n - (lower_bound(L+1, L+n+1, r[i]) - L);
		cnt += lower_bound(R+1, R+n+1, l[i]) - R;
		if(n - cnt > 1) {ID = i; break;}
	}

	for(int i=1; i<=n; i++) {
		if(i == ID) continue;
		if(l[ID] <= l[i] and r[ID] >= r[i]) {JD = i; break;}
	}

	if(ID == -1 or JD == -1) return puts("-1 -1");
	cout << JD << " " << ID << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = 300005;

struct Segment {
    int index, first, second;
    bool inline operator < (const Segment &rhs) const {
        return (first < rhs.first || (first == rhs.first && second >= rhs.second));
    }
};

int main() {
    int n;
    Segment a[MAX];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a[i].first, &a[i].second);
        a[i].index = i + 1;
    }
    sort(a, a + n);
    for (int i = 1; i < n; i++)
        if (a[i - 1].second >= a[i].second) {
            printf("%d %d", a[i].index, a[i - 1].index);
            return 0;
        }
    printf("-1 -1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define FastIO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define FileIO freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)

#define Rep(var, a, b) for (int var = a; var <= b; var++)
#define For(var, a, b) for (int var = a; var < b; var++)
#define Forr(var, a, b) for (int var = a; var >= b; var--)
#define Foreach(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)

#define db(x) { cout << #x << " = " << x << endl; }

#define pii pair<ll, int>
#define pll pair<int, int>
#define mk(x, y) make_pair(x, y)

const ll MAX = 3 * 1e5 + 10;
const ll INF = 3 * 1e18 + 10;

int main() {
    FastIO;
    // FileIO;
    int n, l, r;
    pair<int, int> a[MAX];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        a[i] = pair<int, int>(l, r);
    }
    sort(a, a + n);
    int j = 0;
    int i = 1;
    while (i < n) {
        if (a[j].second >= a[i].second) {
            cout << i + 1 << " " << j + 1;
            return 0;
        } else {
            j = i;
            i++;
        }
    }
    cout << "-1 -1";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = 300005;

struct Segment {
    int index, first, second;
    bool inline operator < (const Segment &rhs) const {
        return ((first == rhs.first && second >= rhs.second) || first < rhs.first);
    }
};

Segment a[MAX];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a[i].first, &a[i].second);
        a[i].index = i + 1;
    }
    sort(a, a + n);
    for (int i = 1; i < n; i++)
        if (a[i - 1].second >= a[i].second) {
            printf("%d %d", a[i].index, a[i - 1].index);
            return 0;
        }
    printf("-1 -1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = 300005;

struct Segment {
    int index, first, second;
    bool operator < (const Segment &rhs) const {
        return ((first == rhs.first && second > rhs.second) || first < rhs.first);
    }
};

Segment a[MAX];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a[i].first, &a[i].second);
        a[i].index = i + 1;
    }
    sort(a, a + n);
    for (int i = 1; i < n; i++)
        if (a[i - 1].second >= a[i].second) {
            printf("%d %d", a[i].index, a[i - 1].index);
            return 0;
        }
    printf("-1 -1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = 300005;

struct Segment {
    int index, first, second;
    bool inline operator < (const Segment &rhs) const {
        return (first < rhs.first || (first == rhs.first && second >= rhs.second));
    }
};

Segment a[MAX];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &a[i].first, &a[i].second);
        a[i].index = i + 1;
    }
    sort(a, a + n);
    for (int i = 1; i < n; i++)
        if (a[i - 1].second >= a[i].second) {
            printf("%d %d", a[i].index, a[i - 1].index);
            return 0;
        }
    printf("-1 -1");
    return 0;
}