import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main{

    public static PrintWriter out;

    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {
                    e.printStackTrace();

                }

            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try{
                str = br.readLine();
            } catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        MyScanner s = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=s.nextInt();
        List<Long> list=new ArrayList<Long>();
        for(int i=0;i<n;i++) list.add(s.nextLong());//=s.nextInt();
        Collections.sort(list);
        long min=list.get(0)+1;
        int k=1;
        while(k<n){
            if(list.get(k)==list.get(k-1)){k++; continue;}
            if(list.contains(min)){min++; k++; continue;}
            else{list.set(k++,min++);}
        }
        out.println(list.get(n-1)+1);

        out.close();

    }
}


import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main{

    public static PrintWriter out;

    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {
                    e.printStackTrace();

                }

            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try{
                str = br.readLine();
            } catch (IOException e){
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) {
        MyScanner s = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=s.nextInt();
        List<Long> list=new ArrayList<Long>();
        for(int i=0;i<n;i++) list.add(s.nextLong());//=s.nextInt();
        Collections.sort(list);
        list.set(0,(long)1);
        long min=list.get(0)+1;
        int k=1;
        while(k<n){
            if(list.get(k)==list.get(k-1)){k++; continue;}
            if(list.contains(min)){min++; k++; continue;}
            else{list.set(k++,min++);}
        }
        out.println(list.get(n-1)+1);

        out.close();

    }
}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		int[] arr = new int[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		//quickSort(arr, 0, arr.length - 1);
		mergeSort(arr, 0, arr.length - 1);
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (arr[i] >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void mergeSort(int[] arr, int l, int r) {

		if (l >= r) {
			return;
		}
		int mid = (l + r) / 2;
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

	public static void merge(int[] arr, int l, int mid, int r) {
		int[] tempArr = new int[r - l + 1];
		int i = l;
		int j = mid + 1;
		int counter = 0;
		while (i <= mid && j <= r) {
			if (arr[i] <= arr[j]) {
				tempArr[counter] = arr[i];
				++i;
			} else {
				tempArr[counter] = arr[j];
				++j;
			}
			++counter;
		}

		while (i <= mid) {
			tempArr[counter] = arr[i];
			++i;
			++counter;
		}
		while (j <= r) {
			tempArr[counter] = arr[j];
			++j;
			++counter;
		}
		for (counter = 0; counter < r - l + 1; ++counter) {
			arr[l + counter] = tempArr[counter];
		}

	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		Integer[] arr = new Integer[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		//quickSort(arr, 0, arr.length - 1);
		//mergeSort(arr, 0, arr.length - 1);
		//List<Integer> list = new ArrayList<>(Arrays.asList(arr));
		List<Integer> list = Arrays.asList(arr);
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer i1, Integer i2) {
				return i1 - i2;
			}
		});
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (arr[i] >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void mergeSort(int[] arr, int l, int r) {

		if (l >= r) {
			return;
		}
		int mid = (l + r) / 2;
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

	public static void merge(int[] arr, int l, int mid, int r) {
		int[] tempArr = new int[r - l + 1];
		int i = l;
		int j = mid + 1;
		int counter = 0;
		while (i <= mid && j <= r) {
			if (arr[i] <= arr[j]) {
				tempArr[counter] = arr[i];
				++i;
			} else {
				tempArr[counter] = arr[j];
				++j;
			}
			++counter;
		}

		while (i <= mid) {
			tempArr[counter] = arr[i];
			++i;
			++counter;
		}
		while (j <= r) {
			tempArr[counter] = arr[j];
			++j;
			++counter;
		}
		for (counter = 0; counter < r - l + 1; ++counter) {
			arr[l + counter] = tempArr[counter];
		}

	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		Integer[] arr = new Integer[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		//quickSort(arr, 0, arr.length - 1);
		//mergeSort(arr, 0, arr.length - 1);
		List<Integer> list = new ArrayList<>(Arrays.asList(arr));
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer i1, Integer i2) {
				return i1 - i2;
			}
		});
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (list.get(i) >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void mergeSort(int[] arr, int l, int r) {

		if (l >= r) {
			return;
		}
		int mid = (l + r) / 2;
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

	public static void merge(int[] arr, int l, int mid, int r) {
		int[] tempArr = new int[r - l + 1];
		int i = l;
		int j = mid + 1;
		int counter = 0;
		while (i <= mid && j <= r) {
			if (arr[i] <= arr[j]) {
				tempArr[counter] = arr[i];
				++i;
			} else {
				tempArr[counter] = arr[j];
				++j;
			}
			++counter;
		}

		while (i <= mid) {
			tempArr[counter] = arr[i];
			++i;
			++counter;
		}
		while (j <= r) {
			tempArr[counter] = arr[j];
			++j;
			++counter;
		}
		for (counter = 0; counter < r - l + 1; ++counter) {
			arr[l + counter] = tempArr[counter];
		}

	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		int[] arr = new int[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		quickSort(arr, 0, arr.length - 1);
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (arr[i] >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		Integer[] arr = new Integer[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		//quickSort(arr, 0, arr.length - 1);
		//mergeSort(arr, 0, arr.length - 1);
		//List<Integer> list = new ArrayList<>(Arrays.asList(arr));
		//List<Integer> list = Arrays.asList(arr);
		Arrays.sort(arr);
		/*
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer i1, Integer i2) {
				return i1 - i2;
			}
		});
		*/
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (arr[i] >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void mergeSort(int[] arr, int l, int r) {

		if (l >= r) {
			return;
		}
		int mid = (l + r) / 2;
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

	public static void merge(int[] arr, int l, int mid, int r) {
		int[] tempArr = new int[r - l + 1];
		int i = l;
		int j = mid + 1;
		int counter = 0;
		while (i <= mid && j <= r) {
			if (arr[i] <= arr[j]) {
				tempArr[counter] = arr[i];
				++i;
			} else {
				tempArr[counter] = arr[j];
				++j;
			}
			++counter;
		}

		while (i <= mid) {
			tempArr[counter] = arr[i];
			++i;
			++counter;
		}
		while (j <= r) {
			tempArr[counter] = arr[j];
			++j;
			++counter;
		}
		for (counter = 0; counter < r - l + 1; ++counter) {
			arr[l + counter] = tempArr[counter];
		}

	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(input.readLine());
		Integer[] arr = new Integer[n];
		String[] tempInput = input.readLine().split("\\s+");
		int i = 0;
		for (String s : tempInput) {
			arr[i] = Integer.parseInt(s);
			i++;
		}
		//quickSort(arr, 0, arr.length - 1);
		//mergeSort(arr, 0, arr.length - 1);
		List<Integer> list = new ArrayList<>(Arrays.asList(arr));
		list.sort(new Comparator<Integer>() {
			@Override
			public int compare(Integer i1, Integer i2) {
				return i1 - i2;
			}
		});
		int currentMin = 1;
		for (i = 0; i < n; i++) {
			if (arr[i] >= currentMin) {
				++currentMin;
			}
		}
		PrintWriter writer = new PrintWriter(System.out);
		writer.println(currentMin);
		//for(int j=0;j<n;j++)writer.println(arr[j]);
		writer.flush();
	}

	public static void mergeSort(int[] arr, int l, int r) {

		if (l >= r) {
			return;
		}
		int mid = (l + r) / 2;
		mergeSort(arr, l, mid);
		mergeSort(arr, mid + 1, r);
		merge(arr, l, mid, r);
	}

	public static void merge(int[] arr, int l, int mid, int r) {
		int[] tempArr = new int[r - l + 1];
		int i = l;
		int j = mid + 1;
		int counter = 0;
		while (i <= mid && j <= r) {
			if (arr[i] <= arr[j]) {
				tempArr[counter] = arr[i];
				++i;
			} else {
				tempArr[counter] = arr[j];
				++j;
			}
			++counter;
		}

		while (i <= mid) {
			tempArr[counter] = arr[i];
			++i;
			++counter;
		}
		while (j <= r) {
			tempArr[counter] = arr[j];
			++j;
			++counter;
		}
		for (counter = 0; counter < r - l + 1; ++counter) {
			arr[l + counter] = tempArr[counter];
		}

	}

	public static void quickSort(int[] arr, int l, int r) {
		if (l >= r) {
			return;
		}
		int mid = partition(arr, l, r);
		quickSort(arr, l, mid);
		quickSort(arr, mid + 1, r);
	}

	public static int partition(int[] arr, int l, int r) {
		int i = l - 1;
		int j = r + 1;
		int pivot = arr[l];
		while (true) {
			do {
				i++;
			} while (pivot > arr[i]);
			do {
				j--;
			} while (pivot < arr[j]);
			if (i >= j) {
				return j;
			}
			swap(arr, i, j);
		}
	}
	public static void swap(int[] arr, int pos1, int pos2) {
		int temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

}


#include <bits/stdc++.h>
#define nl '\n'
#define pb push_back
#define mp make_pair
#define pob pop_back
using namespace std;
const int MMAX = 10002;
char s[MMAX];
int col[MMAX];
int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );
    int n, m;
    bool ok = 1;
    cin >> n >> m;
    for ( int i = 1; i <= n; i++ )
        {
            int st[MMAX] = {0}, dr[MMAX] = {0};
            stack<int>S;
            int last = m + 1, first = m + 1;
            cin >> s;
            for ( int j = 0; j < m; j++ )
                {
                    if ( s[j] == '1' )
                        {
                            st[j] = j;
                            dr[j] = j;
                            while ( !S.empty() )
                                {
                                    dr[S.top()] = j;
                                    S.pop();
                                }
                            if ( first == m + 1 )
                                first = j;
                            last = j;
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    dr[S.top()] = first;
                    S.pop();
                }
            if ( first == m + 1 )
                ok = 0;
            for ( int j = m - 1; j >= 0; j-- )
                {
                    if ( s[j] == '1' )
                        {
                            while ( !S.empty() )
                                {
                                    st[S.top()] = j;
                                    S.pop();
                                }
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    st[S.top()] = last;
                    S.pop();
                }
            for ( int j = 0; j < m; j++ )
                {
                    int ddr;
                    if ( dr[j] < j )
                        {
                            ddr = m - j + dr[j];
                        }
                    else ddr = dr[j] - j;
                    int dst;
                    if ( st[j] > j )
                        {
                            dst = j + m - st[j];
                        }
                    else dst = j - st[j];
                    col[j] += min ( ddr, dst );
                }
        }
    if ( ok == 0 )
        cout << -1;
    else
        {
            int ans = MMAX;
            for ( int i = 0; i < m; i++ )
                ans = min ( ans, col[i] );
            cout << ans;
        }
    return 0;
}


#include <bits/stdc++.h>
#define nl '\n'
#define pb push_back
#define mp make_pair
#define pob pop_back
using namespace std;
const int MMAX = 10002;
char s[MMAX];
int col[MMAX];
int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );
    int n, m;
    bool ok = 1;
    cin >> n >> m;
    for ( int i = 1; i <= n; i++ )
        {
            int st[MMAX] = {0}, dr[MMAX] = {0};
            stack<int>S;
            int last = m + 1, first = m + 1;
            cin >> s;
            for ( int j = 0; j < m; j++ )
                {
                    if ( s[j] == '1' )
                        {
                            st[j] = j;
                            dr[j] = j;
                            while ( !S.empty() )
                                {
                                    dr[S.top()] = j;
                                    S.pop();
                                }
                            if ( first == m + 1 )
                                first = j;
                            last = j;
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    dr[S.top()] = first;
                    S.pop();
                }
            if ( first == m + 1 )
                ok = 0;
            for ( int j = m - 1; j >= 0; j-- )
                {
                    if ( s[j] == '1' )
                        {
                            while ( !S.empty() )
                                {
                                    st[S.top()] = j;
                                    S.pop();
                                }
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    st[S.top()] = last;
                    S.pop();
                }
            for ( int j = 0; j < m; j++ )
                {
                    int ddr;
                    if ( dr[j] < j )
                        {
                            ddr = m - j + dr[j];
                        }
                    else ddr = dr[j] - j;
                    int dst;
                    if ( st[j] > j )
                        {
                            dst = j + m - st[j];
                        }
                    else dst = j - st[j];
                    col[j] += max ( ddr, dst );
                }
        }
    if ( ok == 0 )
        cout << -1;
    else
        {
            int ans = MMAX;
            for ( int i = 0; i < m; i++ )
                ans = min ( ans, col[i] );
            cout << ans;
        }
    return 0;
}


#include <bits/stdc++.h>
#define nl '\n'
#define pb push_back
#define mp make_pair
#define pob pop_back
using namespace std;
const int MMAX = 10002;
char s[MMAX];
int col[MMAX];
int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );
    int n, m;
    bool ok = 1;
    cin >> n >> m;
    for ( int i = 1; i <= n; i++ )
        {
            int st[MMAX] = {0}, dr[MMAX] = {0};
            stack<int>S;
            int last = m + 1, first = m + 1;
            cin >> s;
            for ( int j = 0; j < m; j++ )
                {
                    if ( s[j] == '1' )
                        {
                            st[j] = j;
                            dr[j] = j;
                            while ( !S.empty() )
                                {
                                    dr[S.top()] = j;
                                    S.pop();
                                }
                            if ( first == m + 1 )
                                first = j;
                            last = j;
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    dr[S.top()] = first;
                    S.pop();
                }
            if ( first == m + 1 )
                ok = 0;
            for ( int j = m - 1; j >= 0; j-- )
                {
                    if ( s[j] == '1' )
                        {
                            while ( !S.empty() )
                                {
                                    st[S.top()] = j;
                                    S.pop();
                                }
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    st[S.top()] = last;
                    S.pop();
                }
            for ( int j = 0; j < m; j++ )
                {
                    int ddr;
                    if ( dr[j] < j )
                        {
                            ddr = m - j + dr[j];
                        }
                    else ddr = dr[j] - j;
                    int dst;
                    if ( st[j] > j )
                        {
                            dst = j + m - st[j];
                        }
                    else dst = j - st[j];
                    col[j] += min ( ddr, dst );
                }
            if ( !S.empty() )
                cout << 2;
        }
    if ( ok == 0 )
        cout << -1;
    else
        {
            int ans = MMAX;
            for ( int i = 0; i < m; i++ )
                ans = min ( ans, col[i] );
            cout << ans;
        }
    return 0;
}


#include <bits/stdc++.h>
#define nl '\n'
#define pb push_back
#define mp make_pair
#define pob pop_back
using namespace std;
const int MMAX = 10002;
char s[MMAX];
int col[MMAX];
int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );
    int n, m;
    bool ok = 1;
    cin >> n >> m;
    for ( int i = 1; i <= n; i++ )
        {
            int st[MMAX] = {0}, dr[MMAX] = {0};
            stack<int>S;
            int last = m + 1, first = m + 1;
            cin >> s;
            for ( int j = 0; j < m; j++ )
                {
                    if ( s[j] == '1' )
                        {
                            st[j] = j;
                            dr[j] = j;
                            while ( !S.empty() )
                                {
                                    dr[S.top()] = j;
                                    S.pop();
                                }
                            if ( first == m + 1 )
                                first = j;
                            last = j;
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    dr[S.top()] = first;
                    S.pop();
                }
            if ( first == m + 1 )
                ok = 0;
            for ( int j = m - 1; j >= 0; j-- )
                {
                    if ( s[j] == '1' )
                        {
                            while ( !S.empty() )
                                {
                                    st[S.top()] = j;
                                    S.pop();
                                }
                        }
                    else S.push ( j );
                }
            while ( !S.empty() )
                {
                    st[S.top()] = last;
                    S.pop();
                }
            for ( int j = 0; j < m; j++ )
                {
                    int ddr;
                    if ( dr[j] < j )
                        {
                            ddr = m - j + dr[j];
                        }
                    else ddr = dr[j] - j;
                    int dst;
                    if ( st[j] > j )
                        {
                            dst = j + m - st[j];
                        }
                    else dst = j - st[j];
                    col[j] += min ( ddr, dst );
                }
        }
    if ( ok == 0 )
        cout << -1;
    else
        {
            int ans = 100*MMAX;
            for ( int i = 0; i < m; i++ )
                ans = min ( ans, col[i] );
            cout << ans;
        }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <locale>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define mkp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = (int)1e9;
const int mod = (int)1e9+7;
const int MAX = (int)1e5+5;
void build(){
    return ;
}
int n, m;
char store[105][MAX];
void Init(){
    scanf("%d %d", &n, &m);
    rep(i, n) scanf("%s", store[i]);
    return ;
}
vector<int>check[105];
void sol(){
    rep(i, n){
        per1(j, m-1, 0) if(store[i][j] == '1'){
            check[i].pb(j-m); break;
        }
        rep(j, m){
            if(store[i][j] == '1') check[i].pb(j);
        }
        if((int)check[i].size() == 0){
            puts("-1"); return ;
        }
        check[i].pb(check[i][1] + m);
    }
    rep(i, n){
        for(int x : check[i]) printf("%d ", x);
        puts("");
    }
    LL res = 1e9;
    rep(i, m){
        LL cnt = 0;
        rep(j, n){
            auto it = lower_bound(ALL(check[j]), i);
            int v = *it - i;
            it--;
            v = min(v, i - *it);

            cnt += v;
        }
        res = min(res, cnt);
    }
    printf("%lld", res);
    return ;
}
int main(void){
    Init();
    build();
    sol();
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <locale>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define mkp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = (int)1e9;
const int mod = (int)1e9+7;
const int MAX = (int)1e5+5;
void build(){
    return ;
}
int n, m;
char store[105][MAX];
void Init(){
    scanf("%d %d", &n, &m);
    rep(i, n) scanf("%s", store[i]);
    return ;
}
vector<int>check[105];
void sol(){
    rep(i, n){
        per1(j, m-1, 0) if(store[i][j] == '1'){
            check[i].pb(j-m); break;
        }
        rep(j, m){
            if(store[i][j] == '1') check[i].pb(j);
        }
        if((int)check[i].size() == 0){
            puts("-1"); return ;
        }
        check[i].pb(check[i][1] + m);
    }
    LL res = 1e9;
    rep(i, m){
        LL cnt = 0;
        rep(j, n){
            auto it = lower_bound(ALL(check[j]), i);
            int v = min(*it - i, i - *(--it));
            cnt += v;
        }
        res = min(res, cnt);
    }
    printf("%lld", res);
    return ;
}
int main(void){
    Init();
    build();
    sol();
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <locale>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define mkp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int INF = (int)1e9;
const int mod = (int)1e9+7;
const int MAX = (int)1e5+5;
void build(){
    return ;
}
int n, m;
char store[105][MAX];
void Init(){
    scanf("%d %d", &n, &m);
    rep(i, n) scanf("%s", store[i]);
    return ;
}
vector<int>check[105];
void sol(){
    rep(i, n){
        per1(j, m-1, 0) if(store[i][j] == '1'){
            check[i].pb(j-m); break;
        }
        rep(j, m){
            if(store[i][j] == '1') check[i].pb(j);
        }
        if((int)check[i].size() == 0){
            puts("-1"); return ;
        }
        check[i].pb(check[i][1] + m);
    }
    LL res = 1e9;
    rep(i, m){
        LL cnt = 0;
        rep(j, n){
            auto it = lower_bound(ALL(check[j]), i);
            int v = *it - i;
            it--;
            v = min(v, i - *it);

            cnt += v;
        }
        res = min(res, cnt);
    }
    printf("%lld", res);
    return ;
}
int main(void){
    Init();
    build();
    sol();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<long long int,long long int> pa;
#define resize(A) A.resize(unique(A.begin(), A.end()) - A.begin())
//ios_base::sync_with_stdio(false);cin.tie(NULL);
/*ifstream cin("input.txt");
ofstream cout("output.txt");*/
#define pb push_back
#define  MAX 100009
#define ll long long int
long long int MOD=998244353;
long long int mod=1e9+7;
long long int INF=1e18;
ll dx[]={1,-1,-1,1};
ll dy[]={1,-1,1,-1};
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    vector<string> v(n);
    for(int i=0;i<n;i++)
        cin>>v[i];
    int a[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(v[i][j]=='1')
                a[i][j]=0;
            else
                a[i][j]=INT_MAX-4;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=1;j<m;j++)
        {
            a[i][j]=min(a[i][j-1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);
        for(int j=1;j<m;j++)
        {
            a[i][j]=min(a[i][j-1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);

        for(int j=m-2;j>=0;j--)
        {
            a[i][j]=min(a[i][j+1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);
        for(int j=m-2;j>=0;j--)
        {
            a[i][j]=min(a[i][j+1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);

    }
    ll ans=INF;
    for(int j=0;j<m;j++)
    {ll  temp=0;
        for(int i=0;i<n;i++)
            {
                if(a[i][j]!=INT_MAX-4)
                temp=temp+a[i][j];
                else
                {temp=INF;break;}
            }
            ans=min(ans,temp);
    }
    if(ans==INF)
        cout<<-1;
    else
        cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<long long int,long long int> pa;
#define resize(A) A.resize(unique(A.begin(), A.end()) - A.begin())
//ios_base::sync_with_stdio(false);cin.tie(NULL);
/*ifstream cin("input.txt");
ofstream cout("output.txt");*/
#define pb push_back
#define  MAX 100009
#define ll long long int
long long int MOD=998244353;
long long int mod=1e9+7;
long long int INF=1e18;
ll dx[]={1,-1,-1,1};
ll dy[]={1,-1,1,-1};
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    vector<string> v(n);
    for(int i=0;i<n;i++)
        cin>>v[i];
    int a[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(v[i][j]=='1')
                a[i][j]=0;
            else
                a[i][j]=INT_MAX-4;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=1;j<m;j++)
        {
            a[i][j]=min(a[i][j-1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);
    a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);

        for(int j=m-2;j>=0;j--)
        {
            a[i][j]=min(a[i][j+1]+1,a[i][j]);
        }
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);
        a[i][0]=min(a[i][m-1]+1,a[i][0]);
        a[i][m-1]=min(a[i][0]+1,a[i][m-1]);

    }
    ll ans=INF;
    for(int j=0;j<m;j++)
    {ll  temp=0;
        for(int i=0;i<n;i++)
            {
                if(a[i][j]!=INT_MAX-4)
                temp=temp+a[i][j];
                else
                {temp=INF;break;}
            }
            ans=min(ans,temp);
    }
    if(ans==INF)
        cout<<-1;
    else
        cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n, m, p[10005];

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

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

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        int first = 0, last = m - 1;
        while (first < m && s[first] != '1') first++;

        if (first == m) {
            cout << -1 << '\n';
            return 0;
        }

        while (last >= 0 && s[last] != '1') last--;

        int l[m + 1], r[m + 1], cur = -1;
        for (int i = 0; i < m; i++) {
            if (s[i] == '1') cur = i;
            if (cur == -1) l[i] = last;
            else l[i] = cur;
        }
        for (int i = m - 1; i >= 0; i--) {
            if (s[i] == '1') cur = i;
            if (cur == -1) r[i] = first;
            else r[i] = cur;
        }

        for (int i = 0; i < m; i++) {
            p[i] += min((i - l[i] + m) % m, (r[i] - i + m) % m);
        }
    }

    int ans = INT_MAX;
    for (int i = 0; i < m; i++) {
        ans = min(ans, p[i]);
    }

    cout << ans << '\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n, m, p[10005];

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

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

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        int first = 0, last = m - 1;
        while (first < m && s[first] != '1') first++;

        if (first == m) {
            cout << -1 << '\n';
            return 0;
        }

        while (last >= 0 && s[last] != '1') last--;

        int l[m + 1], r[m + 1], cur = -1;
        for (int i = 0; i < m; i++) {
            if (s[i] == '1') cur = i;
            if (cur == -1) l[i] = last;
            else l[i] = cur;
        }
        cur = -1;
        for (int i = m - 1; i >= 0; i--) {
            if (s[i] == '1') cur = i;
            if (cur == -1) r[i] = first;
            else r[i] = cur;
        }

        for (int i = 0; i < m; i++) {
            p[i] += min((i - l[i] + m) % m, (r[i] - i + m) % m);
        }
    }

    int ans = INT_MAX;
    for (int i = 0; i < m; i++) {
        ans = min(ans, p[i]);
    }

    cout << ans << '\n';

    return 0;
}


#include<bits/stdc++.h>

#define Imposter ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long int
#define mp make_pair
#define se second
#define fi first

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)

using namespace std;
#define f(n) for(ll i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define vll vector<ll>
#define vlll vector<vll>
#define all(x) x.begin(),x.end()

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

int main()
{
    Imposter

    int t=1;
    //cin>>t;
    while(t--)
    {

        int n,m;
        cin>>n>>m;
        string s[n];
        f(n) cin>>s[i];
        int arr[n][m];
        rep(i,0,n) rep(j,0,m) arr[i][j]=INT_MAX;
        for(int i=0;i<n;i++)
        {
            int pos=-1;
            for(int jk=0;jk<2*m;jk++)
            {
                int j=jk%m;
                if(s[i][j]=='1')
                {
                    arr[i][j]=0;
                    pos=jk;
                }
                else
                {
                    if(pos!=-1)
                    arr[i][j]=min(arr[i][j],jk-pos);
                }
            }
            pos=-1;
            for(int jk=2*m-1;jk>=0;jk--)
            {
                int j=jk%m;
                if(s[i][j]=='1')
                {
                    arr[i][j]=0;
                    pos=jk;
                }
                else
                {
                    if(pos!=-1)
                    arr[i][j]=min(arr[i][j],pos-jk);
                }
            }
        }
        ll ans=INT_MAX;
        for(int j=0;j<m;j++)
        {
            ll temp=0;
            for(int i=0;i<n;i++)
            {
                temp+=arr[i][j];
            }
            ans=min(ans,temp);
        }
        cout<<(ans==INT_MAX ? -1:ans)<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>

#define Imposter ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define ll long long int
#define mp make_pair
#define se second
#define fi first

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>//s.order_of_key(val) *s.find_by_order(ind)

using namespace std;
#define f(n) for(ll i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define vll vector<ll>
#define vlll vector<vll>
#define all(x) x.begin(),x.end()

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

int main()
{
    Imposter

    int t=1;
    //cin>>t;
    while(t--)
    {

        int n,m;
        cin>>n>>m;
        string s[n];
        f(n) cin>>s[i];
        int arr[n][m];
        rep(i,0,n) rep(j,0,m) arr[i][j]=INT_MAX;
        for(int i=0;i<n;i++)
        {
            int pos=-1;
            for(int jk=0;jk<2*m;jk++)
            {
                int j=jk%m;
                if(s[i][j]=='1')
                {
                    arr[i][j]=0;
                    pos=jk;
                }
                else
                {
                    if(pos!=-1)
                    arr[i][j]=min(arr[i][j],jk-pos);
                }
            }
            pos=-1;
            for(int jk=2*m-1;jk>=0;jk--)
            {
                int j=jk%m;
                if(s[i][j]=='1')
                {
                    arr[i][j]=0;
                    pos=jk;
                }
                else
                {
                    if(pos!=-1)
                    arr[i][j]=min(arr[i][j],pos-jk);
                }
            }
        }
        int ans=INT_MAX;
        for(int j=0;j<m;j++)
        {
            int temp=0;
            for(int i=0;i<n;i++)
            {
                temp+=arr[i][j];
            }
            ans=min(ans,temp);
        }
        cout<<(ans==INT_MAX ? -1:ans)<<endl;

    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define eps 1e-8

int f[10010], g[10010], n, m, cur, ans;
char s[10010];

int main() {
	memset(g, 0, sizeof(g));
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		memset(f, INF, sizeof(f));
		scanf("%s", s);
		cur = -1;
		for (int j = m-1; j >= 0 && cur == -1; j--)
			if (s[j] == '1') cur = j;
		if (cur == -1) {
			printf("-1\n");
			return 0;
		}
		for (int j = 0; j < m-1; j++) {
			if (s[j] == '1') cur = j;
			f[j] = min(f[j], (j+m-cur)%m);
		}
		cur = -1;
		for (int j = 0; j < m && cur == -1; j++)
			if (s[j] == '1') cur = j;
		for (int j = m-1; j >= 0; j--) {
			if (s[j] == '1') cur = j;
			f[j] = min(f[j], (cur+m-j)%m);
		}
		for (int j = 0; j < m; j++)
			g[j] += f[j];
	}
	ans = INF;
	for (int i = 0; i < m; i++)
		ans = min(ans, g[i]);
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define eps 1e-8

int f[1010], g[1010], n, m, cur, ans;
char s[1010];

int main() {
	memset(g, 0, sizeof(g));
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		memset(f, INF, sizeof(f));
		scanf("%s", s);
		cur = -1;
		for (int j = m-1; j >= 0 && cur == -1; j--)
			if (s[j] == '1') cur = j;
		if (cur == -1) {
			printf("-1\n");
			return 0;
		}
		for (int j = 0; j < m-1; j++) {
			if (s[j] == '1') cur = j;
			f[j] = min(f[j], (j+m-cur)%m);
		}
		cur = -1;
		for (int j = 0; j < m && cur == -1; j++)
			if (s[j] == '1') cur = j;
		for (int j = m-1; j >= 0; j--) {
			if (s[j] == '1') cur = j;
			f[j] = min(f[j], (cur+m-j)%m);
		}
		for (int j = 0; j < m; j++)
			g[j] += f[j];
	}
	ans = INF;
	for (int i = 0; i < m; i++)
		ans = min(ans, g[i]);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define forn(i,s,e) for(ll i = s;i<e;i++)
#define vv(x) vector<x >
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

int main(){
    ll n,m; cin >> n >> m  ;
    string arr[n];
    forn(i,0,n) cin >> arr[i];
    set<ll> pos[n];
    forn(i,0,n){
        forn(j,0,m){
            if(arr[i][j] == '1'){
                pos[i].insert(j);
            }
        }
    }
    forn(i,0,n){
        if(pos[i].size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }
    ll ans = INT_MAX;
    for(ll i = 0;i<m;i++){
        bool f = 1;
        ll temp = 0;
        forn(j,0,n){
            auto l1 = pos[j].lower_bound(i);
            ll length1 = INT_MAX, length2 = INT_MAX;
            if(l1 != pos[j].end()){
                length1= min((*l1-i),(m-(*l1-i)));
            }else{
                l1 = pos[j].begin();
                length1= min((max(*l1,i) - min(*l1,i)), m- (max(*l1,i) - min(*l1,i)));
            }
            if(l1 == pos[j].begin()){
                auto l2 = pos[j].rbegin();
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }else{
                auto l2 = --l1;
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }
            temp += (min(min(length1,length2),min(m-length1,m-length2)));
        }
        ans = min(ans,temp);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define ll long long int
#define forn(i,s,e) for(ll i = s;i<e;i++)
#define vv(x) vector<x >
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

int main(){
    ll n,m; cin >> n >> m  ;
    string arr[n];
    forn(i,0,n) cin >> arr[i];
    set<ll> pos[n];
    forn(i,0,n){
        forn(j,0,m){
            if(arr[i][j] == '1'){
                pos[i].insert(j);
            }
        }
    }
    forn(i,0,n){
        if(pos[i].size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }
    ll ans = INT_MAX;
    for(ll i = 0;i<m;i++){
        bool f = 1;
        ll temp = 0;
        forn(j,0,n){
            auto l1 = pos[j].lower_bound(i);
            ll length1 = INT_MAX, length2 = INT_MAX;
            if(l1 != pos[j].end()){
                length1= min((*l1-i),(m-(*l1-i)));
            }
            if(l1 == pos[j].begin()){
                auto l2 = pos[j].rbegin();
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }else{
                auto l2 = --l1;
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }
            temp += (min(min(length1,length2),min(m-length1,m-length2)));
        }
        ans = min(ans,temp);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define ll long long int
#define forn(i,s,e) for(ll i = s;i<e;i++)
#define vv(x) vector<x >
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

int main(){
    ll n,m; cin >> n >> m  ;
    string arr[n];
    forn(i,0,n) cin >> arr[i];
    set<ll> pos[n];
    forn(i,0,n){
        forn(j,0,m){
            if(arr[i][j] == '1'){
                pos[i].insert(j);
            }
        }
    }
    forn(i,0,n){
        if(pos[i].size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }
    ll ans = INT_MAX;
    for(ll i = 0;i<m;i++){
        bool f = 1;
        ll temp = 0;
        forn(j,0,n){
            auto l1 = pos[j].lower_bound(i);
            ll length1 = INT_MAX, length2 = INT_MAX;
            if(l1 != pos[j].end()){
                length1= (*l1-i);
            }
            if(l1 == pos[j].begin()){
                auto l2 = pos[j].rbegin();
                length2 = (max(*l2,i) - min(*l2,i));
            }else{
                auto l2 = --l1;
                length2 = (max(*l2,i)- min(*l2,i));
            }
            temp += (min(length1,length2));
        }
        ans = min(ans,temp);
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define ll long long int
#define forn(i,s,e) for(ll i = s;i<e;i++)
#define vv(x) vector<x >
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

int main(){
    ll n,m; cin >> n >> m  ;
    string arr[n];
    forn(i,0,n) cin >> arr[i];
    set<ll> pos[n];
    forn(i,0,n){
        forn(j,0,m){
            if(arr[i][j] == '1'){
                pos[i].insert(j);
            }
        }
    }
    forn(i,0,n){
        if(pos[i].size() == 0) {
            cout << -1 << endl;
            return 0;
        }
    }
    ll ans = INT_MAX;
    for(ll i = 0;i<m;i++){
        bool f = 1;
        ll temp = 0;
        forn(j,0,n){
            auto l1 = pos[j].lower_bound(i);
            ll length1 = INT_MAX, length2 = INT_MAX;
            if(l1 != pos[j].end()){
                length1= min((*l1-i),(m-(*l1-i)));
            }
            if(l1 == pos[j].begin()){
                auto l2 = pos[j].rbegin();
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }else{
                auto l2 = --l1;
                length2 = min((max(*l2,i) - min(*l2,i)), m- (max(*l2,i) - min(*l2,i)));
            }
            temp += (min(length1,length2));
        }
        ans = min(ans,temp);
    }
    cout << ans << endl;
}


#include<iostream>
using namespace std;
int mod=1e5;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
    fastio;
    long long int n,m,i,j,ans=mod;
    cin>>n>>m;
    string s[n];long long int a[n][m];
    for(i=0;i<n;i++)
        cin>>s[i];
    for(i=0;i<n;i++)
    {
        long long int l=mod,r=mod;
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                a[i][j]=0;
                l=j;
            }
            else
            {
                if(l!=mod)
                    a[i][j]=min(j-l,m-j+l);
                else
                    a[i][j]=mod;
            }
        }
        for(j=m-1;j>=0;j--)
        {
            if(s[i][j]=='1')
                r=j;
            else if(r!=mod)
                a[i][j]=min(a[i][j],min(r-j,m-r+j));
        }
    }
    for(j=0;j<m;j++)
    {
        long long int sum=0;
        for(i=0;i<n;i++)
        {
            if(a[i][j]==mod)
                break;
            sum+=a[i][j];
        }
        if(i==n)
            ans=min(ans,sum);
    }
    if(ans!=mod)
        cout<<ans;
    else
        cout<<-1;
}


#include<iostream>
using namespace std;
int mod=INT_MAX;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
    fastio;
    long long int n,m,i,j,ans=mod;
    cin>>n>>m;
    string s[n];long long int a[n][m];
    for(i=0;i<n;i++)
        cin>>s[i];
    for(i=0;i<n;i++)
    {
        long long int l=mod,r=mod;
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                a[i][j]=0;
                l=j;
            }
            else
            {
                if(l!=mod)
                    a[i][j]=min(j-l,m-j+l);
                else
                    a[i][j]=mod;
            }
        }
        for(j=m-1;j>=0;j--)
        {
            if(s[i][j]=='1')
                r=j;
            else if(r!=mod)
                a[i][j]=min(a[i][j],min(r-j,m-r+j));
        }
    }
    for(j=0;j<m;j++)
    {
        long long int sum=0;
        for(i=0;i<n;i++)
        {
            if(a[i][j]==mod)
                break;
            sum+=a[i][j];
        }
        if(i==n)
            ans=min(ans,sum);
    }
    if(ans!=mod)
        cout<<ans;
    else
        cout<<-1;
}


#include<iostream>
using namespace std;
int mod=1e4+1;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
    fastio;
    int n,m,i,j,ans=mod;
    cin>>n>>m;
    string s[n];int a[n][m];
    for(i=0;i<n;i++)
        cin>>s[i];
    for(i=0;i<n;i++)
    {
        int l=mod,r=mod;
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                a[i][j]=0;
                l=j;
            }
            else
            {
                if(l!=mod)
                    a[i][j]=min(j-l,m-j+l);
                else
                    a[i][j]=mod;
            }
        }
        for(j=m-1;j>=0;j--)
        {
            if(s[i][j]=='1')
                r=j;
            else if(r!=mod)
                a[i][j]=min(a[i][j],min(r-j,m-r+j));
        }
    }
    for(j=0;j<m;j++)
    {
        int sum=0;
        for(i=0;i<n;i++)
        {
            if(a[i][j]==mod)
                break;
            sum+=a[i][j];
        }
        if(i==n)
            ans=min(ans,sum);
    }
    if(ans!=mod)
        cout<<ans;
    else
        cout<<-1;
}


#include<iostream>
using namespace std;
int mod=INT_MAX;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
    fastio;
    long long int n,m,i,j,ans=mod;
    cin>>n>>m;
    string s[n];long long int a[n][m];
    for(i=0;i<n;i++)
        cin>>s[i];
    for(i=0;i<n;i++)
    {
        long long int l=mod,r=mod,l1=mod,r1=mod;
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                a[i][j]=0;
                l=j;
                if(l1==mod)
                    l1=l;
            }
            else
            {
                if(l!=mod)
                    a[i][j]=min(j-l,m-j+l1);
                else
                    a[i][j]=mod;
            }
        }
        for(j=m-1;j>=0;j--)
        {
            if(s[i][j]=='1')
            {
                r=j;
                if(r1==mod)
                    r1=r;
            }
            else if(r!=mod)
                a[i][j]=min(a[i][j],min(r-j,m-r1+j));
        }
    }
    for(j=0;j<m;j++)
    {
        long long int sum=0;
        for(i=0;i<n;i++)
        {
            if(a[i][j]==mod)
                break;
            sum+=a[i][j];
        }
        if(i==n)
            ans=min(ans,sum);
    }
    if(ans!=mod)
        cout<<ans;
    else
        cout<<-1;
}


#include<iostream>
using namespace std;
int mod=1e4+1;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
    fastio;
    int n,m,i,j,ans=mod;
    cin>>n>>m;
    string s[n];int a[n][m];
    for(i=0;i<n;i++)
        cin>>s[i];
    for(i=0;i<n;i++)
    {
        int l=mod,r=mod;
        for(j=0;j<m;j++)
        {
            if(s[i][j]=='1')
            {
                a[i][j]=0;
                l=j;
            }
            else
            {
                if(l!=mod)
                    a[i][j]=min(j-l,m-j+l);
                else
                    a[i][j]=mod;
            }
        }
        for(j=m-1;j>=0;j--)
        {
            if(s[i][j]=='1')
                r=j;
            else
            {
                if(r!=mod)
                    a[i][j]=min(r-j,m-r+j);
                else
                    a[i][j]=mod;
            }
        }
    }
    for(j=0;j<m;j++)
    {
        int sum=0;
        for(i=0;i<n;i++)
        {
            if(a[i][j]==mod)
                break;
            sum+=a[i][j];
        }
        if(i==n)
            ans=min(ans,sum);
    }
    if(ans!=mod)
        cout<<ans;
    else
        cout<<-1;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){

    int n,m;
    cin>>n>>m;
    vector<string> s(n);
    for(int i=0;i<n;i++) cin>>s[i];
    int a[n][m];
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='1') a[i][j]=0;
            else{
                int p=0,k=j+1,x=0;
                while(true){
                    p++;
                    if(k>=m) k=0;
                    if(k==j) break;

                    if(s[i][k]=='1') {x=1;break;}
                    k++;

                }
            int h=0;
                k=j-1;
                while(true)
                {   h++;
                if(k<0) k=m-1;
                    if(k==j) break;

                    if(s[i][k]=='1') {x|=1;break;}
                    k--;
                }
                if(x==0) return cout<<-1,0;
                a[i][j]=min(h,p);
                //cout<<i<<" "<<j<<endl;
            }
        }
    }
    int ans=1e9,cnt=0;
    for(int i=0;i<m;i++){
        cnt=0;
        for(int j=0;j<n;j++){
            cnt+=a[j][i];
        }
        ans=min(ans,cnt);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){

    int n,m;
    cin>>n>>m;
    vector<string> s(n);
    for(int i=0;i<n;i++) cin>>s[i];
    int a[n][m];
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='1') a[i][j]=0;
            else{
                int p=0,k=j+1;
                while(true){
                    p++;
                    if(k==j) break;
                    if(k>=m) k=0;
                    if(s[i][k]=='1') {break;}
                    k++;

                }
            int h=0;
                k=j-1;
                while(true)
                {   h++;
                    if(k==j) break;
                    if(k<0) k=m-1;
                    if(s[i][k]=='1') {break;}
                    k--;
                }
                if(h==0 && p==0) return cout<<-1,0;
                a[i][j]=min(h,p);
                //cout<<i<<" "<<j<<endl;
            }
        }
    }
    int ans=1e9,cnt=0;
    for(int i=0;i<m;i++){
        cnt=0;
        for(int j=0;j<n;j++){
            cnt+=a[j][i];
        }
        ans=min(ans,cnt);
    }
    cout<<ans<<endl;
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;

#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

#ifdef DEBUG
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
#else
#define eprintf(...) assert (true)
#endif

const int MAX_H = 102, MAX_W = 10004, MAX_C = 0x3F3F3F3F, NA = -1;

char a [MAX_H] [MAX_W];
int d [MAX_H] [MAX_W];
int m, n;

int main (void)
{
 int i, j, cur;

 while (scanf (" %d %d", &n, &m) != EOF)
 {
  for (i = 0; i < n; i++)
   scanf (" %s", a[i]);
  memset (d, MAX_C, sizeof (d));

  for (i = 0; i < n; i++)
  {
   cur = MAX_C;
   for (j = 0; j < m + m; j++)
   {
    if (a[i][j % m] == '1')
     cur = 0;
    else
     cur++;
    d[i][j % m] = min (d[i][j % m], cur);
   }
  }

  for (i = 0; i < n; i++)
  {
   cur = MAX_C;
   for (j = m + m - 1; j >= 0; j--)
   {
    if (a[i][j % m] == '1')
     cur = 0;
    else
     cur++;
    d[i][j % m] = min (d[i][j % m], cur);
   }
  }

  int res = MAX_C;
  for (j = 0; j < m; j++)
  {
   int sum = 0;
   for (i = 0; i < n; i++)
    sum = min (MAX_C, sum + d[i][j]);
   eprintf ("%d: %d\n", j, sum);
   res = min (res, sum);
  }
  if (res >= MAX_C)
   res = NA;
  printf ("%d\n", res);
 }

 return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){

    int n,m;
    cin>>n>>m;
    vector<string> s(n);
    for(int i=0;i<n;i++) cin>>s[i];
    ll g=0;
for(int i=0;i<n;i++){
    g=0;
    for(int j=0;j<m;j++){
        if(s[i][j]=='1') {g=1;break;}
    }
    if(g==0) return cout<<-1,0;
}
    int a[n][m];
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='1') a[i][j]=0;
            else{
                int p=0,k=j+1,x=0;
                while(true){
                    p++;
                    if(k>=m) k=0;
                    if(k==j) break;

                    if(s[i][k]=='1') {x=1;break;}
                    k++;

                }
            int h=0;
                k=j-1;
                while(true)
                {   h++;
                if(k<0) k=m-1;
                    if(k==j) break;

                    if(s[i][k]=='1') {x|=1;break;}
                    k--;
                }
                if(x==0) return cout<<-1,0;
                a[i][j]=min(h,p);
                //cout<<i<<" "<<j<<endl;
            }
        }
    }
    int ans=1e9,cnt=0;
    for(int i=0;i<m;i++){
        cnt=0;
        for(int j=0;j<n;j++){
            cnt+=a[j][i];
        }
        ans=min(ans,cnt);
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define ld long double
#define int long long
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
using namespace std;
const int INF=3e18+9237;
const int MAXN=1e5+7;
const int M=110;
int a[M][MAXN],dp[M][MAXN];
bool chk=true;

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;cin>>n>>m;
    for(int i=0;i<n;i++){
        int OK=false;
        string s;cin>>s;
		for(int j=0;j<m;j++)
            a[i][j]=(s[j]=='1'),OK|=(s[j]=='1');
		chk&=OK;
    }
    if(chk==false)return cout<<-1,0;
    for(int i=0;i<n;i++){
        int t=1,cur=m-1;
        while(a[i][cur--]==0)t++;
        for(int j=0;j<m;j++){
            if(a[i][j]==1)t=0;
            dp[i][j]=t++;
        }
        t=1,cur=0;
        while(a[i][cur++]==0)t++;
        for(int j=m-1;j>=0;j--){
            if(a[i][j]==1)t=0;
            dp[i][j]=min(t++,dp[i][j]);
        }
    }
    /*for(int i=0;i<n;i++,cout<<endl)
        for(int j=0;j<m;j++)
            cout<<dp[i][j]<<" ";*/
    int res=INF;
    for(int j=0;j<m;j++){
        int sum=0;
        for(int i=0;i<n;i++)
            sum+=dp[i][j];
        res=min(res,sum);
    }
    cout<<res;
}


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

const int N = 1e4 + 2 ;
const int INF = 1e7 + 7 ;

char t[ 102 ][ N ] ;
int Left[ 102 ][ N ] ;
int Right[ 102 ][ N ] ;

int main() {
	int n , m ;
	cin >> n >> m ;
	for( int i = 0 ; i < n ; i ++ ) {
		scanf( "%s" , t[ i ] ) ;
	}
	for( int i = 0 ; i < n ; i ++ ) {
		int last = -INF ;
		for( int j = 0 ; j < m ; j ++ ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Left[ i ][ j ] = last ;
		}
		last = INF ;
		for( int j = m - 1 ; j >= 0 ; j -- ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Right[ i ][ j ] = last ;
		}
	}
	int ans = INF ;
	for( int j = 0 ; j < n ; j ++ ) {
		int sum = 0 ;
		for( int i = 0 ; i < n ; i ++ ) {
			sum += min( j - Left[ i ][ j ] , Right[ i ][ j ] - j ) ;
		}
		ans = min( ans , sum ) ;
	}
	if( ans > n*m ) ans = -1 ;
	cout << ans ;

	return 0 ;
}


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

const int N = 1e4 + 2 ;
const int INF = 1e7 + 7 ;

char t[ 102 ][ N ] ;
int Left[ 102 ][ N ] ;
int Right[ 102 ][ N ] ;

int main() {
	int n , m ;
	cin >> n >> m ;
	for( int i = 0 ; i < n ; i ++ ) {
		scanf( "%s" , t[ i ] ) ;
	}
	for( int i = 0 ; i < n ; i ++ ) {
		int len = INF ;
		for( int j_ = -m + 1 ; j_ < m ; j_ ++ ) {
			int j = ( j_ + m )%m ;
			if( t[ i ][ j ] == '1' ) len = 0 ;
			else len ++ ;
			Left[ i ][ j ] = len ;
		}
		len = INF ;
		for( int j_ = 2*m - 1 ; j_ >= 0 ; j_ -- ) {
			int j = ( j_ )%m ;
			if( t[ i ][ j ] == '1' ) len = 0 ;
			else len ++ ;
			Right[ i ][ j ] = len ;
		}
	}
	int ans = INF ;
	for( int j = 0 ; j < m ; j ++ ) {
		int sum = 0 ;
		for( int i = 0 ; i < n ; i ++ ) {
			sum += min( Left[ i ][ j ] , Right[ i ][ j ] ) ;
		}
		ans = min( ans , sum ) ;
	}
	if( ans >= INF ) ans = -1 ;
	cout << ans ;

	return 0 ;
}


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

const int N = 1e4 + 2 ;
const int INF = 1e7 + 7 ;

char t[ 102 ][ N ] ;
int Left[ 102 ][ N ] ;
int Right[ 102 ][ N ] ;

int main() {
	int n , m ;
	cin >> n >> m ;
	for( int i = 0 ; i < n ; i ++ ) {
		scanf( "%s" , t[ i ] ) ;
	}
	for( int i = 0 ; i < n ; i ++ ) {
		int last = -INF ;
		for( int j = 0 ; j < m ; j ++ ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Left[ i ][ j ] = last ;
		}
		last = INF ;
		for( int j = m - 1 ; j >= 0 ; j -- ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Right[ i ][ j ] = last ;
		}
	}
	int ans = INF ;
	for( int j = 0 ; j < m ; j ++ ) {
		int sum = 0 ;
		for( int i = 0 ; i < n ; i ++ ) {
			sum += min( j - Left[ i ][ j ] , Right[ i ][ j ] - j ) ;
		}
		ans = min( ans , sum ) ;
	}
	if( ans > n*m ) ans = -1 ;
	cout << ans ;

	return 0 ;
}


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

const int N = 1e4 + 2 ;
const int INF = 1e7 + 7 ;

bool t[ 102 ][ N ] ;
int Left[ 102 ][ N ] ;
int Right[ 102 ][ N ] ;

int main() {
	int n , m ;
	cin >> n >> m ;
	for( int i = 0 ; i < n ; i ++ ) {
		scanf( "%s" , t[ i ] ) ;
	}
	for( int i = 0 ; i < n ; i ++ ) {
		int last = -INF ;
		for( int j = 0 ; j < m ; j ++ ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Left[ i ][ j ] = last ;
		}
		last = INF ;
		for( int j = m - 1 ; j >= 0 ; j -- ) {
			if( t[ i ][ j ] == '1' ) last = j ;
			Right[ i ][ j ] = last ;
		}
	}
	int ans = INF ;
	for( int j = 0 ; j < n ; j ++ ) {
		int sum = 0 ;
		for( int i = 0 ; i < n ; i ++ ) {
			sum += min( j - Left[ i ][ j ] , Right[ i ][ j ] - j ) ;
		}
		ans = min( ans , sum ) ;
	}
	if( ans > n*m ) ans = -1 ;
	cout << ans ;

	return 0 ;
}


#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#pragma comment(linker, "/STACK:836777216")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-7
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))

int a[105][10005];
int l[105][10005];
int r[105][10005];
int mins[105][10005];

int n, m;

int main(void) {
  scanf("%d%d", &n, &m);
  REP(i, n) {
    REP(j, m) {
      scanf("%1d", &a[i][j]);
    }
  }

  REP(i, n) {
    int cur = -1;
    REP(j, m) {
      if (a[i][j] == 0) {
        if (cur != -1) {
          l[i][j] = j - cur;
        } else {
          l[i][j] = -1;
        }
      } else {
        cur = j;
        l[i][j] = 0;
      }
    }

    if (cur == -1) {
      printf("-1\n");
      return 0;
    }

    REP(j, m) {
      if (l[i][j] == -1) {
        l[i][j] = m - cur + j;
      }
    }

    cur = -1;
    REPD(j, m) {
      if (!a[i][j]) {
        if (cur != -1)
          r[i][j] = cur - j;
        else
          r[i][j] = -1;
      } else {
        cur = j;
        r[i][j] = 0;
      }
    }

    REPD(j, m) {
      if (r[i][j] == -1) {
        r[i][j] = m - j + cur;
      }
    }

    REP(j, m) {
      mins[i][j] = min(l[i][j], r[i][j]);
    }
  }

  int ans = -1;
  REP(j, m) {
    int cur = 0;
    REP(i, n) {
      cur += mins[i][j];
    }

    if (ans == -1 || ans > cur) {
      ans = cur;
    }
  }

  printf("%d\n", ans);
}


#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#pragma comment(linker, "/STACK:836777216")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-7
#define X first
#define Y second
#define bit(n) (1 << (n))
#define bit64(n) (ll(1) << (n))
#define sqr(n) ((n) * (n))

int a[105][1005];
int l[105][1005];
int r[105][1005];
int mins[105][1005];

int n, m;

int main(void) {
  scanf("%d%d", &n, &m);
  REP(i, n) {
    REP(j, m) {
      scanf("%1d", &a[i][j]);
    }
  }

  REP(i, n) {
    int cur = -1;
    REP(j, m) {
      if (a[i][j] == 0) {
        if (cur != -1) {
          l[i][j] = j - cur;
        } else {
          l[i][j] = -1;
        }
      } else {
        cur = j;
        l[i][j] = 0;
      }
    }

    if (cur == -1) {
      printf("-1\n");
      return 0;
    }

    REP(j, m) {
      if (l[i][j] == -1) {
        l[i][j] = m - cur + j;
      }
    }

    cur = -1;
    REPD(j, m) {
      if (!a[i][j]) {
        if (cur != -1)
          r[i][j] = cur - j;
        else
          r[i][j] = -1;
      } else {
        cur = j;
        r[i][j] = 0;
      }
    }

    REPD(j, m) {
      if (r[i][j] == -1) {
        r[i][j] = m - j + cur;
      }
    }

    REP(j, m) {
      mins[i][j] = min(l[i][j], r[i][j]);
    }
  }

  int ans = -1;
  REP(j, m) {
    int cur = 0;
    REP(i, n) {
      cur += mins[i][j];
    }

    if (ans == -1 || ans > cur) {
      ans = cur;
    }
  }

  printf("%d\n", ans);
}


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

signed main(){
  ios::sync_with_stdio( 0 );
  int N, M; cin >> N >> M;
  vector< string > mat( N );
  for( int i = 0; i < N; ++i ){
    cin >> mat[ i ];
  }
  vector< vector< int > > nl( N, vector< int >( M ) ), nr( N, vector< int >( M ) );
  for( int i = 0; i < N; ++i ){
    {
      int p = M - 1;
      while( p >= 0 ){
        if( mat[ i ][ p ] == '1' ){
          break;
        }
        --p;
      }
      if( p == -1 ){
        cout << -1 << endl;
        exit( 0 );
      }
      p -= M; // CYUUI!!!
      for( int j = 0; j < M; ++j ){
        if( mat[ i ][ j ] == '1' ){
          p = j;
        }
        nl[ i ][ j ] = p;
      }
    }
    {
      int p = 0;
      while( p < M ){
        if( mat[ i ][ p ] == '1' ){
          break;
        }
        ++p;
      }
      p += M; // CYUUI!!!
      for( int j = M - 1; j >= 0; --j ){
        if( mat[ i ][ j ] == '1' ){
          p = j;
        }
        nr[ i ][ j ] = p;
      }
    }
  }
  int ans = 1 << 30;
  for( int i = 0; i < M; ++i ){
    int cost = 0;
    for( int j = 0; j < N; ++j ){
      cost += min( i - nl[ j ][ i ], nr[ j ][ i ] - i );
    }
    ans = min( ans, cost );
  }
  if( ans == 1 << 30 ) ans = -1;
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define fill(a,val) memset(a,val,sizeof(a))
#define pb  push_back
#define mp  make_pair
#define ff  first
#define ss  second
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ld PI  = 3.141592653589793;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll power1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
int max(int a,int b){if(a>b)return a;return b;}
int min(int a,int b){if(a>b)return b;return a;}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return a.second<b.second;
}
int main()
{
    fastio();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n,m;
    cin>>n>>m;
    string a[n];int b[n][m],flag=0;
    for(int i=0;i<n;i++){cin>>a[i];}
    for(int i=0;i<n;i++)
    {
        int k=-1;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
            {
                if(k==-1)
                {
                    for(int l=0;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
                else
                {
                    for(int l=k+1;l<=(j+k)/2;l++)
                    {
                        b[i][l]=l-k;
                    }
                    for(int l=(j+k)/2+1;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
            }
        }
        if(k==-1)flag=1;
        else
        {
            for(int l=k+1;l<m;l++)
            {
                b[i][l]=l-k;
            }
        }
    }
    /*for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<b[i][j]<<" ";
        }
        cout<<"\n";
    }*/
    if(flag==1){cout<<-1;return 0;}
    int ans=INT_MAX;
    for(int i=0;i<m;i++)
    {
        int sum=0;
        for(int j=0;j<n;j++)
        {
            if(b[j][i]>m/2)b[j][i]=m-b[j][i];
            sum+=b[j][i];
        }
        if(sum<ans)ans=sum;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define fill(a,val) memset(a,val,sizeof(a))
#define pb  push_back
#define mp  make_pair
#define ff  first
#define ss  second
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ld PI  = 3.141592653589793;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll power1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
int max(int a,int b){if(a>b)return a;return b;}
int min(int a,int b){if(a>b)return b;return a;}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return a.second<b.second;
}
int main()
{
    fastio();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n,m;
    cin>>n>>m;
    string a[n];int b[n][m],flag=0;
    for(int i=0;i<n;i++){cin>>a[i];}
    for(int i=0;i<n;i++)
    {
        int k=-1;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
            {
                if(k==-1)
                {
                    for(int l=0;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
                else
                {
                    for(int l=k+1;l<=(j+k)/2;l++)
                    {
                        b[i][l]=l-k;
                    }
                    for(int l=(j+k)/2+1;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
            }
        }
        if(k==-1)flag=1;
        else
        {
            for(int l=k+1;l<m;l++)
            {
                b[i][l]=l-k;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='0')
            {
                if(b[i][m-1]+j+1<b[i][j])
                {
                    b[i][j]=b[i][m-1]+j+1;
                }
            }
            else break;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=m-1;j>=0;j--)
        {
            if(a[i][j]=='0')
            {
                if(b[i][0]+(m-j)<b[i][j])
                {
                    b[i][j]=b[i][0]+(m-j);
                }
            }
            else break;
        }
    }
    if(flag==1){cout<<-1;return 0;}
    int ans=INT_MAX;
    for(int i=0;i<m;i++)
    {
        int sum=0;
        for(int j=0;j<n;j++)
        {
            if(b[j][i]>m/2)b[j][i]=m-b[j][i];
            sum+=b[j][i];
        }
        if(sum<ans)ans=sum;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define fill(a,val) memset(a,val,sizeof(a))
#define pb  push_back
#define mp  make_pair
#define ff  first
#define ss  second
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ld PI  = 3.141592653589793;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll power1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
int max(int a,int b){if(a>b)return a;return b;}
int min(int a,int b){if(a>b)return b;return a;}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return a.second<b.second;
}
int main()
{
    fastio();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n,m;
    cin>>n>>m;
    string a[n];int b[n][m],flag=0;
    for(int i=0;i<n;i++){cin>>a[i];}
    for(int i=0;i<n;i++)
    {
        int k=-1;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
            {
                if(k==-1)
                {
                    for(int l=0;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
                else
                {
                    for(int l=k+1;l<=(j+k)/2;l++)
                    {
                        b[i][l]=l-k;
                    }
                    for(int l=(j+k)/2+1;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
            }
        }
        if(k==-1)flag=1;
        else
        {
            for(int l=k+1;l<m;l++)
            {
                b[i][l]=l-k;
            }
        }
    }
    /*for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<b[i][j]<<" ";
        }
        cout<<"\n";
    }*/
    if(flag==1){cout<<-1;return 0;}
    int ans=INT_MAX;
    for(int i=0;i<m;i++)
    {
        int sum=0;
        for(int j=0;j<n;j++)
        {
            sum+=b[j][i];
        }
        if(sum<ans)ans=sum;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define fill(a,val) memset(a,val,sizeof(a))
#define pb  push_back
#define mp  make_pair
#define ff  first
#define ss  second
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ld PI  = 3.141592653589793;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll power1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
int max(int a,int b){if(a>b)return a;return b;}
int min(int a,int b){if(a>b)return b;return a;}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return a.second<b.second;
}
int main()
{
    fastio();
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n,m;
    cin>>n>>m;
    string a[n];int b[n][m],flag=0;
    for(int i=0;i<n;i++){cin>>a[i];}
    for(int i=0;i<n;i++)
    {
        int k=-1;
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='1')
            {
                if(k==-1)
                {
                    for(int l=0;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
                else
                {
                    for(int l=k+1;l<(j+k)/2;l++)
                    {
                        b[i][l]=l-k;
                    }
                    for(int l=(j+k)/2;l<=j;l++)
                    {
                        b[i][l]=j-l;
                    }
                    k=j;
                }
            }
        }
        if(k==-1)flag=1;
        else
        {
            for(int l=k+1;l<m;l++)
            {
                b[i][l]=l-k;
            }
        }
    }
    /*for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<b[i][j]<<" ";
        }
        cout<<"\n";
    }*/
    if(flag==1){cout<<-1;return 0;}
    int ans=INT_MAX;
    for(int i=0;i<m;i++)
    {
        int sum=0;
        for(int j=0;j<n;j++)
        {
            sum+=b[j][i];
        }
        if(sum<ans)ans=sum;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

#define MAXN 10002
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz size
#define FAIL {cout <<-1 << endl; return 0;}

int N, M;
char S[100][MAXN];
int dp[100][MAXN];

int main(int argc, char** argv) {
#ifdef OJ
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    std::ios::sync_with_stdio(false);

    cin >> N >> M;
    int ok = 1;
    for (int i = 0; i < N; i++) {
        cin >> S[i];

        int x = -1;
        for (int j = 0; j < M; j++)
            if (S[i][j] == '1') x = j;
        ok &= (x > -1);
    }

    if (!ok) FAIL;

    for (int i = 0; i < N; i++) {
        int x;
        for (int j = 0; j < M; j++)
            if (S[i][j] == '1') {
                x = j;
                break;
            }

        int d = 0;
        for (int j = (x + 1) % M; j != x; j = (j + 1) % M) {
            d = (S[i][j] == '1') ? 0 : d + 1;
            dp[i][j] = d;
        }

        d = 0;
        for (int j = (x + M - 1) % M; j != x; j = (j + M - 1) % M) {
            d = (S[i][j] == '1') ? 0 : d + 1;
            dp[i][j] = min(d, dp[i][j]);
        }
    }

    int ans = 1e9;
    for (int j = 0; j < M; j++) {
        int s = 0;
        for (int i = 0; i < N; i++) s += dp[i][j];
        ans = min(ans, s);
    }

    cout << ans << endl;
    return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <bits/stdc++.h>
#define f(i,n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef vector<int> vi;
typedef long long int ll;
vector<vector<int>> adj;

class TaskA {
public:
	void solve(std::istream& in, std::ostream& out) {
        int n, m; in>>n>>m;
        int dp[n][m];
        memset(dp, 0, sizeof(dp));
        vector<string> arr(n);
        vector<int> temp;
        f(i, n) in>>arr[i];
        f(i, n){
            temp.clear();
            f(j, m) if(arr[i][j]=='1') temp.push_back(j);
            if(temp.size()==0){
                out<<-1;
                return;
            }
            int cur=0, pos=0;
            while(cur<m){
                int front = pos==temp.size()?temp[0]:temp[pos];
                int back = pos==0?temp.back():temp[pos-1];
                int lim=temp.size()==pos?m:temp[pos];
                while(1){
                    if(cur==lim) break;
                    int d1 = front<cur? front+m-cur:front-cur;
                    int d2 = back>cur? cur+m-back:cur-back;
                    dp[i][cur]=min(d1,d2);
                    cur++;
                }
                cur++;
                pos++;
            }
        }
        int res = INT_MAX;
        f(j, m){
            int cost=0;
            f(i, n){
                cost+=dp[i][j];
            }
            res=min(res, cost);
        }
        out<<res;

	}
    void inG(int m, istream& in){ int u,v; adj = vector<vi>(200005);f(i, m){ in>>u>>v; adj[u].push_back(v); adj[v].push_back(u); }}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	TaskA solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 10005
#define MXX 200005
#define  s second
#define f first
#define mod 1000000007
#define inf 2000000000000
ll ex[]={1,-1,0,0};
ll wye[]={0,0,1,-1};
using namespace std;
vector<ll>vt[105];
ll a[105],an=inf,r,c,cnt[105];
char s[105][MX];
int main()
{
    fast();
    scanf("%I64d%I64d",&r,&c);
    for(ll i=1;i<=r;i++) scanf("%s",s[i]);
    for(ll i=1;i<=r;i++)
    {
        bool flag=true;
        for(ll j=0;j<c;j++)
        {
            if(s[i][j]=='1')
            {
                flag=false;
                cnt[i]++;
                vt[i].push_back(j+1);
            }
        }
       // cout<<vt[i].size()<<endl;
        if(flag)
        {
            printf("-1");
            return 0;
        }
    }
    for(ll i=1;i<=c;i++)
    {
        ll tmp=0;
        for(ll j=1;j<=r;j++)
        {
            ll k=a[j],b=-1,f=-1;
            while(vt[j][k]<i && k<cnt[j])
            {
                ++k;
            }
            a[j]=k;
            if(k>=0 && k<cnt[j] && vt[j][k]==i) continue;
            else if(k>=cnt[j])
            {
                f=(vt[j][0]+c)-i;
                b=i-vt[j][k-1];
            }
            else if(k==0)
            {
                f=vt[j][k]-i;
                b=c-vt[j][cnt[j]-1]+i;
            }
            else
            {
                f=vt[j][k]-i;
                b=i-vt[j][k-1];
            }
            tmp+=min(f,b);
        }
       // cout<<tmp<<" ";
        an=min(an,tmp);
      //  cout<<an<<endl;
    }
    printf("%I64d\n",an);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, m;
char a[107][10007];
vector<int> sq1[107];

int Move(int u, int v) {
    return (min(abs(u - v), m - max(u, v) + min(u, v)));
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        bool ok = true;
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] != '0') {
                ok = false;
                sq1[i].push_back(j);
            }
        }
        if (ok) {
            cout << -1;
            return 0;
        }
    }

    int res = 1000000007;
    for(int i = 1; i <= m; i++) {
        int temp = 0;
        for(int j = 1; j <= n; j++) {
            int temp1 = 1000000007;
            for(int k = 0; k < sq1[j].size(); k++) temp1 = min(temp1, Move(sq1[j][k], i));
            temp += temp1;
            if (temp > res) break;
        }
        res = min(res, temp);
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define NN 10010
#define rep(i, a, b) for (int i = (a); i < (b); i ++)
#define clr(a, b) memset(a, b, sizeof(a))
#define eps 1e-10
#define inf 1000000000

typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;

int a[110][30010];

char str[NN], s[3 * NN];

int main() {

	int N, M, la, ans, res;
	scanf("%d%d", &N, &M);
	for (int i = 0; i < N; i ++) {
		scanf("%s", str);
		strcpy(s, str);
		strcat(s, str);
		strcat(s, str);

		for (int j = 0; j < 3 * M; j ++) a[i][j] = inf;
		la = -1;
		for (int j = 0; j < 3 * M; j ++) {
			if (s[j] == '1') la = j;
			if (la != -1) a[i][j] = j - la;
		}
		la = -1;
		for (int j = 3 * M - 1; j >= 0; j --) {
			if (s[j] == '1') la = j;
			if (la != -1) a[i][j] = min(a[i][j], la - j);
		}

	}

	ans = inf;
	for (int j = M ; j < 2 * M; j ++) {
		res = 0;
		bool ok = false ;
		for (int i = 0; i < N; i ++) {
			if (a[i][j] == inf) {
				ok = true ;
				break ;
			}
			res += a[i][j];
		}
		if (ok) continue ;
		ans = min(ans, res);
	}
	if (ans == inf) puts("-1");
	else printf("%d\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, m;
char a[107][107];
vector<int> sq1[107];

int Move(int u, int v) {
    return (min(abs(u - v), m - max(u, v) + min(u, v)));
}

int main() {
    //freopen("input.txt", "r", stdin);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        bool ok = true;
        for(int j = 1; j <= m; j++) {
            cin >> a[i][j];
            if (a[i][j] != '0') {
                ok = false;
                sq1[i].push_back(j);
            }
        }
        if (ok) {
            cout << -1;
            return 0;
        }
    }

    int res = 1000000007;
    for(int i = 1; i <= m; i++) {
        int temp = 0;
        for(int j = 1; j <= n; j++) {
            int temp1 = 1000000007;
            for(int k = 0; k < sq1[j].size(); k++) temp1 = min(temp1, Move(sq1[j][k], i));
            temp += temp1;
        }
        res = min(res, temp);
    }
    cout << res << endl;
}


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

const int MAX = 1e4 + 6;
char tab[106][MAX];
int bame[106][MAX], dane[106][MAX];
int L[106], R[106];

int main(){

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

    int n, m;
    cin >> n >> m;

    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin >> tab[i][j];
        }
    }

    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(tab[i][j] == '1'){
                L[i] = j;
                break;
            }
        }
        for(int j=1; j<=m; j++){
            if(tab[i][j] == '1'){
                R[i] = j;
            }
        }
        if(!L[i] and !R[i]){
            cout << "-1";
            return 0;
        }
    }

    for(int i=1; i<=n; i++){
        int last_one = -1;
        for(int j=1; j<=m; j++){
            if(tab[i][j] == '1'){
                last_one = j;
            }
            if(last_one == -1){
                bame[i][j] = m - R[i] + j;
                continue;
            }
            bame[i][j] = j - last_one;
        }
    }

    for(int i=1; i<=n; i++){
        int last_one = -1;
        for(int j=m; j>=1; j--){
            if(tab[i][j] == '1'){
                last_one = j;
            }
            if(last_one == -1){
                dane[i][j] = L[i] + m - j;
                continue;
            }
            dane[i][j] = last_one - j;
        }
    }

    int ans = 1e9;

    for(int col=1; col<=m; col++){
        int curr = 0;
        for(int row=1; row<=n; row++){
            curr += min(bame[row][col], dane[row][col]);
        }
        ans = min(ans, curr);
    }

    cout << ans;

}


#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 1e4 + 5;

int ans[N];

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, m;
	cin >> n >> m;

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

		int f = -1, l = -1;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				if (f == -1) f = j;
				l = j;
			}
		}

		if (f == -1) {
			cout << "-1\n";
			return 0;
		}

		vector< int> cur(m);

		for (int j = 0; j < m; j++) {
			int d1 = abs(j - f), d2 = abs(j - l);
			cur[j] = min(m - d1, m - d2);
		}

		int last = -1;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') last = j;
			if (last != -1) cur[j] = min(cur[j], j - last);
		}
		last = -1;
		for (int j = m - 1; j >= 0; j--) {
			if (s[j] == '1') last = j;
			if (last != -1) cur[j] = min(cur[j], last - j);
		}

		for (int j = 0; j < m; j++) ans[j] += cur[j];
	}

	cout << *min_element(ans, ans + m) << "\n";
}


#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 102;

int L[N][N * N], R[N][N * N];
int ans[N][N * N];

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, m;
	cin >> n >> m;

	memset(L, -1, sizeof L);
	memset(R, -1, sizeof R);
	memset(ans, 0x3f3f3f3f, sizeof ans);

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

		int f = -1, l = -1;
		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				if (f == -1) f = j;
				l = j;
			}
		}

		if (f == -1) {
			cout << "-1\n";
			return 0;
		}

		for (int j = 0; j < m; j++) {
			int d1 = abs(j - f), d2 = abs(j - l);
			ans[i][j] = min(m - d1, m - d2);
		}

		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				L[i][j] = j;
			}
			else if (j > 0) {
				L[i][j] = L[i][j - 1];
			}
		}

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

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (L[i][j] != -1) {
				ans[i][j] = min(ans[i][j], j - L[i][j]);
			}
			if (R[i][j] != -1) {
				ans[i][j] = min(ans[i][j], R[i][j] - j);
			}
		}
	}

	int best = 0x3f3f3f3f;

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

		best = min(best, cur);
	}

	cout << best << endl;
}


#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 102;

int L[N][N * N], R[N][N * N];
int ans[N][N * N];

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, m;
	cin >> n >> m;

	memset(L, -1, sizeof L);
	memset(R, -1, sizeof R);

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

		for (int j = 0; j < m; j++) {
			if (s[j] == '1') {
				L[i][j] = j;
			}
			else if (j > 0) {
				L[i][j] = L[i][j - 1];
			}
		}

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

		if (L[i][0] == -1 && R[i][0] == -1) {
			cout << -1;
			return 0;
		}
	}

	memset(ans, 0x3f3f3f3f, sizeof ans);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (L[i][j] != -1) {
				ans[i][j] = min(ans[i][j], j - L[i][j]);
			}
			if (R[i][j] != -1) {
				ans[i][j] = min(ans[i][j], R[i][j] - j);
			}
		}
	}

	int best = 0x3f3f3f3f;

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

		best = min(best, cur);
	}

	cout << best << endl;
}


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

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

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


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

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

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