package round141;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class A implements Runnable {

	private void solve() throws NumberFormatException, IOException {
		int n = in.nextInt(); // 100
		int m = in.nextInt(); // 10000

		boolean table[][] = new boolean[n][m];
		for (int i = 0; i < n; i++) {
			String row = in.next();
			for (int j = 0; j < m; j++) {
				table[i][j] = row.charAt(j) == '1';
			}
		}

		int firstOne[] = new int[n];
		int lastOne[] = new int[n];
		boolean foundZeroRow = false;
		for (int i = 0; i < n; i++) {
			firstOne[i] = -1;
			for (int j = 0; j < m; ++j) {
				if (table[i][j]) {
					firstOne[i] = j;
					break;
				}
			}
			if (firstOne[i] == -1) {
				foundZeroRow = true;
				break;
			}
			for (int j = m - 1; j >= 0; j--) {
				if (table[i][j]) {
					lastOne[i] = j;
					break;
				}
			}
		}

		if (foundZeroRow) {
			out.println(-1);
			return;
		}

		int[][] minDistance = new int[n][m];
		for (int i = 0; i < n; i++) {
			// Left
			int curDistance = m - lastOne[i];
			for (int j = 0; j < m; j++, curDistance++) {
				if (table[i][j])
					curDistance = 0;
				minDistance[i][j] = curDistance;
			}
			// Right
			curDistance = firstOne[i] + 1;
			for (int j = m - 1; j >= 0; j--, curDistance++) {
				if (table[i][j])
					curDistance = 0;
				minDistance[i][j] = Math.min(minDistance[i][j], curDistance);
			}
		}

		int totalMin = Integer.MAX_VALUE;
		for (int j = 0; j < m; j++) {
			int currentSum = 0;
			for (int i = 0; i < n; i++) {
				currentSum += minDistance[i][j];
			}
			totalMin = Math.min(totalMin, currentSum);
		}

		out.println(totalMin);
	}

	private FastScanner in;
	private PrintWriter out;

	@Override
	public void run() {
		in = new FastScanner(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		try {
			solve();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
	}

	private static class FastScanner
	{
		private final BufferedReader reader;
		private StringTokenizer tokens;

		public FastScanner(Reader rawReader) {
			reader = new BufferedReader(rawReader);
		}

		public boolean hasNext() throws IOException
		{
			while (tokens == null || !tokens.hasMoreTokens()) {
				String nextLine = reader.readLine();
				if (nextLine == null)
					return false;
				tokens = new StringTokenizer(nextLine);
			}
			return true;
		}

		public String next() throws IOException {
			return hasNext() ? tokens.nextToken() : null;
		}

		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}
	}

	public static void main(String[] args) {
		new A().run();
	}

}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
public class B {
	static int oo = 987654321;
	public static void main(String[] args) {
		MScanner sc = new MScanner();
		PrintWriter out = new PrintWriter(System.out);

		int N = sc.nextInt();
		int M = sc.nextInt();
		int[][] grid = new int[N][M];
		for(int a=0;a<N;a++){
			String cur = sc.next();
			int safe = 0;
			for(int b=0;b<M;b++){
				grid[a][b]=cur.charAt(b)=='1'?1:0;
				if(grid[a][b]==1)safe++;
			}
			if(safe==0){
				out.println("-1");
				out.close();
				return;
			}
		}
		int[][] magic = new int[N][M];
		for(int a=0;a<N;a++){
			int[] left=  new int[M];
			int[] right = new int[M];
			Arrays.fill(left,oo);
			Arrays.fill(right,oo);
			for(int b=0;b<M;b++){
				if(grid[a][b]==1)
					left[b]=b;
				else if(b!=0)left[b]=left[b-1];
			}
			for(int b=M-1;b>=0;b--){
				if(grid[a][b]==1)
					right[b]=b;
				else if(b!=M-1)right[b]=right[b+1];
			}
			for(int b=0;b<M;b++)
				magic[a][b] = Math.min(Math.abs(b-left[b]),Math.abs(b-right[b]));
		}
		int best = Integer.MAX_VALUE;
		for(int b=0;b<M;b++){
			int cur = 0;
			for(int a=0;a<N;a++)
				cur+=magic[a][b];
			best = Math.min(cur,best);
		}
		out.println(best);
		out.close();

	}

	static class MScanner{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public MScanner(){
			stream = System.in;
			//stream = new FileInputStream(new File("dec.in"));

		}
		int read(){
			if(numChars==-1)
				throw new InputMismatchException();
			if(curChar>=numChars){
				curChar = 0;
				try{
					numChars = stream.read(buf);
				} catch (IOException e){
					throw new InputMismatchException();
				}
				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c){
			return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
		}
		boolean isEndline(int c){
			return c=='\n'||c=='\r'||c==-1;
		}
		int nextInt(){
			return Integer.parseInt(next());
		}
		int[] nextInt(int N){
			int[] ret = new int[N];
			for(int a=0;a<N;a++)
				ret[a] = nextInt();
			return ret;
		}
		int[][] nextInt(int N, int M){
			int[][] ret = new int[N][M];
			for(int a=0;a<N;a++)
				ret[a] = nextInt(M);
			return ret;
		}
		long nextLong(){
			return Long.parseLong(next());
		}
		long[] nextLong(int N){
			long[] ret = new long[N];
			for(int a=0;a<N;a++)
				ret[a] = nextLong();
			return ret;
		}
		double nextDouble(){
			return Double.parseDouble(next());
		}
		double[] nextDouble(int N){
			double[] ret = new double[N];
			for(int a=0;a<N;a++)
				ret[a] = nextDouble();
			return ret;
		}
		String next(){
			int c = read();
			while(isSpaceChar(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c=read();
			} while(!isSpaceChar(c));
			return res.toString();
		}
		String[] next(int N){
			String[] ret = new String[N];
			for(int a=0;a<N;a++)
				ret[a] = next();
			return ret;
		}
		String nextLine(){
			int c = read();
			while(isEndline(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c = read();
			}while(!isEndline(c));
			return res.toString();
		}
		String[] nextLine(int N){
			String[] ret = new String[N];
			for(int a=0;a<N;a++)
				ret[a] = nextLine();
			return ret;
		}

	}
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
public class B {
	static int oo = 987654321;
	public static void main(String[] args) {
		MScanner sc = new MScanner();
		PrintWriter out = new PrintWriter(System.out);

		int N = sc.nextInt();
		int M = sc.nextInt();
		int[][] grid = new int[N][M];
		for(int a=0;a<N;a++){
			String cur = sc.next();
			int safe = 0;
			for(int b=0;b<M;b++){
				grid[a][b]=cur.charAt(b)=='1'?1:0;
				if(grid[a][b]==1)safe++;
			}
			if(safe==0){
				out.println("-1");
				out.close();
				return;
			}
		}
		int[][] magic = new int[N][M];
		for(int a=0;a<N;a++){
			int[] dist = new int[M];
			Arrays.fill(dist,-1);
			Queue<Integer> Q = new LinkedList<Integer>();
			for(int b=0;b<M;b++){
				if(grid[a][b]==1){
					Q.add(b);
					dist[b]=0;
				}
			}
			while(!Q.isEmpty()){
				int cur = Q.poll();
				int pos = (cur+1)%M;
				if(dist[pos]==-1){
					dist[pos]=dist[cur]+1;
					Q.add(pos);
				}
				pos = (cur-1+M)%M;
				if(dist[pos]==-1){
					dist[pos]=dist[cur]+1;
					Q.add(pos);
				}
			}
			magic[a] = dist;
		}
		int best = Integer.MAX_VALUE;
		for(int b=0;b<M;b++){
			int cur = 0;
			for(int a=0;a<N;a++)
				cur+=magic[a][b];
			best = Math.min(cur,best);
		}
		out.println(best);
		out.close();

	}

	static class MScanner{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public MScanner(){
			stream = System.in;
			//stream = new FileInputStream(new File("dec.in"));

		}
		int read(){
			if(numChars==-1)
				throw new InputMismatchException();
			if(curChar>=numChars){
				curChar = 0;
				try{
					numChars = stream.read(buf);
				} catch (IOException e){
					throw new InputMismatchException();
				}
				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c){
			return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
		}
		boolean isEndline(int c){
			return c=='\n'||c=='\r'||c==-1;
		}
		int nextInt(){
			return Integer.parseInt(next());
		}
		int[] nextInt(int N){
			int[] ret = new int[N];
			for(int a=0;a<N;a++)
				ret[a] = nextInt();
			return ret;
		}
		int[][] nextInt(int N, int M){
			int[][] ret = new int[N][M];
			for(int a=0;a<N;a++)
				ret[a] = nextInt(M);
			return ret;
		}
		long nextLong(){
			return Long.parseLong(next());
		}
		long[] nextLong(int N){
			long[] ret = new long[N];
			for(int a=0;a<N;a++)
				ret[a] = nextLong();
			return ret;
		}
		double nextDouble(){
			return Double.parseDouble(next());
		}
		double[] nextDouble(int N){
			double[] ret = new double[N];
			for(int a=0;a<N;a++)
				ret[a] = nextDouble();
			return ret;
		}
		String next(){
			int c = read();
			while(isSpaceChar(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c=read();
			} while(!isSpaceChar(c));
			return res.toString();
		}
		String[] next(int N){
			String[] ret = new String[N];
			for(int a=0;a<N;a++)
				ret[a] = next();
			return ret;
		}
		String nextLine(){
			int c = read();
			while(isEndline(c))
				c=read();
			StringBuilder res = new StringBuilder();
			do{
				res.appendCodePoint(c);
				c = read();
			}while(!isEndline(c));
			return res.toString();
		}
		String[] nextLine(int N){
			String[] ret = new String[N];
			for(int a=0;a<N;a++)
				ret[a] = nextLine();
			return ret;
		}

	}
}


import java.io.*;

import java.awt.geom.Point2D;
import java.text.*;
import java.math.*;
import java.util.*;

public class Main implements Runnable {

	final String filename = "";

	public void solve() throws Exception {
		int n = iread(), m = iread();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++) {
			String s = readword();
			for (int j = 0; j < m; j++)
				a[i][j] = s.charAt(j) - '0';
		}
		int ans = n * m + 1;
		int[][] dist = new int[n][m];
		for (int i = 0; i < n; i++) {
			boolean flag = false;
			Arrays.fill(dist[i], m + 1);
			for (int j = 0; j < m; j++) {
				if (a[i][j] == 1) {
					flag = true;
					dist[i][j] = 0;
				}
			}
			if (!flag) {
				out.write("-1\n");
				return;
			}
			for (int j = 0; j < 2 * m; j++) {
				dist[i][j % m] = Math.min(dist[i][j % m], dist[i][(j + m - 1) % m] + 1);
			}
			for (int j = 2 * m - 1; j >= 0; j--) {
				dist[i][j % m] = Math.min(dist[i][j % m], dist[i][(j + 1) % m] + 1);
			}
		}
		for (int c = 0; c < m; c++) {
			int res = 0;
			for (int i = 0; i < n; i++) {
				res += dist[i][c];
			}
			ans = Math.min(ans, res);
		}
		out.write(ans + "\n");
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new BufferedWriter(new OutputStreamWriter(System.out));
			// in = new BufferedReader(new FileReader(filename+".in"));
			// out = new BufferedWriter(new FileWriter(filename+".out"));
			solve();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public int iread() throws Exception {
		return Integer.parseInt(readword());
	}

	public double dread() throws Exception {
		return Double.parseDouble(readword());
	}

	public long lread() throws Exception {
		return Long.parseLong(readword());
	}

	BufferedReader in;

	BufferedWriter out;

	public String readword() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) {
		try {
			Locale.setDefault(Locale.US);
		} catch (Exception e) {

		}
		// new Thread(new Main()).start();
		new Thread(null, new Main(), "1", 1 << 25).start();
	}
}


import java.io.*;

import java.awt.geom.Point2D;
import java.text.*;
import java.math.*;
import java.util.*;

public class Main implements Runnable {

	final String filename = "";

	public void solve() throws Exception {
		int n = iread(), m = iread();
		int[][] a = new int[n][m];
		for (int i = 0; i < n; i++) {
			String s = readword();
			for (int j = 0; j < m; j++)
				a[i][j] = s.charAt(j) - '0';
		}
		int ans = n * m + 1;
		for (int c = 0; c < m; c++) {
			int res = 0;
			for (int i = 0; i < n; i++) {
				int b = -1;
				for (int j = 0; j < m; j++) {
					if (a[i][j] == 1) {
						int k = Math.min(Math.abs(c - j), m - Math.abs(c - j));
						if (b == -1 || b > k)
							b = k;
					}
				}
				if (b == -1) {
					out.write("-1\n");
					return;
				}
				res += b;
			}
			ans = Math.min(ans, res);
		}
		out.write(ans + "\n");
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new BufferedWriter(new OutputStreamWriter(System.out));
			// in = new BufferedReader(new FileReader(filename+".in"));
			// out = new BufferedWriter(new FileWriter(filename+".out"));
			solve();
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public int iread() throws Exception {
		return Integer.parseInt(readword());
	}

	public double dread() throws Exception {
		return Double.parseDouble(readword());
	}

	public long lread() throws Exception {
		return Long.parseLong(readword());
	}

	BufferedReader in;

	BufferedWriter out;

	public String readword() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ')
			c = in.read();
		if (c < 0)
			return "";
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}

	public static void main(String[] args) {
		try {
			Locale.setDefault(Locale.US);
		} catch (Exception e) {

		}
		// new Thread(new Main()).start();
		new Thread(null, new Main(), "1", 1 << 25).start();
	}
}


import java.io.*;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author samiththa
 */
public class AAsami{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            BufferedReader scan = new BufferedReader (new InputStreamReader (System.in));
                            String s1[]=scan.readLine().split(" ");
                            int r=Integer.parseInt(s1[0]);
                            int c=Integer.parseInt(s1[1]);
                            int[][] num=new int[r][c];
                            int [ ] m=new int[c];
                           for(int i=0;i<r;i++)
                           {

                                for(int j=0;j<c;j++)
                           {
                             num[i][j]= scan.read();

                           }
                           scan.read();}
                           boolean t=true;
                           ABC:
                            for(int i=0;i<c;i++)
                                for(int j=0;j<r;j++)
                                { int left=0,right=0;

                                   int k;
                                   if(i==c-1)
                                    k=0;
                                   else
                                      k= i+1;
                                   if(num[j][i]!=49)

                                   {while(k!=i)
                                   {left++;
                                       if(num[j][k]==49)
                                       {

                                           break;}

                                       if(k==c-1)
                                           k=-1;
                                       k++;
                                   }

                                    if(k==i)
                                    { t=false;
                                        break ABC;}
                                   if(i==0)
                                       k=c-1;
                                   else
                                       k=i-1;

                                   while(k!=i)
                                   { right++;
                                       if(num[j][k]==49)
                                       {
                                           break;}

                                       if(k==0)
                                           k=c;
                                       k--;
                                   }
                                    if(k==i)
                                    {   t=false;
                                        break ABC;}}
                                   if(left<right)
                                       m[i]+=(left);
                                    else
                                       m[i]+=(right);
                                }
                           if(t==false)
                               System.out.println("-1");
                           else
                           {
                               Arrays.sort(m);
                               System.out.println(m[0]);
                           }

        } catch (IOException ex) {
           // Logger.getLogger(JavaApplication9.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


import java.util.Scanner;

public class AA {

    public void solve() {
        Scanner sc = new Scanner(System.in);
        String f[] = sc.nextLine().split(" ");
        int row = Integer.valueOf(f[0]);
        int col = Integer.valueOf(f[1]);
        int i = 0, j = 0;
        int h = 0;
        int a[][] = new int[row][col];
        for (i = 0; i < row; ++i) {
            String s = sc.nextLine();
            h = 0;
            for (j = 0; j < col; ++j) {
                a[i][j] = Integer.valueOf(s.charAt(j) + "");
                if (a[i][j] == 0) {
                    h++;
                }
            }
            if (h == col) {
                System.out.println("-1");
                System.exit(0);
            }
        }

//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(a[i][j] + " ");
//            }
//            System.out.println("");
//        }
//
        int sol[][] = setMat(a,a, row, col);
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (j = 0; j < col; ++j) {
            sum = 0;
            for (i = 0; i < row; ++i) {
                sum += sol[i][j];
            }
            if (min > sum) {
                min = sum;
            }
        }
        System.out.println(min);

    }

    public int[][] setMat(int a[][],int b[][], int row, int col) {

        int i = 0, j = 0, k = 10001, x = 0, y = 0;
        int sol[][] =new int[row][col];

        for(i=0;i<row;++i){
            for(j=0;j<col;++j){
                sol[i][j]=a[i][j];
            }
        }

        int count = 0;
        for (i = 0; i < row ; ++i) {
            count = 0;
            k = 10001;

            for (j = 0; j < col; ++j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;
                    //System.out.println("(i,j) = "+i+" "+j+" a = "+a[i][j]);

                } else {

                    sol[i][j] = ++k;

                }
                if (j == col - 1 && count != 2) {
                    j = -1;
                    count = 2;

                }

            }
        }

//        System.out.println("");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("");
//

        count = 0;

        k = 10001;
        for (i = 2; i < row; ++i) {
            count = 0;
            k = 10001;
            for (j = col - 1; j >= 0; --j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;

                } else {
                    k++;
                    sol[i][j] = Math.min( k, sol[i][j]);

                }
                if (j == 0 && count != 2) {
                    j = col ;
                    count = 2;
                }

            }
        }

//        System.out.println("");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("");
         //System.exit(0);
        return sol;

    }

    public static void main(String[] args) {
        new AA().solve();

    }
}


import java.util.Scanner;

public class AA {

    public void solve() {
        Scanner sc = new Scanner(System.in);
        String f[] = sc.nextLine().split(" ");
        int row = Integer.valueOf(f[0]);
        int col = Integer.valueOf(f[1]);
        int i = 0, j = 0;
        int h = 0;
        int a[][] = new int[row][col];
        for (i = 0; i < row; ++i) {
            String s = sc.nextLine();
            h = 0;
            for (j = 0; j < col; ++j) {
                a[i][j] = Integer.valueOf(s.charAt(j) + "");
                if (a[i][j] == 0) {
                    h++;
                }
            }
            if (h == col) {
                System.out.println("-1");
                System.exit(0);
            }
        }

//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(a[i][j] + " ");
//            }
//            System.out.println("");
//        }
//
        int sol[][] = setMat(a,a, row, col);
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (j = 0; j < col; ++j) {
            sum = 0;
            for (i = 0; i < row; ++i) {
                sum += sol[i][j];
            }
            if (min > sum) {
                min = sum;
            }
        }
        System.out.println(min);

    }

    public int[][] setMat(int a[][],int b[][], int row, int col) {

        int i = 0, j = 0, k = 10001, x = 0, y = 0;
        int sol[][] =new int[row][col];

        for(i=0;i<row;++i){
            for(j=0;j<col;++j){
                sol[i][j]=a[i][j];
            }
        }

        int count = 0;
        for (i = 0; i < row ; ++i) {
            count = 0;
            k = 10001;

            for (j = 0; j < col; ++j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;
                    //System.out.println("(i,j) = "+i+" "+j+" a = "+a[i][j]);

                } else {

                    sol[i][j] = ++k;

                }
                if (j == col - 1 && count != 2) {
                    j = -1;
                    count = 2;

                }

            }
        }

//        System.out.println("");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("");
//

        count = 0;

        k = 10001;
        for (i = 0; i < row; ++i) {
            count = 0;
            k = 10001;
            for (j = col - 1; j >= 0; --j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;

                } else {
                    k++;
                    sol[i][j] = Math.min( k, sol[i][j]);

                }
                if (j == 0 && count != 2) {
                    j = col ;
                    count = 2;
                }

            }
        }

//        System.out.println("");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("");
//         System.exit(0);
        return sol;

    }

    public static void main(String[] args) {
        new AA().solve();

    }
}


import java.io.*;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AAsami {

    public static void main(String[] args) {
        try {
            BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
            String s1[] = scan.readLine().split(" ");
            int r = Integer.parseInt(s1[0]);
            int c = Integer.parseInt(s1[1]);
            int[][] num = new int[r][c];
            int[] m = new int[c];
            for (int i = 0; i < r; i++) {

                for (int j = 0; j < c; j++) {
                    num[i][j] = scan.read();
                   //System.out.println(num[i][j]);
                }
                scan.read();
            }
            boolean t = true;
            ABC:
            for (int i = 0; i < c; i++) {
                for (int j = 0; j < r; j++) {
                    int left = 0, right = 0;

                    int k;
                    if (i == c - 1) {
                        k = 0;
                    } else {
                        k = i + 1;
                    }
                    if (num[j][i] != 49) {
                        while (k != i) {
                            left++;
                            if (num[j][k] == 49) {

                                break;
                            }

                            if (k == c - 1) {
                                k = -1;
                            }
                            k++;
                        }

                        if (k == i) {
                            t = false;
                            break ABC;
                        }
                        if (i == 0) {
                            k = c - 1;
                        } else {
                            k = i - 1;
                        }

                        while (k != i) {
                            right++;
                            if (num[j][k] == 49) {
                                break;
                            }

                            if (k == 0) {
                                k = c;
                            }
                            k--;
                        }
                        if (k == i) {
                            t = false;
                            break ABC;
                        }
                    }
                    if (left < right) {
                        m[i] += (left);
                    } else {
                        m[i] += (right);
                    }
                }
            }
            if (t == false) {
                System.out.println("-1");
            } else {
                Arrays.sort(m);
                System.out.println(m[0]+1);
            }

        } catch (IOException ex) {
            // Logger.getLogger(JavaApplication9.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


import java.util.Scanner;

public class AA {

    public void solve() {
        Scanner sc = new Scanner(System.in);
        String f[] = sc.nextLine().split(" ");
        int row = Integer.valueOf(f[0]);
        int col = Integer.valueOf(f[1]);
        int i = 0, j = 0;
        int h=0;
        int a[][] = new int[row][col];
        for (i = 0; i < row; ++i) {
            String s = sc.nextLine();
            h=0;
            for (j = 0; j < col; ++j) {
                a[i][j] = Integer.valueOf(s.charAt(j) + "");
                if(a[i][j]==0){
                    h++;
                }
            }
            if(h==col){
                System.out.println("-1");
                System.exit(0);
            }
        }

//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(a[i][j] + " ");
//            }
//            System.out.println("");
//        }
//
        int sol[][] = setMat(a, row, col);
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//        for (i = 0; i < row; ++i) {
//            for (j = 0; j < col; ++j) {
//                System.out.print(sol[i][j] + " ");
//            }
//            System.out.println("");
//        }
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

        int min = Integer.MAX_VALUE;
        int sum=0;
        for (j = 0; j < col; ++j) {
            sum=0;
            for(i=0;i<row;++i){
                sum+=sol[i][j];
            }
            if(min>sum){
                min=sum;
            }
        }
        System.out.println(min);

    }

    public int[][] setMat(int a[][], int row, int col) {

        int i = 0, j = 0, k = 0;
        int sol[][] = new int[row][col];

        for (i = 0; i < row; ++i) {
            for (j = 0; j < col; ++j) {
                if (a[i][j] == 1) {
                    sol[i][j] = 0;
                } else {
                    int rcount = 0;
                    int lcount = 0;

                    k = j + 1;
                    if (k == col) {
                        k = 0;
                    }
                    while (k != j && a[i][k]!=1) {

                        if (a[i][k] == 0) {
                            rcount++;
                        }
                        ++k;
                        if (k == col) {
                            k = 0;
                        }

                    }

                    k = j - 1;
                    if (k == -1) {
                        k = col - 1;
                    }
                    while (k != j && a[i][k]!=1) {

                        if (a[i][k] == 0) {
                            lcount++;
                        }
                        --k;
                        if (k == -1) {
                            k = col - 1;
                        }

                    }

                    sol[i][j] = Math.min(rcount, lcount)+1;
                }

            }
        }
        return sol;

    }

    public static void main(String[] args) {
        new AA().solve();

    }
}


import java.util.*;
public class a {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int rows = input.nextInt(), cols = input.nextInt();
    boolean[][] data = new boolean[rows][cols];
    int[] firsts = new int[rows], lasts = new int[rows];
    Arrays.fill(lasts, -1);
    boolean valid = true;
    for(int i = 0; i<rows; i++)
    {
        String str = input.next();
        for(int j = 0; j<cols; j++)
        {
            data[i][j] = str.charAt(j) == '1';
            if(data[i][j])
            {
                lasts[i] = Math.max(lasts[i], j);
            }
        }
        if(lasts[i] == -1)
            valid = false;
    }
    if(!valid)
        System.out.println(-1);
    else
    {
        memo = new int[rows][cols];
        visited = new boolean[rows+1][cols];
        for(int i = 0; i<rows; i++)
            Arrays.fill(memo[i], -1);
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                int num = go(j, data, i);
            }
            for(int k = lasts[i]; k<data[0].length; k++)
                memo[i][k] = (k-lasts[i]);
        }
        int min = 99999999;
        for(int i = 0; i<cols; i++)
        {
            int sum = 0;
            for(int j = 0; j<rows; j++)
            {
                sum += memo[j][i];
            }
            //System.out.println(i + " " + sum);
            min = Math.min(min, sum);
        }
        System.out.println(min);
    }

}
public static int[][] memo;
public static boolean[][] visited;
public static int go(int at, boolean[][] data, int row)
{
    if(at < 0 || at >= data[0].length)
        return memo[row][at] = 99999;
    visited[row][at] = true;
    if(memo[row][at] != -1)
        return memo[row][at];
    if(data[row][at])
        return memo[row][at] = 0;
    int res = 99999;
    if(at < data[0].length-1 && (!visited[row][at+1] || memo[row][at+1] != -1))
        res = Math.min(res, 1+go(at+1, data, row));
    if(at > 0 && (!visited[row][at-1] || memo[row][at-1] != -1))
        res = Math.min(res, 1+go(at-1, data, row));
    return memo[row][at] = res;
}
}


import java.util.*;
public class a {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int rows = input.nextInt(), cols = input.nextInt();
    boolean[][] data = new boolean[rows][cols];
    int[] firsts = new int[rows], lasts = new int[rows];
    Arrays.fill(lasts, -1);
    Arrays.fill(firsts, 999999);
    boolean valid = true;
    for(int i = 0; i<rows; i++)
    {
        String str = input.next();
        for(int j = 0; j<cols; j++)
        {
            data[i][j] = str.charAt(j) == '1';
            if(data[i][j])
            {

                lasts[i] = Math.max(lasts[i], j);
                firsts[i] = Math.min(firsts[i], j);
            }
        }
        if(lasts[i] == -1)
            valid = false;
    }
    if(!valid)
        System.out.println(-1);
    else
    {
        memo = new int[rows][cols];
        visited = new boolean[rows+1][cols];
        for(int i = 0; i<rows; i++)
            Arrays.fill(memo[i], -1);
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                int num = go(j, data, i);
            }
            for(int k = lasts[i]; k<data[0].length; k++)
                memo[i][k] = Math.min((k-lasts[i]), firsts[i]+memo[i].length-k);
        }
        int min = 99999999;
        for(int i = 0; i<cols; i++)
        {
            int sum = 0;
            for(int j = 0; j<rows; j++)
            {
                sum += memo[j][i];
            }
            //System.out.println(i + " " + sum);
            min = Math.min(min, sum);
        }
        System.out.println(min);
    }

}
public static int[][] memo;
public static boolean[][] visited;
public static int go(int at, boolean[][] data, int row)
{
    int n = data[0].length;
    if(at < 0 || at >= data[0].length)
        return memo[row][at] = 99999;
    visited[row][at] = true;
    if(memo[row][at] != -1)
        return memo[row][at];
    if(data[row][at])
        return memo[row][at] = 0;
    int res = 99999;
    if((!visited[row][(at+1)%n] || memo[row][(at+1)%n] != -1))
        res = Math.min(res, 1+go((at+1)%n, data, row));
    if((!visited[row][(at-1+n)%n] || memo[row][(at-1+n)%n] != -1))
        res = Math.min(res, 1+go((at-1+n)%n, data, row));
    return memo[row][at] = res;
}
}


import java.util.*;
import java.io.*;
public class a {
  static long mod = 1000000007;
  static boolean[][] blacks;
public static void main(String[] args) throws IOException
{
  input.init(System.in);
  PrintWriter out = new PrintWriter(new PrintStream(System.out));
  //Scanner input = new Scanner(new File("input.txt"));
  //PrintWriter out = new PrintWriter(new File("output.txt"));

  int n = input.nextInt(), m = input.nextInt();
  boolean[][] ones = new boolean[n][m];
  boolean okay = true;
  for(int i = 0; i<n; i++)
  {
    String s = input.next();
    okay &= s.indexOf('1') > -1;
    for(int j = 0; j<m; j++) ones[i][j] = s.charAt(j) == '1';
  }
  if(!okay) out.println(-1);
  else
  {
    int[] tot = new int[m];
    for(int i = 0; i<n; i++)
    {
      int[] left = new int[m], right = new int[m];
      left[0] = ones[i][0] ? 0 : 100000;
      for(int j = 1; j<m; j++)
        if(ones[i][j]) left[j] = 0;
        else left[j] = left[j-1] == 100000 ? 100000 : 1+left[j-1];
      for(int j = 0; j<m; j++) left[j] = Math.min(left[j], j+1+left[m-1]);
      right[m-1] = ones[i][m-1] ? 0 : 100000;
      for(int j = m-2; j>=0; j--)
        if(ones[i][j]) right[j] = 0;
        else right[j] = right[j+1] == 100000 ? 100000 : 1+right[j+1];
      for(int j = m-1; j>=0; j--) right[j] = Math.min(right[j], m-j+right[0]);
      for(int j = 0; j<m; j++)
      {
        if(left[j] == -1) tot[j] += right[j];
        else if(right[j] == -1) tot[j] += left[j];
        else tot[j] += Math.min(left[j], right[j]);
      }
    }
    //for(int t: tot) out.println(t);
    int min = tot[0];
    for(int i = 1; i<m; i++) min = Math.min(min, tot[i]);
    out.println(min);
  }

  out.close();
}
static long pow(long a, long p)
{
  if(p==0) return 1;
  if((p&1) == 0)
  {
    long sqrt = pow(a, p/2);
    return (sqrt*sqrt)%mod;
  }
  else
    return (a*pow(a,p-1))%mod;
}
static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

  static int nextInt() throws IOException {
      return Integer.parseInt( next() );
  }
  static long nextLong() throws IOException {
    return Long.parseLong( next() );
}

  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static String nextLine() throws IOException {
    return reader.readLine();
  }
}
}


import java.util.*;
import java.io.*;
public class a {
  static long mod = 1000000007;
  static boolean[][] blacks;
public static void main(String[] args) throws IOException
{
  input.init(System.in);
  PrintWriter out = new PrintWriter(new PrintStream(System.out));
  //Scanner input = new Scanner(new File("input.txt"));
  //PrintWriter out = new PrintWriter(new File("output.txt"));

  int n = input.nextInt(), m = input.nextInt();
  boolean[][] ones = new boolean[n][m];
  boolean okay = true;
  for(int i = 0; i<n; i++)
  {
    String s = input.next();
    okay &= s.indexOf('1') > -1;
    for(int j = 0; j<m; j++) ones[i][j] = s.charAt(j) == '1';
  }
  if(!okay) out.println(-1);
  else
  {
    int[] tot = new int[m];
    for(int i = 0; i<n; i++)
    {
      int[] left = new int[m], right = new int[m];
      left[0] = ones[i][0] ? 0 : -1;
      for(int j = 1; j<m; j++)
        if(ones[i][j]) left[j] = 0;
        else left[j] = left[j-1] == -1 ? -1 : 1+left[j-1];
      right[m-1] = ones[i][m-1] ? 0 : -1;
      for(int j = m-2; j>=0; j--)
        if(ones[i][j]) right[j] = 0;
        else right[j] = right[j+1] == -1 ? -1 : 1+right[j+1];
      for(int j = 0; j<m; j++)
      {
        if(left[j] == -1) tot[j] += right[j];
        else if(right[j] == -1) tot[j] += left[j];
        else tot[j] += Math.min(left[j], right[j]);
      }
    }
    //for(int t: tot) out.println(t);
    int min = tot[0];
    for(int i = 1; i<m; i++) min = Math.min(min, tot[i]);
    out.println(min);
  }

  out.close();
}
static long pow(long a, long p)
{
  if(p==0) return 1;
  if((p&1) == 0)
  {
    long sqrt = pow(a, p/2);
    return (sqrt*sqrt)%mod;
  }
  else
    return (a*pow(a,p-1))%mod;
}
static class input {
  static BufferedReader reader;
  static StringTokenizer tokenizer;

  /** call this method to initialize reader for InputStream */
  static void init(InputStream input) {
      reader = new BufferedReader(
                   new InputStreamReader(input) );
      tokenizer = new StringTokenizer("");
  }

  /** get next word */
  static String next() throws IOException {
      while ( ! tokenizer.hasMoreTokens() ) {
          //TODO add check for eof if necessary
          tokenizer = new StringTokenizer(
                 reader.readLine() );
      }
      return tokenizer.nextToken();
  }

  static int nextInt() throws IOException {
      return Integer.parseInt( next() );
  }
  static long nextLong() throws IOException {
    return Long.parseLong( next() );
}

  static double nextDouble() throws IOException {
      return Double.parseDouble( next() );
  }
  static String nextLine() throws IOException {
    return reader.readLine();
  }
}
}


import java.util.*;
public class a {
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int rows = input.nextInt(), cols = input.nextInt();
    boolean[][] data = new boolean[rows][cols];
    int[] firsts = new int[rows], lasts = new int[rows];
    Arrays.fill(lasts, -1);
    boolean valid = true;
    for(int i = 0; i<rows; i++)
    {
        String str = input.next();
        for(int j = 0; j<cols; j++)
        {
            data[i][j] = str.charAt(j) == '1';
            if(data[i][j])
            {
                lasts[i] = Math.max(lasts[i], j);
            }
        }
        if(lasts[i] == -1)
            valid = false;
    }
    if(!valid)
        System.out.println(-1);
    else
    {
        memo = new int[rows][cols];
        visited = new boolean[rows+1][cols];
        for(int i = 0; i<rows; i++)
            Arrays.fill(memo[i], -1);
        for(int i = 0; i<rows; i++)
        {
            for(int j = 0; j<cols; j++)
            {
                int num = go(j, data, i);
            }
            for(int k = lasts[i]; k<data[0].length; k++)
                memo[i][k] = (k-lasts[i]);
        }
        int min = 99999999;
        for(int i = 0; i<cols; i++)
        {
            int sum = 0;
            for(int j = 0; j<rows; j++)
            {
                sum += memo[j][i];
            }
            //System.out.println(i + " " + sum);
            min = Math.min(min, sum);
        }
        System.out.println(min);
    }

}
public static int[][] memo;
public static boolean[][] visited;
public static int go(int at, boolean[][] data, int row)
{
    int n = data[0].length;
    if(at < 0 || at >= data[0].length)
        return memo[row][at] = 99999;
    visited[row][at] = true;
    if(memo[row][at] != -1)
        return memo[row][at];
    if(data[row][at])
        return memo[row][at] = 0;
    int res = 99999;
    if((!visited[row][(at+1)%n] || memo[row][(at+1)%n] != -1))
        res = Math.min(res, 1+go((at+1)%n, data, row));
    if((!visited[row][(at-1+n)%n] || memo[row][(at-1+n)%n] != -1))
        res = Math.min(res, 1+go((at-1+n)%n, data, row));
    return memo[row][at] = res;
}
}


import java.util.Scanner;

public class C {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matr = new int[n][m];

        boolean zeroLine=false;

        f: for(int i=0;i<n;i++){
            String line = in.next();
            boolean[] lineb = new boolean[m];
            for(int j=0;j<lineb.length;j++)
                if(line.charAt(j)=='1')
                    lineb[j] = true;

            //-------------------
            int prev=-1;
            for(int j=0; j<lineb.length;j++){
                if(lineb[j]){
                    matr[i][j]=0;
                    if(prev == -1){
                        for(int k=j-1;k>=0;k--)
                            matr[i][k]=matr[i][k+1]+1;
                        prev=j;
                    }
                    else{
                        for(int k=1;k<=(j-prev)/2;k++){
                            matr[i][prev+k]=matr[i][prev+k-1]+1;
                            matr[i][j-k]=matr[i][j-k+1]+1;
                        }
                        prev = j;
                    }
                }
            }
            if(prev == -1){
                zeroLine=true;
                break f;
            }

            for(int k=prev+1;k<m;k++)
                matr[i][k]=matr[i][k-1]+1;
        }
        if(zeroLine){
            System.out.println("-1");
            return;
        }

        int[] sum = new int[m];

        for(int i=0;i<m;i++){
            int s = 0;
            for(int j=0;j<n;j++)
                s+=matr[j][i];
            sum[i] = s;
        }

        int min = sum[0];
        for(int i=1;i<m;i++)
            if(min>sum[i])
                min=sum[i];
        System.out.println(min);
    }
}


import java.util.Scanner;

public class C {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] matr = new int[n][m];

        boolean zeroLine=false;

        f: for(int i=0;i<n;i++){
            String line = in.next();
            boolean[] lineb = new boolean[m];
            for(int j=0;j<lineb.length;j++)
                if(line.charAt(j)=='1')
                    lineb[j] = true;

            //-------------------
            int prev=-1;
            int first=-1;
            for(int j=0; j<lineb.length;j++){
                if(lineb[j]){
                    matr[i][j]=0;
                    if(prev == -1){
                        for(int k=j-1;k>=0;k--)
                            matr[i][k]=matr[i][k+1]+1;
                        prev=j;
                        first = j;
                    }
                    else{
                        for(int k=1;k<=(j-prev)/2;k++){
                            matr[i][prev+k]=matr[i][prev+k-1]+1;
                            matr[i][j-k]=matr[i][j-k+1]+1;
                        }
                        prev = j;
                    }
                }
            }
            if(prev == -1){
                zeroLine=true;
                break f;
            }

            for(int k=prev+1;k<m;k++)
                matr[i][k]=matr[i][k-1]+1;

            if(m-1-prev>first){
                int curr = m-1;
                matr[i][curr]=matr[i][0]+1;
                while(matr[i][curr]+1<matr[i][curr-1]){
                    matr[i][curr-1]=matr[i][curr]+1;
                    curr--;
                }
            }
            else if(m-1-prev<first){
                int curr=0;
                matr[i][curr]=matr[i][m-1]+1;
                while(matr[i][curr]+1<matr[i][curr+1]){
                    matr[i][curr+1]=matr[i][curr]+1;
                    curr++;
                }
            }
        }
        if(zeroLine){
            System.out.println("-1");
            return;
        }

        int[] sum = new int[m];

        for(int i=0;i<m;i++){
            int s = 0;
            for(int j=0;j<n;j++)
                s+=matr[j][i];
            sum[i] = s;
        }

        int min = sum[0];
        for(int i=1;i<m;i++)
            if(min>sum[i])
                min=sum[i];
        System.out.println(min);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class C230 {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        String[] t = in.readLine().split(" ");
        int n = Integer.valueOf(t[0]);
        int m = 2*Integer.valueOf(t[1]);
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) {
            String s = in.readLine();
            map[i] = (s + s).toCharArray();
        }
        int[] a = new int[m];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) {
            int left = -1;
            int right = m;
            for (int j = 0; j < m; j++) {
                if (map[i][j] == '1') {
                    b[j] = 0;
                    left = j;
                }
                else {
                    if (left == -1) b[j] = Integer.MAX_VALUE;
                    else b[j] = j - left;
                }
            }
            for (int j = m-1; j >= 0; j--) {
                if (map[i][j] == '1') {
                    b[j] = 0;
                    right = j;
                }
                else {
                    if (right == m) b[j] = Math.min(b[j], Integer.MAX_VALUE);
                    else b[j] = Math.min(b[j], right - j);
                }
            }
            for (int j = 0; j < m; j++)
                if (b[j] == Integer.MAX_VALUE) a[j] = Integer.MAX_VALUE;
                else if (a[j] != Integer.MAX_VALUE) a[j] += b[j];
        }

        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) ans = Math.min(ans, a[i]);
        out.println(ans == Integer.MAX_VALUE ? -1 : ans);

        out.flush();
    }

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author niyaznigmatul
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		FastPrinter out = new FastPrinter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
	public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        char[][] c = new char[n][];
        for (int i = 0; i < n; i++) {
            c[i] = in.next().toCharArray();
        }
        int answer = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) {
            int best = 0;
            for (int j = 0; j < n; j++) {
                int curBest = Integer.MAX_VALUE;
                for (int k = 0; k < m; k++) {
                    if (c[j][k] == '1') {
                        curBest = Math.min(curBest, Math.abs(k - i));
                        curBest = Math.min(curBest, m - Math.abs(k - i));
                    }
                }
                if (curBest == Integer.MAX_VALUE) {
                    best = Integer.MAX_VALUE;
                    break;
                }
                best += curBest;
            }
            answer = Math.min(answer, best);
        }
        out.println(answer == Integer.MAX_VALUE ? -1 : answer);
	}
}

class FastScanner extends BufferedReader {

    boolean isEOF;

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public int read() {
        try {
            int ret = super.read();
            if (isEOF && ret < 0) {
                throw new InputMismatchException();
            }
            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public String next() {
        StringBuilder sb = new StringBuilder();
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        while (!isWhiteSpace(c)) {
            sb.appendCodePoint(c);
            c = read();
        }
        return sb.toString();
    }

    static boolean isWhiteSpace(int c) {
        return c >= -1 && c <= 32;
    }

    public int nextInt() {
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int ret = 0;
        while (!isWhiteSpace(c)) {
            if (c < '0' || c > '9') {
                throw new NumberFormatException("digit expected " + (char) c
                        + " found");
            }
            ret = ret * 10 + c - '0';
            c = read();
        }
        return ret * sgn;
    }

    }

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author niyaznigmatul
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		FastPrinter out = new FastPrinter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskA {
    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        char[][] c = new char[n][];
        for (int i = 0; i < n; i++) {
            c[i] = in.next().toCharArray();
        }
        int answer = Integer.MAX_VALUE;
        int[][] left = getLeft(n, m, c);
        if (left == null) {
            out.println(-1);
            return;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0, k = c[i].length - 1; j < k; j++, k--) {
                char t = c[i][j];
                c[i][j] = c[i][k];
                c[i][k] = t;
            }
        }
        int[][] right = getLeft(n, m, c);
        for (int i = 0; i < n; i++) {
            for (int j = 0, k = right[i].length - 1; j < k; j++, k--) {
                int t = right[i][j];
                right[i][j] = right[i][k];
                right[i][k] = t;
            }
        }
        for (int i = 0; i < m; i++) {
            int best = 0;
            for (int j = 0; j < n; j++) {
                int curBest = Integer.MAX_VALUE;
                curBest = Math.min(curBest, left[j][i]);
                curBest = Math.min(curBest, right[j][i]);
                if (curBest == Integer.MAX_VALUE) {
                    best = Integer.MAX_VALUE;
                    break;
                }
                best += curBest;
            }
            answer = Math.min(answer, best);
        }
        out.println(answer == Integer.MAX_VALUE ? -1 : answer);
    }

    private static int[][] getLeft(int n, int m, char[][] c) {
        int[][] left = new int[n][m];
        for (int i = 0; i < n; i++) {
            int z = m - 1;
            while (z >= 0 && c[i][z] == '0') {
                --z;
            }
            if (z < 0) {
                return null;
            }
            left[i][0] = c[i][0] == '1' ? 0 : m - z;
            for (int j = 1; j < m; j++) {
                if (c[i][j] == '1') {
                    left[i][j] = 0;
                } else {
                    left[i][j] = left[i][j - 1] + 1;
                }
            }
        }
        return left;
    }
}

class FastScanner extends BufferedReader {

    boolean isEOF;

    public FastScanner(InputStream is) {
        super(new InputStreamReader(is));
    }

    public int read() {
        try {
            int ret = super.read();
            if (isEOF && ret < 0) {
                throw new InputMismatchException();
            }
            isEOF = ret == -1;
            return ret;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

    public String next() {
        StringBuilder sb = new StringBuilder();
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        while (!isWhiteSpace(c)) {
            sb.appendCodePoint(c);
            c = read();
        }
        return sb.toString();
    }

    static boolean isWhiteSpace(int c) {
        return c >= -1 && c <= 32;
    }

    public int nextInt() {
        int c = read();
        while (isWhiteSpace(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int ret = 0;
        while (!isWhiteSpace(c)) {
            if (c < '0' || c > '9') {
                throw new NumberFormatException("digit expected " + (char) c
                        + " found");
            }
            ret = ret * 10 + c - '0';
            c = read();
        }
        return ret * sgn;
    }

    }

class FastPrinter extends PrintWriter {

    public FastPrinter(OutputStream out) {
        super(out);
    }

    public FastPrinter(Writer out) {
        super(out);
    }

}


import java.util.Scanner;

public class AA {

    public void solve() {
        Scanner sc = new Scanner(System.in);
        String f[] = sc.nextLine().split(" ");
        int row = Integer.valueOf(f[0]);
        int col = Integer.valueOf(f[1]);
        int i = 0, j = 0;
        int h = 0;
        int a[][] = new int[row][col];
        for (i = 0; i < row; ++i) {
            String s = sc.nextLine();
            h = 0;
            for (j = 0; j < col; ++j) {
                a[i][j] = Integer.valueOf(s.charAt(j) + "");
                if (a[i][j] == 0) {
                    h++;
                }
            }
            if (h == col) {
                System.out.println("-1");
                System.exit(0);
            }
        }

        int sol[][] = setMat(a,a, row, col);

        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (j = 0; j < col; ++j) {
            sum = 0;
            for (i = 0; i < row; ++i) {
                sum += sol[i][j];
            }
            if (min > sum) {
                min = sum;
            }
        }
        System.out.println(min);

    }

    public int[][] setMat(int a[][],int b[][], int row, int col) {

        int i = 0, j = 0, k = 10001, x = 0, y = 0;
        int sol[][] =new int[row][col];

        for(i=0;i<row;++i){
            for(j=0;j<col;++j){
                sol[i][j]=a[i][j];
            }
        }

        int count = 0;
        for (i = 0; i < row ; ++i) {
            count = 0;
            k = 10001;

            for (j = 0; j < col; ++j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;
                    //System.out.println("(i,j) = "+i+" "+j+" a = "+a[i][j]);

                } else {

                    sol[i][j] = ++k;

                }
                if (j == col - 1 && count != 2) {
                    j = -1;
                    count = 2;

                }

            }
        }

        count = 0;

        k = 10001;
        for (i = 0; i < row; ++i) {
            count = 0;
            k = 10001;
            for (j = col - 1; j >= 0; --j) {

                if ((sol[i][j] == 1 || sol[i][j] == 0) && a[i][j] == 1) {
                    k = 0;
                    sol[i][j] = k;

                } else {
                    k++;
                    sol[i][j] = Math.min( k, sol[i][j]);

                }
                if (j == 0 && count != 2) {
                    j = col ;
                    count = 2;
                }

            }
        }

        return sol;

    }

    public static void main(String[] args) {
        new AA().solve();

    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
      //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
        boolean found = true;
        int[] cost = new int[m];
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();
            int[] temp = new int[m];
            Arrays.fill(temp, Integer.MAX_VALUE);
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    found = true;
                    for (int k = 0; k < m; k++) {
                        int a = j - k;
                        if (a < 0) {
                            a += m;
                        }
                        int b = k - j;
                        if (b < 0) {
                            b += m;
                        }
                        temp[k] = Math.min(Math.min(a, b), temp[k]);
                    }
                }
            }
            for (int j = 0; j < m; j++) {
                cost[j] += temp[j];
            }
        }
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                result = Math.min(result, cost[i]);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.println();
//        }
        boolean found = true;
        TreeSet<Integer>[] list = new TreeSet[n];
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();
            list[i] = new TreeSet();
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    found = true;
                    list[i].add(j);
                }
            }

        }
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    Integer a = list[j].lower(i);
                    Integer b = list[j].ceiling(i);
                    int min = Integer.MAX_VALUE;
                    if (a != null) {

                        min = i - a.intValue();
                        min = Math.min(a.intValue() - i + m, min);
                    }
                    if (b != null) {
                        min = Math.min(b.intValue() - i, min);
                        min = Math.min(i - b.intValue() + m, min);
                    }
                    min = Math.min(min, i - list[j].last().intValue() + m);
                    min = Math.min(min, list[j].first().intValue() - i + m);
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.println();
//        }
        boolean found = true;
        TreeSet<Integer>[] list = new TreeSet[n];
        int[] first = new int[n];
        int[] last = new int[n];
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();
            list[i] = new TreeSet();
            first[i] = -1;
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    if (first[i] == - 1) {
                        first[i] = j;
                    }
                    last[i] = j;
                    found = true;
                    list[i].add(j);
                }
            }

        }
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    Integer a = list[j].lower(i);
                    Integer b = list[j].ceiling(i);
                    int min = Integer.MAX_VALUE;
                    if (a != null) {

                        min = i - a.intValue();
                        min = Math.min(a.intValue() - i + m, min);
                    }
                    if (b != null) {
                        min = Math.min(b.intValue() - i, min);
                        min = Math.min(i - b.intValue() + m, min);
                    }
                    min = Math.min(min , first[j] - i + m);
                    min = Math.min(min , i - last[j] + m);
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n ; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.print("\n");
//        }

        boolean found = true;
        int[] index = new int[n];
        int[][] map = new int[n][m];
        //System.out.println("HE HE");
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();

            //System.out.println(i);
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    found = true;
                    map[i][index[i]++] = j;
                }
            }

        }
        //  System.out.println("HE HE");
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    int s = 0;
                    int e = index[j] - 1;
                    int a = -1;
                    int b = -1;
                    while (s + 1 < e) {
                        //   System.out.println(s + " " + e);
                        int mid = (s + e) / 2;
                        if (map[j][mid] > i) {
                            e = mid - 1;
                        } else if (map[j][mid] == i) {
                            s = mid;
                            break;
                        } else {
                            s = mid;
                        }
                    }
                    a = map[j][s];
                    s = 0;
                    e = index[j] - 1;

                    while (s < e) {

                        int mid = (s + e) / 2;

                        if (map[j][mid] > i) {
                            e = mid;
                        } else if (map[j][mid] == i) {
                            s = mid;
                            break;
                        } else {
                            s = mid + 1;
                        }
                    }
                    b = map[j][s];

                    //  System.out.println(a + " " + b);

                    int min = Integer.MAX_VALUE;
                    int c = i - a;
                    if (c < 0) {
                        c += m;
                    }
                    min = Math.min(c, min);
                    c = a - i;
                    if (c < 0) {
                        c += m;
                    }
                    min = Math.min(c, min);
                    c = b - i;
                    if (c < 0) {
                        c += m;
                    }
                    min = Math.min(c, min);
                    c =  i - b;
                    if (c < 0) {
                        c += m;
                    }
                    min = Math.min(c, min);
                    //  System.out.println(i + " " + j);
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n ; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.print("\n");
//        }
        boolean found = true;
        int [] index = new int[n];
        int [][]map = new int[n][m];
        for (int i = 0; i < n && found; i++) {
            found = false;
            char[] line = in.next().toCharArray();

            System.out.println(i);
            for (int j = 0; j < m; j++) {
                if (line[j] == '1') {
                    found = true;
                    map[i][index[i]++] = j;
                }
            }

        }
        System.out.println("HE HE");
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    int s = 0;
                    int e = index[j] - 1;
                    int a = -1;
                    int b = -1;
                    while(s < e){
                        int mid = (s + e)/2;
                        if(map[j][mid]> i){
                            e = mid - 1;
                        }else if (map[j][mid] == i){
                            s = mid;
                            break;
                        }else{
                            s = mid;
                        }
                    }
                    a = s;
                    s = 0;
                    e = index[j] - 1;
                    while(s < e){
                        int mid = (s + e)/2;
                        if(map[j][mid]> i){
                            e = mid;
                        }else if (map[j][mid] == i){
                            s = mid;
                            break;
                        }else{
                            s = mid + 1;
                        }
                    }
                    b = s;

                    int min = Math.min(i - a, b - i);
                  //  System.out.println(i + " " + j);
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.println();
//        }
        boolean found = true;
        TreeSet<Integer>[] list = new TreeSet[n];
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();
            list[i] = new TreeSet();
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    found = true;
                    list[i].add(j);
                }
            }

        }
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    Integer a = list[j].lower(i);
                    Integer b = list[j].ceiling(i);
                    int min = Integer.MAX_VALUE;
                    if (a != null) {
                        min = i - a.intValue();
                    }
                    if (b != null) {
                        min = Math.min(b.intValue() - i, min);
                    }
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 * 111118315581
 *
 * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3
 *
 * @author pttrung
 */
public class C {

    //   public static long x, y, gcd;
    //   public static int Mod = 1000000007;
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner();
        PrintWriter out = new PrintWriter(System.out);
        //  PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt")));
        int n = in.nextInt();
        int m = in.nextInt();
//        for(int i = 0; i < n; i++){
//            for(int j = 0; j < m; j++){
//                System.out.print(1);
//            }
//            System.out.println();
//        }
        boolean found = true;
        TreeSet<Integer>[] list = new TreeSet[n];
        for (int i = 0; i < n && found; i++) {
            found = false;
            String line = in.next();
            list[i] = new TreeSet();
            for (int j = 0; j < m; j++) {
                if (line.charAt(j) == '1') {
                    found = true;
                    list[i].add(j);
                }
            }

        }
        if (!found) {
            out.println(-1);
        } else {
            int result = Integer.MAX_VALUE;
            for (int i = 0; i < m; i++) {
                int temp = 0;
                for (int j = 0; j < n; j++) {
                    Integer a = list[j].lower(i);
                    Integer b = list[j].ceiling(i);
                    int min = Integer.MAX_VALUE;
                    if (a != null) {

                        min = i - a.intValue();
                        min = Math.min(a.intValue() - i + m, min);
                    }
                    if (b != null) {
                        min = Math.min(b.intValue() - i, min);
                        min = Math.min(i -b.intValue() +  m, min);
                    }
                    temp += min;
                }
                result = Math.min(temp, result);
            }
            out.println(result);
        }

        out.close();
    }

    public static long pow(long a, long b) {
        if (b == 0) {
            return 1;
        }
        long val = pow(a, b / 2);
        if (b % 2 == 0) {
            return val * val;
        } else {
            return val * val * a;
        }
    }

    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

//    public static void extendEuclid(long a, long b) {
//        if (b == 0) {
//            x = 1;
//            y = 0;
//            gcd = a;
//            return;
//        }
//        extendEuclid(b, a % b);
//        long x1 = y;
//        long y1 = x - (a / b) * y;
//        x = x1;
//        y = y1;
//
//    }
    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}