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

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

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

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

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

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

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

}

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

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

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

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


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

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

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

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

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

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

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

}

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

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

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

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


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

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

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

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

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

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

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

}

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

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

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

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

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

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


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

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

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

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

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

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

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

}

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

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

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        l1 left[n][m] , right[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = 1e15;
                right[i][j] = 1e15;
            }
        }
        l1 temp = 1e15;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e15;
            while(c--)
            {
                for(int j=0;j<m;j++)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    left[i][j] = min(left[i][j],temp);
                }
            }
        }

        temp = 1e15;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e15;
            while(c--)
            {
                for(int j=m-1;j>=0;j--)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    right[i][j] = min(right[i][j],temp);
                }
            }
        }
        l1 ans = 1e15;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = min(left[i][j],right[i][j]);
            }
        }
        for(int j=0;j<m;j++)
        {
            l1 sum=0;
            for(int i=0;i<n;i++) sum+=left[i][j];
            ans = min(ans,sum);
        }
        if(ans==1e15) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


// Created by ...
#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'

#define vll vector< ll >
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i,a,b) for(ll i = a; i <= b; ++i)
#define fo(i,a,b) for(int i = a; i<= b; ++i)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define all(a) a.begin(),a.end()
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

using namespace std;
using ll = long long;
ll md=MD;

ll exp(ll a,ll b){ll r=1ll;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b>>=1;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll Min(ll a,ll b){if(a<b)return a; return b;}
ll Max(ll a,ll b){if(a>b)return a; return b;}

int find1(vll& v,int x)
{
	int low=0,high=v.size()-1,ans=-1,mid;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(v[mid]>=x)
		{
			ans=mid;
			high=mid-1;
		}
		else low=mid+1;
	}
	return ans;
}
int find2(vll& v,int x)
{
	int low=0,high=v.size()-1,ans=-1,mid;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(v[mid]<=x)
		{
			ans=mid;
			low=mid+1;
		}
		else high=mid-1;
	}
	return ans;
}
int32_t main() {
    IOS;

        ll n,m;
        cin>>n>>m;
        vec(string) v(n);
        rep(i,0,n-1)cin>>v[i];
        vec(vll) row(n);
        rep(i,0,n-1)
        {
        	rep(j,0,m-1)if(v[i][j]=='1')row[i].pb(j);
        	if(row[i].empty()){cout<<"-1";return 0;}
        }
        int ans=inf;
        rep(j,0,m-1)
        {
        	int tans=0;
        	rep(i,0,n-1)
        	{
        		int minx=inf;
        		int xr=find1(row[i],j);
        		int xl=find2(row[i],j);
        		 int tr,tl;
        		if(xr==-1)tr=m-j+row[i][0];else tr=row[i][xr]-j;
        		if(xl==-1)tl=j+m-row[i].back(); else tl=j-row[i][xl];
        		//db2(tl,tr);
        		minx=Min(tl,tr);
        		tans+=minx;
        	}
        	//db1(tans);
        	ans=Min(ans,tans);
        }
        cout<<ans;

        cout<<'\n';
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        vector <int> cnt(M + 1);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] >= maxSum) {
                                        maxSum = sums[j];
                                        cnt[maxSum]++;
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = 0;
        if(cnt[maxSum] > 1) {
                int middle = M / 2;
                if(sums[middle] == maxSum) {
                        coll = middle;
                } else {
                        int left = middle - 1, right = middle + 1;
                        while(1) {
                                if(left >= 0) {
                                        if(sums[left] == maxSum) {
                                                coll = left;
                                                break;
                                        }
                                        left--;
                                }
                                if(right < M) {
                                        if(sums[right] == maxSum) {
                                                coll = right;
                                                break;
                                        }
                                        right++;
                                }
                        }
                }
        }
        for(int i = 0; i < N; i++) {
                if(matrix[i][coll] != '1') {
                        int left = coll - 1, right = coll + 1;
                        while(left >= 0 || right < M) {
                                if(left >= 0) {
                                        if(matrix[i][left] == '1') {
                                                ans += coll - left;
                                                break;
                                        }
                                        left--;
                                }
                                if(right < M) {
                                        if(matrix[i][right] == '1') {
                                                ans += right - coll;
                                                break;
                                        }
                                        right++;
                                }
                        }
                }
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] > maxSum) {
                                        maxSum = sums[j];
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = 0;
        for(int i = 0; i < N; i++) {
                if(matrix[i][coll] != '1') {
                        int left = coll - 1, right = coll + 1;
                        while(left >= 0 || right < M) {
                                if(left >= 0) {
                                        if(matrix[i][left] == '1') {
                                                ans += coll - left;
                                                break;
                                        }
                                        left--;
                                }
                                if(right < M) {
                                        if(matrix[i][right] == '1') {
                                                ans += right - coll;
                                                break;
                                        }
                                        right++;
                                }
                        }
                }
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int findDist (int index, int M, int curr) {
        return min(abs(curr - index), (curr > M / 2 ? M - curr + index : curr + M - index));
}

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M;
        scanf("%d%d", &N, &M);
        vector < vector <char> > matrix(N);
        vector < vector <int> > indices(N);
        bool bad = false;
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        if(matrix[i][j] == '1') {
                                indices[i].push_back(j);
                                was = true;
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = INT_MAX;
        for(int i = 0; i < M; i++) {
                int currAns = 0;
                for(int j = 0; j < N; j++) {
                        if(matrix[j][i] == '1') {
                                continue;
                        }
                        int sz = (int) indices[j].size();
                        if(i < indices[j][0] || i > indices[j][sz - 1]) {
                                currAns += min(findDist(indices[j][0], M, i), findDist(indices[j][sz - 1], M, i));
                                continue;
                        }
                        int left = 0, right = sz - 1;
                        while(left <= right) {
                                int middle = (left + right) / 2;
                                if(indices[j][middle] < i && indices[j][middle + 1] > i) {
                                        currAns += min(i - indices[j][middle], indices[j][middle + 1] - i);
                                        break;
                                }
                                if(indices[j][middle] < i) {
                                        left = middle + 1;
                                } else {
                                        right = middle - 1;
                                }
                        }
                }
                ans = min(ans, currAns);
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        vector <int> cnt(N + 1);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] >= maxSum) {
                                        maxSum = sums[j];
                                        cnt[maxSum]++;
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = INT_MAX, total = cnt[maxSum], tryingColl = 0;
        if(total > 1) {
                vector <int> indices;
                for(int j = 0; j < M; j++) {
                        if(sums[j] == maxSum) {
                                indices.push_back(j);
                        }
                }
                tryingColl = indices[total / 2];
        }
        for(int rep = max(0, tryingColl - 2); rep < min(total, tryingColl + 2); rep++) {
                coll = rep;
                int currAns = 0;
                for(int i = 0; i < N; i++) {
                        if(matrix[i][coll] != '1') {
                                int left = coll - 1, right = coll + 1;
                                while(left >= 0 || right < M) {
                                        if(left >= 0) {
                                                if(matrix[i][left] == '1') {
                                                        currAns += min(coll - left, M - coll + left);
                                                        break;
                                                }
                                                left--;
                                        }
                                        if(right < M) {
                                                if(matrix[i][right] == '1') {
                                                        currAns += min(right - coll, coll + M - right);
                                                        break;
                                                }
                                                right++;
                                        }
                                }
                        }
                }
                ans = min(ans, currAns);
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        vector <int> cnt(N + 1);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] >= maxSum) {
                                        maxSum = sums[j];
                                        cnt[maxSum]++;
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = INT_MAX, total = cnt[maxSum], tryingColl = 0;
        if(total > 1) {
                vector <int> indices;
                for(int j = 0; j < M; j++) {
                        if(sums[j] == maxSum) {
                                indices.push_back(j);
                        }
                }
                tryingColl = indices[total / 2];
        }
        for(int rep = max(0, tryingColl - 5); rep < min(total, tryingColl + 5); rep++) {
                coll = rep;
                int currAns = 0;
                for(int i = 0; i < N; i++) {
                        if(matrix[i][coll] != '1') {
                                int left = coll - 1, right = coll + 1;
                                while(left >= 0 || right < M) {
                                        if(left >= 0) {
                                                if(matrix[i][left] == '1') {
                                                        currAns += coll - left;
                                                        break;
                                                }
                                                left--;
                                        }
                                        if(right < M) {
                                                if(matrix[i][right] == '1') {
                                                        currAns += right - coll;
                                                        break;
                                                }
                                                right++;
                                        }
                                }
                        }
                }
                ans = min(ans, currAns);
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        vector <int> cnt(N + 1);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] >= maxSum) {
                                        maxSum = sums[j];
                                        cnt[maxSum]++;
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = INT_MAX, total = cnt[maxSum], index = coll;
        vector <int> indices;
        if(total > 1) {
                for(int j = 0; j < M; j++) {
                        if(sums[j] == maxSum) {
                                indices.push_back(j);
                        }
                }
                index = total / 2;
        }
        for(int rep = (total > 1 ? max(0, index - 2) : index); rep < (total > 1 ? min(total, index + 2) : index + 1); rep++) {
                coll = total > 1 ? indices[rep] : rep;
                int currAns = 0;
                for(int i = 0; i < N; i++) {
                        if(matrix[i][coll] != '1') {
                                int left = coll - 1, right = coll + 1;
                                while(left >= 0 || right < M) {
                                        if(left >= 0) {
                                                if(matrix[i][left] == '1') {
                                                        currAns += min(coll - left, M - coll + left);
                                                        break;
                                                }
                                                left--;
                                        }
                                        if(right < M) {
                                                if(matrix[i][right] == '1') {
                                                        currAns += min(right - coll, coll + M - right);
                                                        break;
                                                }
                                                right++;
                                        }
                                }
                        }
                }
                ans = min(ans, currAns);
        }
        printf("%d\n", ans);
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
//        freopen("input.txt", "r", stdin);
//        freopen("output.txt", "w", stdout);
        int N, M, coll = 0, maxSum = -1;
        bool bad = false;
        scanf("%d%d", &N, &M);
        vector <int> sums(M);
        vector < vector < char > > matrix(N);
        vector <int> cnt(N + 1);
        for(int i = 0; i < N; i++) {
                bool was = false;
                matrix[i].resize(M);
                for(int j = 0; j < M; j++) {
                        scanf(" %c", &matrix[i][j]);
                        char curr = matrix[i][j];
                        if(curr == '1') {
                                was = true;
                                sums[j]++;
                                if(sums[j] >= maxSum) {
                                        maxSum = sums[j];
                                        cnt[maxSum]++;
                                        coll = j;
                                }
                        }
                }
                if(!was) {
                        bad = true;
                }
        }
        if(bad) {
                printf("-1\n");
                return 0;
        }
        int ans = 0;
        if(cnt[maxSum] > 1) {
                int middle = M / 2;
                if(sums[middle] == maxSum) {
                        coll = middle;
                } else {
                        int left = middle - 1, right = middle + 1;
                        while(1) {
                                if(left >= 0) {
                                        if(sums[left] == maxSum) {
                                                coll = left;
                                                break;
                                        }
                                        left--;
                                }
                                if(right < M) {
                                        if(sums[right] == maxSum) {
                                                coll = right;
                                                break;
                                        }
                                        right++;
                                }
                        }
                }
        }
        for(int i = 0; i < N; i++) {
                if(matrix[i][coll] != '1') {
                        int left = coll - 1, right = coll + 1;
                        while(left >= 0 || right < M) {
                                if(left >= 0) {
                                        if(matrix[i][left] == '1') {
                                                ans += coll - left;
                                                break;
                                        }
                                        left--;
                                }
                                if(right < M) {
                                        if(matrix[i][right] == '1') {
                                                ans += right - coll;
                                                break;
                                        }
                                        right++;
                                }
                        }
                }
        }
        printf("%d\n", ans);
        return 0;
}


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

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

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

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

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

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

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

}

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

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

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int left[n][m] , right[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = 1e15;
                right[i][j] = 1e15;
            }
        }
        int temp = 1e15;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e15;
            while(c--)
            {
                for(int j=0;j<m;j++)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    left[i][j] = min(left[i][j],temp);
                }
            }
        }

        temp = 1e15;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e15;
            while(c--)
            {
                for(int j=m-1;j>=0;j--)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    right[i][j] = min(right[i][j],temp);
                }
            }
        }
        l1 ans = 1e15;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = min(left[i][j],right[i][j]);
            }
        }
        for(int j=0;j<m;j++)
        {
            l1 sum=0;
            for(int i=0;i<n;i++) sum+=left[i][j];
            ans = min(ans,sum);
        }
        if(ans==1e15) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


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

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

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

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

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

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

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

}

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

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

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int left[n][m] , right[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = 1e9;
                right[i][j] = 1e9;
            }
        }
        int temp = 1e9;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e9;
            while(c--)
            {
                for(int j=0;j<m;j++)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    left[i][j] = min(left[i][j],temp);
                }
            }
        }

        temp = 1e9;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e9;
            while(c--)
            {
                for(int j=m-1;j>=0;j--)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    right[i][j] = min(right[i][j],temp);
                }
            }
        }
        int ans = 1e9;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = min(left[i][j],right[i][j]);
            }
        }
        for(int j=0;j<m;j++)
        {
            int sum=0;
            for(int i=0;i<n;i++) sum+=left[i][j];
            ans = min(ans,sum);
        }
        if(ans==1e9) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


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

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

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

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

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

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

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

}

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

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

int main()
{
    FIO;
    int t;
    t = 1;
    //cin>>t;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    for(int tc=0;tc<t;tc++)
    {
        int n,m;
        cin>>n>>m;
        vector<string> arr(n);
        for(int i1=0;i1<n;i1++) cin>>arr[i1];
        int left[n][m] , right[n][m];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = 1e9;
                right[i][j] = 1e9;
            }
        }
        int temp = 1e9;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e9;
            while(c--)
            {
                for(int j=0;j<m;j++)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    left[i][j] = min(left[i][j],temp);
                }
            }
        }

        temp = 1e9;
        for(int i=0;i<n;i++)
        {
            int c = 2;
            temp= 1e9;
            while(c--)
            {
                for(int j=m-1;j>=0;j--)
                {
                    if(arr[i][j]=='1') temp = 0;
                    else temp++;
                    right[i][j] = min(right[i][j],temp);
                }
            }
        }
        l1 ans = 1e15;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                left[i][j] = min(left[i][j],right[i][j]);
            }
        }
        for(int j=0;j<m;j++)
        {
            l1 sum=0;
            for(int i=0;i<n;i++) sum+=left[i][j];
            ans = min(ans,sum);
        }
        if(ans==1e15) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}


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

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

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

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

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

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

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

}

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

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

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

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


#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;

// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>

vector <ll> v[105];
//ll l[102][10004],r[102][10004];

ll fep(ll x, ll y, ll mod)
{ll res=1;while(y>0){if(y&1)res=(res*x)%mod;
    y=y>>1;x=(x*x)%mod;}return res%mod;}

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

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

	ll n,i,m,j,k,x,y,a,b,t;
	string s[105];

	cin >> n >> m;
	for(i=0;i<n;i++)
		cin >> s[i];
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
			if(s[i][j]=='1')
			{
				v[i].pb(j);
			}
		if(v[i].empty())
		{
			cout << -1;
			return 0;
		}
	}
	ll ans=1000000000000;
	for(j=0;j<m;j++)
	{
		ll sm=0;
		for(i=0;i<n;i++)
			if(s[i][j]=='0')
			{
				auto it=lower_bound(v[i].begin(),v[i].end(),j);
				if(it==v[i].end())
				{
					it--;
					sm+=min(*(v[i].begin())+(m-j),j-*it);
				}
				else if(it==v[i].begin())
				{
					sm+=min(*(v[i].begin())-j,j+m-v[i].back());
				}
				else
				{
					a=*it-j;
					it--;
					b=j-*it;
					sm+=min(a,b);
				}
			}
		ans=min(ans,sm);
	}
	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long m,x,n,i,j,k,l,d[105][10005],righ,lef;
string s;
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(k=0;k<n;k++)
	{
		cin>>s;
		vector<long long>v;
		for(j=0;j<m;j++)	if(s[j]=='1') v.push_back(j);
		if(v.size()==0) return cout<<"-1",0;
		lef=v[v.size()-1],righ=0;
		for(i=0;i<m;i++)
		{
			if(s[i]=='0') d[k][i]=min(min(abs(lef-i),m-abs(lef-i)),min(abs(v[righ]-i),m-abs(v[righ]-i)));
			else if(i==v[righ]) d[k][i]=0,righ=(righ+1)%v.size(),lef=i;
			else if(s[i]=='1') lef=i,d[k][i]=0;
		}
	}
	for(i=0;i<m;i++)
	{
		for(j=1;j<n;j++)
		{
			d[j][i]=d[j-1][i]+d[j][i];
		}
	}
	cout<<*min_element(d[n-1],d[n-1]+m);
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <random>
#include <iomanip>
#include <bitset>

using namespace std;

template<typename T> void uin(T &a, T b) {
    if (b < a) {
        a = b;
    }
}

template<typename T> void uax(T &a, T b) {
    if (b > a) {
        a = b;
    }
}

#define ghost signed
#define left left228
#define right right228
#define prev prev228
#define list list228
#define next next228

/*const int INF = 2e9 + 228;

const int N = 5005;

int dp[N][N], mn[N][N], pref[N], h[N];*/

const int N = 10005;

int n, m;

char a[105][N];
int mn[105][N];

int dist(int a, int b) {
	return min((a - b + m) % m, (b - a + m) % m);
}

ghost main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	cin >> n >> m;
	bool bad = 0;
	for (int i = 1; i <= n; ++i) {
		int sum = 0;
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j];
			sum += (a[i][j] - '0');
		}
		if (sum == 0) {
			bad = 1;
		}
	}
	if (bad) {
		cout << -1 << '\n';
		return 0;
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) mn[i][j] = 1000000;
	}
	int res = 1000000;
	for (int row = 1; row <= n; ++row) {
		int lm = 0, rm = 0;
		for (int i = 1; i <= m; ++i) {
			if (a[row][i] == '1') {
				if (!lm) lm = i;
				rm = i;
			}
		}
		int last = lm;
		for (int i = 1; i <= m; ++i) {
			if (a[row][i] == '1') last = i;
			uin(mn[row][i], dist(lm, i));
			uin(mn[row][i], dist(rm, i));
			uin(mn[row][i], dist(last, i));
		}
		last = rm;
		for (int i = m; i >= 1; --i) {
			if (a[row][i] == '1') last = i;
			uin(mn[row][i], dist(last, i));
		}
	}
	for (int col = 1; col <= m; ++col) {
		int cur = 0;
		for (int i = 1; i <= n; ++i) {
			cur += mn[i][col];
		}
		uin(res, cur);
	}
	cout << res << '\n';
	/*
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> h[i], pref[i] = pref[i - 1] + h[i];
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			mn[i][j] = INF;
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) dp[i][j] = INF;
	}
	for (int i = 1; i <= n; ++i) {
		dp[i][1] = pref[i];
	}
	for (int i = 1; i <= n; ++i) {
		int go = i;
		for (int j = 1; j <= i; ++j) {
			uin(mn[i][j], mn[i - 1][j]);
			uin(dp[i][j], mn[i][j]);
		}
		for (int j = i; j >= 1; --j) {
			if (dp[i][j] == INF) continue;
			while (go - 1 > 0 && pref[go] - pref[i] >= dp[i][j]) {
				--go;
			}
			while (go < n && pref[go] - pref[i] < dp[i][j]) ++go;
			if (pref[go] - pref[i] >= dp[i][j]) {
				uin(mn[go][j + 1], pref[go] - pref[i]);
			}
		}
	}
	for (int j = n; j >= 0; --j) {
		if (dp[n][j] != INF) {
			cout << n - j << '\n';
			return 0;
		}
	}*/
	return 0;
} // kek;


#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <random>
#include <iomanip>
#include <bitset>

using namespace std;

template<typename T> void uin(T &a, T b) {
    if (b < a) {
        a = b;
    }
}

template<typename T> void uax(T &a, T b) {
    if (b > a) {
        a = b;
    }
}

#define ghost signed
#define left left228
#define right right228
#define prev prev228
#define list list228
#define next next228

/*const int INF = 2e9 + 228;

const int N = 5005;

int dp[N][N], mn[N][N], pref[N], h[N];*/

const int N = 105;

char a[N][N];

ghost main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	int n, m;
	cin >> n >> m;
	bool bad = 0;
	for (int i = 1; i <= n; ++i) {
		int sum = 0;
		for (int j = 1; j <= m; ++j) {
			cin >> a[i][j];
			sum += (a[i][j] - '0');
		}
		if (sum == 0) {
			bad = 1;
		}
	}
	if (bad) {
		cout << -1 << '\n';
		return 0;
	}
	int res = 1000000;
	for (int col = 1; col <= m; ++col) {
		int cur = 0;
		for (int i = 1; i <= n; ++i) {
			int mn = n + n + n;
			for (int j = 1; j <= m; ++j) {
				if (a[i][j] == '1') {
					uin(mn, min((j - col + m) % m, (col - j + m) % m));
				}
			}
			cur += mn;
		}
		uin(res, cur);
	}
	cout << res << '\n';
	/*
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> h[i], pref[i] = pref[i - 1] + h[i];
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			mn[i][j] = INF;
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) dp[i][j] = INF;
	}
	for (int i = 1; i <= n; ++i) {
		dp[i][1] = pref[i];
	}
	for (int i = 1; i <= n; ++i) {
		int go = i;
		for (int j = 1; j <= i; ++j) {
			uin(mn[i][j], mn[i - 1][j]);
			uin(dp[i][j], mn[i][j]);
		}
		for (int j = i; j >= 1; --j) {
			if (dp[i][j] == INF) continue;
			while (go - 1 > 0 && pref[go] - pref[i] >= dp[i][j]) {
				--go;
			}
			while (go < n && pref[go] - pref[i] < dp[i][j]) ++go;
			if (pref[go] - pref[i] >= dp[i][j]) {
				uin(mn[go][j + 1], pref[go] - pref[i]);
			}
		}
	}
	for (int j = n; j >= 0; --j) {
		if (dp[n][j] != INF) {
			cout << n - j << '\n';
			return 0;
		}
	}*/
	return 0;
} // kek;


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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(),(n).end()
#define fi first
#define se second

typedef pair<int, int> PII;
typedef long long ll;

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);

	int n,m;
	cin >> n >> m;
	int a[101][10001],d[101][10001];
	vector<vector<int> > f(n+1,vector<int>(0));
	memset(d,0x3f,sizeof(d));
	memset(a,0,sizeof(a));
	bool bad = false;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			char c;
			cin >> c;
			if (c=='1') a[i][j]=1,f[i].pb(j);
		}
		if (f[i].size()==0) bad = true;
	}
	if (bad) {
		cout << -1 << "\n";
		return 0;
	}
	for (int i = 0; i < n; i++) {
		int k = f[i].size();
		for (int j = 0; j < k; j++) {
			d[i][f[i][j]]=0;
			for (int l = 1; l < m; l++) {
				if (a[i][(l+f[i][j])%m]) break;
				d[i][(l+f[i][j])%m]=min(d[i][(l+f[i][j])%m],l);
			}
			for (int l = 1; l < m; l++) {
				if (a[i][(f[i][j]-l+m)%m]) break;
				d[i][((f[i][j]-l)+m)%m]=min(d[i][((f[i][j]-l)+m)%m],l);
			}
		}
	}
	int ans = 2e9;
	for (int i = 0; i < m; i++) {
		int tot = 0;
		for (int j = 0; j < n; j++) tot+=d[j][i];
		ans = min(ans,tot);
	}
	cout << ans << "\n";
}


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

int main () {
	ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	// freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);

	int n,m;
	cin>>n>>m;
	vector<string>box(n);
	for(string &x:box) cin>>x;

	int arr[n][m];

	for(int i=0;i<n;i++){
		int left=0,right=n-1;
		while(left<m and box[i][left]=='0') left++;
		if(left==m) {
			cout<<-1;
			exit(0);
		}
		while(right>=0 and box[i][right]=='0') right--;
		right=-n;
		for(int j=0;j<m;j++) {
			if(box[i][j]=='1') right=j;
			arr[i][j]=j-right;
		}
		left+=m;
		for(int j=m-1;j>=0;j--){
			if(box[i][j]=='1') left=j;
			arr[i][j]=min(arr[i][j],left-j);
		}
	}

	long long ans=1e18;
	for(int j=0;j<m;j++){
		long long sum=0;
		for(int i=0;i<n;i++){
			sum+=arr[i][j];
		}
		ans=min(ans,sum);
	}
	cout<<ans;
}


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

int main () {
	ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	// freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);

	int n,m;
	cin>>n>>m;
	vector<string>box(n);
	for(string &x:box) cin>>x;

	int arr[n][m];

	for(int i=0;i<n;i++){
		int left=0,right=m-1;
		while(left<m and box[i][left]=='0') left++;
		if(left==m) {
			cout<<-1;
			exit(0);
		}
		while(right>=0 and box[i][right]=='0') right--;
		right-=m;
		for(int j=0;j<m;j++) {
			if(box[i][j]=='1') right=j;
			arr[i][j]=j-right;
		}
		left+=m;
		for(int j=m-1;j>=0;j--){
			if(box[i][j]=='1') left=j;
			arr[i][j]=min(arr[i][j],left-j);
		}
	}

	long long ans=1e18;
	for(int j=0;j<m;j++){
		long long sum=0;
		for(int i=0;i<n;i++){
			sum+=arr[i][j];
		}
		ans=min(ans,sum);
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>   //PBDS
#include <ext/pb_ds/tree_policy.hpp>       //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pi pair<int,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int,int>
#define PI   3.141592653589
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define f(i,x,n) for(int i=x;i<n;i++)
#define rf(i,x,n) for(int i=n-1;i>=x;i--)
#define check(x)   cerr<<#x<<" : "<<x<<endl;
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define fbo find_by_order                    //return iterator PBDS
#define ook order_of_key                     //return number :)) PBDS
#define baar exit(0)
using namespace __gnu_pbds;
using namespace std;
int powmod(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
signed main()
{
   fast
   mi mp1,mp2;
   int n;
   cin>>n;
   int flg=0,moves=LLONG_MAX;
   f(i,0,n)
   {
   	int x,y;
   	cin>>x>>y;
   	mp1[x]++;
   	mp1[y]++;
   	if(x==y) mp1[x]--;
	if(x!=y) mp2[y]--;
   }
   for(auto x:mp1)
   {
   	 if(x.second>=(int)ceil((double)n/2))
   	 {
   	    flg=1;
   	    int dif=x.second+mp2[x.fi];
   	    if(dif>=(int)ceil((double)n/2))
   	    {
   	   	 moves=0;
   	   	 break;
		}
		else
		{
			moves=min(moves,-mp2[x.fi]);
		}
	 }
   }
   if(!flg) cout<<-1;
   else cout<<moves;
   return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>   //PBDS
#include <ext/pb_ds/tree_policy.hpp>       //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pi pair<int,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int,int>
#define PI   3.141592653589
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define f(i,x,n) for(int i=x;i<n;i++)
#define rf(i,x,n) for(int i=n-1;i>=x;i--)
#define check(x)   cerr<<#x<<" : "<<x<<endl;
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define fbo find_by_order                    //return iterator PBDS
#define ook order_of_key                     //return number :)) PBDS
#define baar exit(0)
using namespace __gnu_pbds;
using namespace std;
int powmod(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
signed main()
{
   fast
   mi mp1,mp2;
   int n;
   cin>>n;
   int flg=0,moves=LLONG_MIN;
   f(i,0,n)
   {
   	int x,y;
   	cin>>x>>y;
   	mp1[x]++;
   	mp1[y]++;
   	if(x!=y)
   	mp2[y]--;
   	if(x==y) mp1[y]--;
   	if(mp1[x]>=(int)ceil((double)n/2))
   	{
   		flg=1;
   		if(mp2[x]>moves)
   		{
   			moves=mp2[x];
		}
	}
	if(mp1[y]>=(int)ceil((double)n/2))
	{
		flg=1;
		if(mp2[y]>moves)
		{
			moves=mp2[y];
		}
	}
   }
   if(!flg) cout<<-1;
   else cout<<-moves;
   return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>   //PBDS
#include <ext/pb_ds/tree_policy.hpp>       //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pi pair<int,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int,int>
#define PI   3.141592653589
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define f(i,x,n) for(int i=x;i<n;i++)
#define rf(i,x,n) for(int i=n-1;i>=x;i--)
#define check(x)   cerr<<#x<<" : "<<x<<endl;
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define fbo find_by_order                    //return iterator PBDS
#define ook order_of_key                     //return number :)) PBDS
#define baar exit(0)
using namespace __gnu_pbds;
using namespace std;
int powmod(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
signed main()
{
   fast
   mi mp1,mp2;
   int n;
   cin>>n;
   int flg=0,moves=LLONG_MAX;
   f(i,0,n)
   {
   	int x,y;
   	cin>>x>>y;
	 if(x==y) mp1[x]++;
	 else
	 {
	 	mp1[x]++;
	 	mp2[y]++;
	 }
   }
   int ttl=(int)ceil((double)n/2);
   for(auto x:mp1)
   {
   	 if(x.second>=ttl)
   	 {
   	    moves=0;
	 }
	 else
	 {
	 	if(mp2[x.fi]>=ttl-x.se)
	 	{
	 		moves=min(moves,ttl-x.se);
		 }
	 }
   }
   if(moves==LLONG_MAX) cout<<-1;
   else cout<<moves;
   return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>   //PBDS
#include <ext/pb_ds/tree_policy.hpp>       //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pi pair<int,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int,int>
#define PI   3.141592653589
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define f(i,x,n) for(int i=x;i<n;i++)
#define rf(i,x,n) for(int i=n-1;i>=x;i--)
#define check(x)   cerr<<#x<<" : "<<x<<endl;
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define fbo find_by_order                    //return iterator PBDS
#define ook order_of_key                     //return number :)) PBDS
#define baar exit(0)
using namespace __gnu_pbds;
using namespace std;
int powmod(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
signed main()
{
   fast
   mi mp1,mp2;
   int n;
   cin>>n;
   int flg=0,moves=INT_MAX;
   f(i,0,n)
   {
   	 int x,y;
   	 cin>>x>>y;
	 mp1[x]++;
	 mp2[y]++;
	 if(x==y) mp2[y]--;
   }
   int ttl=(int)ceil((double)n/2);
   for(auto x:mp1)
   {
   	 if(x.second>=ttl)
   	 {
   	    moves=0;
   	    cout<<moves;
   	    baar;
	 }
	 else
	 {
	 	if(x.se+mp2[x.fi]>=ttl)
	 	{
	 		moves=min(moves,min(mp2[x.fi],ttl-x.se));
		 }
	 }
   }
   if(moves==INT_MAX) cout<<-1;
   else cout<<moves;
   return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>   //PBDS
#include <ext/pb_ds/tree_policy.hpp>       //PBDS
#define int long long int
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pi pair<int,int>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mi map<int,int>
#define PI   3.141592653589
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define f(i,x,n) for(int i=x;i<n;i++)
#define rf(i,x,n) for(int i=n-1;i>=x;i--)
#define check(x)   cerr<<#x<<" : "<<x<<endl;
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define fbo find_by_order                    //return iterator PBDS
#define ook order_of_key                     //return number :)) PBDS
#define baar exit(0)
using namespace __gnu_pbds;
using namespace std;
int powmod(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent){
		if(exponent&1)ans=(ans*base)%mod;
		base=(base*base)%mod;
		exponent/=2;
	}
	return ans;
}
signed main()
{
   fast
   mi mp1,mp2;
   int n;
   cin>>n;
   int flg=0,moves=LLONG_MIN;
   f(i,0,n)
   {
   	int x,y;
   	cin>>x>>y;
   	mp1[x]++;
   	mp1[y]++;
   	mp2[y]--;
   	if(mp1[x]>=(int)ceil((double)n/2))
   	{
   		flg=1;
   		if(mp2[x]>moves)
   		{
   			moves=mp2[x];
		}
	}
	if(mp1[y]>=(int)ceil((double)n/2))
	{
		flg=1;
		if(mp2[y]>moves)
		{
			moves=mp2[y];
		}
	}
   }
   if(!flg) cout<<-1;
   else cout<<-moves;
   return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL_TEST
  #pragma comment(linker, "/stack:16777216")
#endif

using namespace std;

typedef long long int ll;
typedef vector <ll> vll;
typedef pair<ll, ll> pp;
typedef vector<pp > vpp;
typedef stack < pair<int, int> > sii;
typedef queue < ll > qll;

#define sz(a) (ll) ((a).size())
#define pb push_back
#define mp(a, b)       (make_pair(a, b) )
#define all(c)        (c).begin(),(c).end()
#define sortA(v)      (sort(v.begin(), v.end()))
#define sortD(v)      (sort(v.rbegin(), v.rend()))
#define fill(a)       (memset(a, 0, sizeof (a)))
#define fillA(a)       (memset(a, -1, sizeof (a)))
#define rep(i,n)     for(ll i=0;i<(n);++i)
#define repA(i,a,n) for(ll i=a;i<=(n);++i)
#define repD(i,a,n) for(ll i=a;i>=(n);--i)
#define pq(x)			    priority_queue<x,std::vector<x>,compare>
#define rpq(x)			  priority_queue<x,std::vector<x>,Rcompare>
const long double PI = 3.141592653589793238462643383;
#define X first
#define Y second

// Functions
struct Rcompare{bool operator()(pp lhs,pp rhs){return lhs.X>rhs.X;}};
struct compare{bool operator()(pp lhs,pp rhs){return lhs.X<rhs.X;}};
// clock_t tm=clock();void fin(){tm=clock()-tm;cerr<<(float)(tm)/CLOCKS_PER_SEC<<"\n";}
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

ll power(ll x,ll y)
{
  if(y==0)return 1;
  ll tmp=power(x,y/2);
  if(y%2==1)return ((tmp*tmp)*x);
  return (tmp*tmp);
}
const int MAX = 200009;
const int MOD = 1e9+7;
pp a[MAX];
map<ll,ll>mp;
ll cnt[MAX]={0},up[MAX]={0};
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifdef LOCAL_TEST
    ifstream cin("in.txt");ofstream cout("out.txt");//tm=clock();
    #endif

	ll n;cin>>n;
	ll m=Ceil(n,2);

	//cout<<m<<'\n';
	ll k=0;
	rep(i,n)
	{
		cin>>a[i].X>>a[i].Y;
		if(mp.find(a[i].X)==mp.end())mp[a[i].X]=k,k++;
		if(mp.find(a[i].Y)==mp.end())mp[a[i].Y]=k,k++;
		up[mp[a[i].X]]++;
		if(a[i].X==a[i].Y)cnt[mp[a[i].X]]++;
		else cnt[mp[a[i].X]]++,cnt[mp[a[i].Y]]++;
	}
	ll d[4],ii=0;
	rep(i,k)if(cnt[i]>=m)d[ii]=i,ii++;
	if(ii==0){cout<<-1;return 0;}
	ll mm=1e17;
	rep(i,ii)mm=min(mm,m-up[d[i]]);//cout<<d[i]<<' '<<up[d[i]]<<'\n';
	cout<<mm;

    return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL_TEST
  #pragma comment(linker, "/stack:16777216")
#endif

using namespace std;

typedef long long int ll;
typedef vector <ll> vll;
typedef pair<ll, ll> pp;
typedef vector<pp > vpp;
typedef stack < pair<int, int> > sii;
typedef queue < ll > qll;

#define sz(a) (ll) ((a).size())
#define pb push_back
#define mp(a, b)       (make_pair(a, b) )
#define all(c)        (c).begin(),(c).end()
#define sortA(v)      (sort(v.begin(), v.end()))
#define sortD(v)      (sort(v.rbegin(), v.rend()))
#define fill(a)       (memset(a, 0, sizeof (a)))
#define fillA(a)       (memset(a, -1, sizeof (a)))
#define rep(i,n)     for(ll i=0;i<(n);++i)
#define repA(i,a,n) for(ll i=a;i<=(n);++i)
#define repD(i,a,n) for(ll i=a;i>=(n);--i)
#define pq(x)			    priority_queue<x,std::vector<x>,compare>
#define rpq(x)			  priority_queue<x,std::vector<x>,Rcompare>
const long double PI = 3.141592653589793238462643383;
#define X first
#define Y second

// Functions
struct Rcompare{bool operator()(pp lhs,pp rhs){return lhs.X>rhs.X;}};
struct compare{bool operator()(pp lhs,pp rhs){return lhs.X<rhs.X;}};
// clock_t tm=clock();void fin(){tm=clock()-tm;cerr<<(float)(tm)/CLOCKS_PER_SEC<<"\n";}
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

ll power(ll x,ll y)
{
  if(y==0)return 1;
  ll tmp=power(x,y/2);
  if(y%2==1)return ((tmp*tmp)*x);
  return (tmp*tmp);
}
const int MAX = 200009;
const int MOD = 1e9+7;
pp a[MAX];
map<ll,ll>mp;
ll cnt[MAX]={0},up[MAX]={0};
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifdef LOCAL_TEST
    ifstream cin("in.txt");ofstream cout("out.txt");//tm=clock();
    #endif

	ll n;cin>>n;
	ll m=Ceil(n,2);

	//cout<<m<<'\n';
	ll k=0;
	rep(i,n)
	{
		cin>>a[i].X>>a[i].Y;
		if(mp.find(a[i].X)==mp.end())mp[a[i].X]=k,k++;
		if(mp.find(a[i].Y)==mp.end())mp[a[i].Y]=k,k++;
		up[mp[a[i].X]]++;
		if(a[i].X==a[i].Y)cnt[mp[a[i].X]]++;
		else cnt[mp[a[i].X]]++,cnt[mp[a[i].Y]]++;
	}
	ll d[4],ii=0;
	rep(i,k)if(cnt[i]>=m)d[ii]=i,ii++;
	if(ii==0){cout<<-1;return 0;}
	ll mm=1e17;
	rep(i,ii)mm=min(mm,m-up[d[i]]);//cout<<d[i]<<' '<<up[d[i]]<<'\n';
	cout<<max((ll)0,mm);

    return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL_TEST
  #pragma comment(linker, "/stack:16777216")
#endif

using namespace std;

typedef long long int ll;
typedef vector <ll> vll;
typedef pair<ll, ll> pp;
typedef vector<pp > vpp;
typedef stack < pair<int, int> > sii;
typedef queue < ll > qll;

#define sz(a) (ll) ((a).size())
#define pb push_back
#define mp(a, b)       (make_pair(a, b) )
#define all(c)        (c).begin(),(c).end()
#define sortA(v)      (sort(v.begin(), v.end()))
#define sortD(v)      (sort(v.rbegin(), v.rend()))
#define fill(a)       (memset(a, 0, sizeof (a)))
#define fillA(a)       (memset(a, -1, sizeof (a)))
#define rep(i,n)     for(ll i=0;i<(n);++i)
#define repA(i,a,n) for(ll i=a;i<=(n);++i)
#define repD(i,a,n) for(ll i=a;i>=(n);--i)
#define pq(x)			    priority_queue<x,std::vector<x>,compare>
#define rpq(x)			  priority_queue<x,std::vector<x>,Rcompare>
const long double PI = 3.141592653589793238462643383;
#define X first
#define Y second

// Functions
struct Rcompare{bool operator()(pp lhs,pp rhs){return lhs.X>rhs.X;}};
struct compare{bool operator()(pp lhs,pp rhs){return lhs.X<rhs.X;}};
// clock_t tm=clock();void fin(){tm=clock()-tm;cerr<<(float)(tm)/CLOCKS_PER_SEC<<"\n";}
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

ll power(ll x,ll y)
{
  if(y==0)return 1;
  ll tmp=power(x,y/2);
  if(y%2==1)return ((tmp*tmp)*x);
  return (tmp*tmp);
}
const int MAX = 200009;
const int MOD = 1e9+7;
pp a[MAX];
map<ll,ll>mp;
ll cnt[MAX]={0},up[MAX]={0};
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifdef LOCAL_TEST
    ifstream cin("in.txt");ofstream cout("out.txt");//tm=clock();
    #endif

	ll n;cin>>n;
	ll m=Ceil(n,2);

	//cout<<m<<'\n';
	ll k=0;
	rep(i,n)
	{
		cin>>a[i].X>>a[i].Y;
		if(mp.find(a[i].X)==mp.end())mp[a[i].X]=k,k++;
		if(mp.find(a[i].Y)==mp.end())mp[a[i].Y]=k,k++;
		up[mp[a[i].X]]++;
		if(a[i].X==a[i].Y)cnt[mp[a[i].X]]++;
		else cnt[mp[a[i].X]]++,cnt[mp[a[i].Y]]++;
	}
	ll d[4],ii=0;
	rep(i,k)if(cnt[i]>=m)d[ii]=i,ii++;
	//cout<<ii<<'\n';
	if(ii==0){cout<<-1;return 0;}
	ll mm=1e17;
	rep(i,ii)mm=min(mm,m-up[d[i]]);//cout<<d[i]<<' '<<up[d[i]]<<'\n';
	cout<<mm;

    return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL_TEST
  #pragma comment(linker, "/stack:16777216")
#endif

using namespace std;

typedef long long int ll;
typedef vector <ll> vll;
typedef pair<ll, ll> pp;
typedef vector<pp > vpp;
typedef stack < pair<int, int> > sii;
typedef queue < ll > qll;

#define sz(a) (ll) ((a).size())
#define pb push_back
#define mp(a, b)       (make_pair(a, b) )
#define all(c)        (c).begin(),(c).end()
#define sortA(v)      (sort(v.begin(), v.end()))
#define sortD(v)      (sort(v.rbegin(), v.rend()))
#define fill(a)       (memset(a, 0, sizeof (a)))
#define fillA(a)       (memset(a, -1, sizeof (a)))
#define rep(i,n)     for(ll i=0;i<(n);++i)
#define repA(i,a,n) for(ll i=a;i<=(n);++i)
#define repD(i,a,n) for(ll i=a;i>=(n);--i)
#define pq(x)			    priority_queue<x,std::vector<x>,compare>
#define rpq(x)			  priority_queue<x,std::vector<x>,Rcompare>
const long double PI = 3.141592653589793238462643383;
#define X first
#define Y second

// Functions
struct Rcompare{bool operator()(pp lhs,pp rhs){return lhs.X>rhs.X;}};
struct compare{bool operator()(pp lhs,pp rhs){return lhs.X<rhs.X;}};
// clock_t tm=clock();void fin(){tm=clock()-tm;cerr<<(float)(tm)/CLOCKS_PER_SEC<<"\n";}
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

ll power(ll x,ll y)
{
  if(y==0)return 1;
  ll tmp=power(x,y/2);
  if(y%2==1)return ((tmp*tmp)*x);
  return (tmp*tmp);
}
const int MAX = 10009;
const int MOD = 1e9+7;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifdef LOCAL_TEST
    ifstream cin("in.txt");ofstream cout("out.txt");//tm=clock();
    #endif

	ll n;cin>>n;
	ll m=Ceil(n,2);
	//cout<<m<<'\n';
	pp a[MAX];
	map<ll,ll> mp,mp2;
	ll cnt[MAX]={0},up[MAX]={0},k=0;
	rep(i,n)
	{
		cin>>a[i].X>>a[i].Y;
		if(mp.find(a[i].X)==mp.end())mp[a[i].X]=k,mp2[k]=a[i].X,k++;
		if(mp.find(a[i].Y)==mp.end())mp[a[i].Y]=k,mp2[k]=a[i].Y,k++;
		up[mp[a[i].X]]++;
		if(a[i].X==a[i].Y)cnt[mp[a[i].X]]++;
		else cnt[mp[a[i].X]]++,cnt[mp[a[i].Y]]++;
	}
	ll d[4],ii=0;
	rep(i,k)if(cnt[i]>=m)d[ii]=i,ii++;
	//cout<<ii<<'\n';
	if(ii==0){cout<<-1;return 0;}
	ll mm=1e17;
	rep(i,ii)mm=min(mm,m-up[d[i]]);//cout<<d[i]<<' '<<up[d[i]]<<'\n';
	cout<<mm;

    return 0;
}