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];
                    if(b < i){
                        b = -1;
                    }
                    if(a > i){
                        a = -1;
                    }
                  //  System.out.println(a + " " + b);

                    int min = Integer.MAX_VALUE ;
                    if(a != -1){
                        min = i - a;
                    }
                    if(b != -1){
                        min = Math.min(min , 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.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
/**
 *
 * @author calcsaransh
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            Parserdoubt pd=new Parserdoubt(System.in);
            PrintWriter pw=new PrintWriter(System.out);
            int n=pd.nextInt();
            int m=pd.nextInt();
            char arr[][]=new char[n][m];
            for(int i=0;i<arr.length;i++)arr[i]=pd.nextString().toCharArray();
            int closestleft[][]=new int[n][m];
            int closestright[][]=new int[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<closestleft[i].length;j++)
                {
                    if(j==0)closestleft[i][j]=Integer.MAX_VALUE;
                    else closestleft[i][j]=closestleft[i][j-1];
                    if(arr[i][j]=='1')
                        closestleft[i][j]=j;
                }
                for(int j=m-1;j>=0;j--)
                {
                    if(j==m-1)closestright[i][j]=Integer.MAX_VALUE;
                    else closestright[i][j]=closestright[i][j+1];
                    if(arr[i][j]=='1')
                        closestright[i][j]=j;
                }
            }
            long min=Long.MAX_VALUE;
            for(int i=0;i<m;i++)
            {
                long res=0;
                for(int j=0;j<n;j++)
                {
                    res+=Math.min(Math.abs(closestright[j][i]-i), Math.abs(closestleft[j][i]-i));
                }
                min=Math.min(min,res);
            }
            if(min>=Integer.MAX_VALUE/2)
                System.out.println(-1);
            else
                System.out.println(min);
            pw.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

}

class Parserdoubt {

    final private int BUFFER_SIZE = 1 << 17;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Parserdoubt(InputStream in) {
        din = new DataInputStream(in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextString() throws Exception {
        StringBuffer sb = new StringBuffer("");
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        do {
            sb.append((char) c);
            c = read();
        } while (c > ' ');
        return sb.toString();
    }

    public char nextChar() throws Exception {
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        return (char) c;
    }

    public int nextInt() throws Exception {
        int ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    public long nextLong() throws Exception {
        long ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    private void fillBuffer() throws Exception {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) {
            buffer[0] = -1;
        }
    }

    private byte read() throws Exception {
        if (bufferPointer == bytesRead) {
            fillBuffer();
        }
        return buffer[bufferPointer++];
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
/**
 *
 * @author calcsaransh
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            Parserdoubt pd=new Parserdoubt(System.in);
            PrintWriter pw=new PrintWriter(System.out);
            int n=pd.nextInt();
            int m=pd.nextInt();
            char arr[][]=new char[n][m];
            boolean f=true;
            for(int i=0;i<arr.length;i++)
            {
                String str=pd.nextString();
                arr[i]=str.toCharArray();
                if(!str.contains("1"))f=false;
            }
            if(!f)
            {
                System.out.println(-1);
                return;
            }
            int closestleft[][]=new int[n][m];
            int leftmost[]=new int[n];
            Arrays.fill(leftmost, Integer.MAX_VALUE/2);
            int rightmost[]=new int[n];
            int closestright[][]=new int[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<closestleft[i].length;j++)
                {
                    if(j==0)closestleft[i][j]=Integer.MAX_VALUE;
                    else closestleft[i][j]=closestleft[i][j-1];
                    if(arr[i][j]=='1')
                    {
                        closestleft[i][j]=j;
                        leftmost[i]=Math.min(leftmost[i],j);
                    }
                }
                for(int j=m-1;j>=0;j--)
                {
                    if(j==m-1)closestright[i][j]=Integer.MAX_VALUE;
                    else closestright[i][j]=closestright[i][j+1];
                    if(arr[i][j]=='1')
                    {
                        closestright[i][j]=j;
                        rightmost[i]=Math.max(rightmost[i],j);
                    }
                }
            }
            long min=Long.MAX_VALUE;
            for(int i=0;i<m;i++)
            {
                long res=0;
                for(int j=0;j<n;j++)
                {
                    res+=min(Math.abs(closestright[j][i]-i), Math.abs(closestleft[j][i]-i),leftmost[j]+m-i,m-rightmost[j]+i);
                }
                min=Math.min(min,res);
            }
            if(min>=Integer.MAX_VALUE/2)
                System.out.println(-1);
            else
                System.out.println(min);
            pw.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static int min(int a,int b,int c,int d)
    {
        int arr[]={a,b,c,d};
        Arrays.sort(arr);
        return arr[0];
    }

}

class Parserdoubt {

    final private int BUFFER_SIZE = 1 << 17;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Parserdoubt(InputStream in) {
        din = new DataInputStream(in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextString() throws Exception {
        StringBuffer sb = new StringBuffer("");
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        do {
            sb.append((char) c);
            c = read();
        } while (c > ' ');
        return sb.toString();
    }

    public char nextChar() throws Exception {
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        return (char) c;
    }

    public int nextInt() throws Exception {
        int ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    public long nextLong() throws Exception {
        long ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    private void fillBuffer() throws Exception {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) {
            buffer[0] = -1;
        }
    }

    private byte read() throws Exception {
        if (bufferPointer == bytesRead) {
            fillBuffer();
        }
        return buffer[bufferPointer++];
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
/**
 *
 * @author calcsaransh
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            Parserdoubt pd=new Parserdoubt(System.in);
            PrintWriter pw=new PrintWriter(System.out);
            int n=pd.nextInt();
            int m=pd.nextInt();
            char arr[][]=new char[n][m];
            boolean f=true;
            for(int i=0;i<arr.length;i++)
            {
                String str=pd.nextString();
                arr[i]=str.toCharArray();
                if(!str.contains("1"))f=false;
            }
            if(!f)
            {
                System.out.println(-1);
                return;
            }
            int closestleft[][]=new int[n][m];
            int leftmost[]=new int[n];
            int rightmost[]=new int[n];
            int closestright[][]=new int[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<closestleft[i].length;j++)
                {
                    if(j==0)closestleft[i][j]=Integer.MAX_VALUE;
                    else closestleft[i][j]=closestleft[i][j-1];
                    if(arr[i][j]=='1')
                    {
                        closestleft[i][j]=j;
                        leftmost[i]=Math.min(leftmost[i],j);
                    }
                }
                for(int j=m-1;j>=0;j--)
                {
                    if(j==m-1)closestright[i][j]=Integer.MAX_VALUE;
                    else closestright[i][j]=closestright[i][j+1];
                    if(arr[i][j]=='1')
                    {
                        closestright[i][j]=j;
                        rightmost[i]=Math.max(rightmost[i],j);
                    }
                }
            }
            long min=Long.MAX_VALUE;
            for(int i=0;i<m;i++)
            {
                long res=0;
                for(int j=0;j<n;j++)
                {
                    res+=min(Math.abs(closestright[j][i]-i), Math.abs(closestleft[j][i]-i),leftmost[j]+m-i,m-rightmost[j]+i);
                }
                min=Math.min(min,res);
            }
            if(min>=Integer.MAX_VALUE/2)
                System.out.println(-1);
            else
                System.out.println(min);
            pw.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static int min(int a,int b,int c,int d)
    {
        int arr[]={a,b,c,d};
        Arrays.sort(arr);
        return arr[0];
    }

}

class Parserdoubt {

    final private int BUFFER_SIZE = 1 << 17;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Parserdoubt(InputStream in) {
        din = new DataInputStream(in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextString() throws Exception {
        StringBuffer sb = new StringBuffer("");
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        do {
            sb.append((char) c);
            c = read();
        } while (c > ' ');
        return sb.toString();
    }

    public char nextChar() throws Exception {
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        return (char) c;
    }

    public int nextInt() throws Exception {
        int ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    public long nextLong() throws Exception {
        long ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    private void fillBuffer() throws Exception {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) {
            buffer[0] = -1;
        }
    }

    private byte read() throws Exception {
        if (bufferPointer == bytesRead) {
            fillBuffer();
        }
        return buffer[bufferPointer++];
    }
}


import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
/**
 *
 * @author calcsaransh
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            Parserdoubt pd=new Parserdoubt(System.in);
            PrintWriter pw=new PrintWriter(System.out);
            int n=pd.nextInt();
            int m=pd.nextInt();
            char arr[][]=new char[n][m];
            boolean f=true;
            for(int i=0;i<arr.length;i++)
            {
                String str=pd.nextString();
                arr[i]=str.toCharArray();
                if(!str.contains("1"))f=false;
            }
            if(!f)
            {
                System.out.println(-1);
                return;
            }
            int closestleft[][]=new int[n][m];
            int leftmost[]=new int[n];
            Arrays.fill(leftmost, Integer.MAX_VALUE);
            int rightmost[]=new int[n];
            int closestright[][]=new int[n][m];
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<closestleft[i].length;j++)
                {
                    if(j==0)closestleft[i][j]=Integer.MAX_VALUE;
                    else closestleft[i][j]=closestleft[i][j-1];
                    if(arr[i][j]=='1')
                    {
                        closestleft[i][j]=j;
                        leftmost[i]=Math.min(leftmost[i],j);
                    }
                }
                for(int j=m-1;j>=0;j--)
                {
                    if(j==m-1)closestright[i][j]=Integer.MAX_VALUE;
                    else closestright[i][j]=closestright[i][j+1];
                    if(arr[i][j]=='1')
                    {
                        closestright[i][j]=j;
                        rightmost[i]=Math.max(rightmost[i],j);
                    }
                }
            }
            long min=Long.MAX_VALUE;
            for(int i=0;i<m;i++)
            {
                long res=0;
                for(int j=0;j<n;j++)
                {
                    res+=min(Math.abs(closestright[j][i]-i), Math.abs(closestleft[j][i]-i),leftmost[j]+m-i,m-rightmost[j]+i);
                }
                min=Math.min(min,res);
            }
            if(min>=Integer.MAX_VALUE/2)
                System.out.println(-1);
            else
                System.out.println(min);
            pw.flush();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static int min(int a,int b,int c,int d)
    {
        int arr[]={a,b,c,d};
        Arrays.sort(arr);
        return arr[0];
    }

}

class Parserdoubt {

    final private int BUFFER_SIZE = 1 << 17;
    private DataInputStream din;
    private byte[] buffer;
    private int bufferPointer, bytesRead;

    public Parserdoubt(InputStream in) {
        din = new DataInputStream(in);
        buffer = new byte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    public String nextString() throws Exception {
        StringBuffer sb = new StringBuffer("");
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        do {
            sb.append((char) c);
            c = read();
        } while (c > ' ');
        return sb.toString();
    }

    public char nextChar() throws Exception {
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        return (char) c;
    }

    public int nextInt() throws Exception {
        int ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    public long nextLong() throws Exception {
        long ret = 0;
        byte c = read();
        while (c <= ' ') {
            c = read();
        }
        boolean neg = c == '-';
        if (neg) {
            c = read();
        }
        do {
            ret = ret * 10 + c - '0';
            c = read();
        } while (c > ' ');
        if (neg) {
            return -ret;
        }
        return ret;
    }

    private void fillBuffer() throws Exception {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if (bytesRead == -1) {
            buffer[0] = -1;
        }
    }

    private byte read() throws Exception {
        if (bufferPointer == bytesRead) {
            fillBuffer();
        }
        return buffer[bufferPointer++];
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ProblemA {
    public static void main(String[] args) throws IOException {
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        String[] data = s.readLine().split(" ");
        int n = Integer.valueOf(data[0]);
        int m = Integer.valueOf(data[1]);
        char[][] tbl = new char[n][m];
        for (int i = 0 ; i < n ; i++) {
            tbl[i] = s.readLine().toCharArray();
        }

        int[][] minToOne = new int[n][m];
        for (int i = 0 ; i < n ; i++) {
            Arrays.fill(minToOne[i], Integer.MAX_VALUE);

            // go right
            {
                int dist = Integer.MAX_VALUE;
                for (int j = 0 ; j < m ; j++) {
                    if (tbl[i][j] == '1') {
                        dist = 0;
                        minToOne[i][j] = 0;
                    } else {
                        if (dist <= m) {
                            dist++;
                            minToOne[i][j] = Math.min(minToOne[i][j], dist);
                        }
                    }
                }

                // back to leftmost
                for (int j = 0 ; j < m ; j++) {
                    if (tbl[i][j] == '1') {
                        break;
                    }
                    if (dist <= 2*m) {
                        dist++;
                        minToOne[i][j] = Math.min(minToOne[i][j], dist);
                    }
                }
            }

            // go left
            {
                int dist = Integer.MAX_VALUE;
                for (int j = m-1 ; j >= 0 ; j--) {
                    if (tbl[i][j] == '1') {
                        dist = 0;
                        minToOne[i][j] = 0;
                    } else {
                        if (dist <= m) {
                            dist++;
                            minToOne[i][j] = Math.min(minToOne[i][j], dist);
                        }
                    }
                }

                // back to rightmost
                for (int j = m-1 ; j >= 0 ; j--) {
                    if (tbl[i][j] == '1') {
                        break;
                    }
                    if (dist <= 2*m) {
                        dist++;
                        minToOne[i][j] = Math.min(minToOne[i][j], dist);
                    }
                }
            }
        }

        long minCost = Integer.MAX_VALUE;
        for (int i = 0 ; i < m ; i++) {
            long sum = 0;
            for (int j = 0 ; j < n ; j++) {
                sum += minToOne[j][i];
            }
            if (sum >= Integer.MAX_VALUE) {
                continue;
            }
            minCost = Math.min(minCost, sum);
        }

        if (minCost == Integer.MAX_VALUE) {
            out.println(-1);
        } else {
            out.println(minCost);
        }

        out.flush();
    }

    public static void debug(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}


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

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

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.readInt();
        int m = in.readInt();
        int[][] dist = new int[n][m];
        for (int i = 0; i < n; ++i) {
            String s = in.readString();
            if (s.indexOf('1') < 0) {
                out.println(-1);
                return;
            }
            Arrays.fill(dist[i], Integer.MAX_VALUE);
            int lastLeft = s.lastIndexOf('1');
            for (int j = 0; j < m; ++j) {
                if (s.charAt(j) == '1')
                    lastLeft = j;
                dist[i][j] = Math.min(dist[i][j], Math.min(Math.abs(j - lastLeft), Math.abs(m - (j - lastLeft))));
            }
            int lastRight = s.indexOf('1');
            for (int j = m - 1; j >= 0; --j) {
                if (s.charAt(j) == '1')
                    lastRight = j;
                dist[i][j] = Math.min(dist[i][j], Math.min(Math.abs(j - lastRight), Math.abs(m - (j - lastRight))));
            }
        }
        int res = Integer.MAX_VALUE;
        for (int j = 0; j < m; ++j) {
            int cur = 0;
            for (int i = 0; i < n; ++i)
                cur += dist[i][j];
            res = Math.min(res, cur);
        }
        out.println(res == Integer.MAX_VALUE ? -1 : res);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public String readString() {
        return next();
    }

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


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

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

class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.readInt();
        int m = in.readInt();
        int[][] dist = new int[n][m];
        for (int i = 0; i < n; ++i) {
            String s = in.readString();
            if (s.indexOf('1') < 0) {
                out.println(-1);
                return;
            }
            Arrays.fill(dist[i], Integer.MAX_VALUE);
            int lastLeft = s.lastIndexOf('1');
            for (int j = 0; j < m; ++j) {
                if (s.charAt(j) == '1')
                    lastLeft = j;
                dist[i][j] = Math.min(dist[i][j], Math.min(Math.abs(j - lastLeft), m - Math.abs(j - lastLeft)));
            }
            int lastRight = s.indexOf('1');
            for (int j = m - 1; j >= 0; --j) {
                if (s.charAt(j) == '1')
                    lastRight = j;
                dist[i][j] = Math.min(dist[i][j], Math.min(Math.abs(j - lastRight), m - Math.abs(j - lastRight)));
            }
        }
        int res = Integer.MAX_VALUE;
        for (int j = 0; j < m; ++j) {
            int cur = 0;
            for (int i = 0; i < n; ++i)
                cur += dist[i][j];
            res = Math.min(res, cur);
        }
        out.println(res == Integer.MAX_VALUE ? -1 : res);
    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException();
            }
        }
        return tokenizer.nextToken();
    }

    public String readString() {
        return next();
    }

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;

public class CFA {

    BufferedReader in;
    StringTokenizer st;
    PrintWriter out;

    public static void main(String[] args) {
        CFA t = new CFA();
        t.init();
    }

    static int MAXVAL = 100000;

    void run() throws Exception {
        int n = Int();
        int m = Int();

        int[][] A = new int[n][m];
        int[][] B = new int[n][m];

        for (int i = 0; i < n; i++) {
            String line = String();
            for (int j = 0; j < m; j++) {
                A[i][j] = line.charAt(j) - '0';
                B[i][j] = MAXVAL;
            }
        }

        for (int i = 0; i < n; i++) {
            Queue<Integer> queue = new LinkedList<Integer>();

            for (int j = 0; j < m; j++) {
                if (A[i][j] == 1) {
                    B[i][j] = 0;
                    int k = (j + 1) % m;
                    int l = (j - 1 + m) % m;
                    queue.add(k);
                    queue.add(l);
                }
            }

            if (queue.isEmpty()) {
                PrintLn(-1);
                return;
            }

            while (!queue.isEmpty()) {
                Integer a = queue.poll();

                if (B[i][a] == MAXVAL) {
                    int k = (a + 1) % m;
                    int l = (a - 1 + m) % m;
                    B[i][a] = Math.min(B[i][k] + 1, B[i][l] + 1);
                    queue.add(k);
                    queue.add(l);
                }
            }
        }

        int best = Integer.MAX_VALUE;
        for (int j = 0; j < m; j++) {
            int current = 0;
            for (int i = 0; i < B.length; i++) {
                current += B[i][j];
            }
            best = Math.min(best, current);
        }
        PrintLn(best);
    }

    void init() {
        try {
            Locale.setDefault(Locale.US);
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            run();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    String String() throws Exception {
        return next();
    }

    int Int() throws Exception {
        return Integer.parseInt(next());
    }

    void PrintLn(Object line) {
        out.println(line);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.StringTokenizer;

public class CFA {

    BufferedReader in;
    StringTokenizer st;
    PrintWriter out;

    public static void main(String[] args) {
        CFA t = new CFA();
        t.init();
    }

    static int MAXVAL = 100000;

    void run() throws Exception {
        int n = Int();
        int m = Int();

        int[][] A = new int[n][m];
        int[][] B = new int[n][m];

        for (int i = 0; i < n; i++) {
            String line = String();
            for (int j = 0; j < m; j++) {
                A[i][j] = line.charAt(j) - '0';
                B[i][j] = MAXVAL;
            }
        }

        for (int i = 0; i < n; i++) {
            Queue<Integer> queue = new LinkedList<Integer>();

            for (int j = 0; j < m; j++) {
                if (A[i][j] == 1) {
                    B[i][j] = 0;
                    int k = (j + 1) % m;
                    int l = (j - 1 + m) % m;
                    queue.add(k);
                    queue.add(l);
                }
            }

            if (queue.isEmpty()) {
                PrintLn(-1);
                return;
            }

            while (!queue.isEmpty()) {
                Integer a = queue.poll();

                if (B[i][a] == MAXVAL) {
                    int k = (a + 1) % m;
                    int l = (a - 1 + m) % m;
                    B[i][a] = Math.min(B[i][k] + 1, B[i][l] + 1);
                    queue.add(k);
                    queue.add(l);
                }
            }
        }

        int best = MAXVAL;
        for (int j = 0; j < m; j++) {
            int current = 0;
            for (int i = 0; i < B.length; i++) {
                current += B[i][j];
            }
            best = Math.min(best, current);
        }
        PrintLn(best);
    }

    void init() {
        try {
            Locale.setDefault(Locale.US);
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
            run();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    String String() throws Exception {
        return next();
    }

    int Int() throws Exception {
        return Integer.parseInt(next());
    }

    void PrintLn(Object line) {
        out.println(line);
    }

}


//package round142;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;

public class A {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni(), m = ni();
		char[][] b = new char[n][];
		for(int i = 0;i < n;i++){
			b[i] = ns(m);
		}

		int[][] pr = new int[n][m];
		int[] pp = new int[n];
		for(int i = 0;i < n;i++){
			int p = 0;
			for(int j = 0;j < m;j++){
				if(b[i][j] == '1'){
					pr[i][p++] = j;
				}
			}
			pp[i] = p;
			if(p == 0){
				out.println(-1);
				return;
			}
		}

		int min = Integer.MAX_VALUE;
		for(int i = 0;i < m;i++){
			int sumd = 0;
			for(int j = 0;j < n;j++){
				int ind = Arrays.binarySearch(pr[j], 0, pp[j], i);
				if(ind >= 0){
					continue;
				}
				int inf = -ind-2;
				int dinf = 0;
				if(inf < 0){
					dinf = i+(m-pr[j][pp[j]-1]);
				}else{
					dinf = i-pr[j][inf];
				}

				int sup = -ind-1;
				int dsup = 0;
				if(sup >= pp[j]){
					dsup = m-i + pr[j][0];
				}else{
					dsup = pr[j][sup] - i;
				}
				sumd += Math.min(dsup, dinf);
			}
			min = Math.min(min, sumd);
		}
		out.println(min);
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

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

	public int ni()
	{
		try {
			int num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public long nl()
	{
		try {
			long num = 0;
			boolean minus = false;
			while((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'));
			if(num == '-'){
				num = 0;
				minus = true;
			}else{
				num -= '0';
			}

			while(true){
				int b = is.read();
				if(b >= '0' && b <= '9'){
					num = num * 10 + (b - '0');
				}else{
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	public String ns()
	{
		try{
			int b = 0;
			StringBuilder sb = new StringBuilder();
			while((b = is.read()) != -1 && (b == '\r' || b == '\n' || b == ' '));
			if(b == -1)return "";
			sb.append((char)b);
			while(true){
				b = is.read();
				if(b == -1)return sb.toString();
				if(b == '\r' || b == '\n' || b == ' ')return sb.toString();
				sb.append((char)b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public char[] ns(int n)
	{
		char[] buf = new char[n];
		try{
			int b = 0, p = 0;
			while((b = is.read()) != -1 && (b == ' ' || b == '\r' || b == '\n'));
			if(b == -1)return null;
			buf[p++] = (char)b;
			while(p < n){
				b = is.read();
				if(b == -1 || b == ' ' || b == '\r' || b == '\n')break;
				buf[p++] = (char)b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	double nd() { return Double.parseDouble(ns()); }
	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


import java.io.*;
import java.util.*;

public class Sdvigi implements Runnable {
	public static void main(String[] args) {
		new Thread(new Sdvigi()).run();
	}

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer in;
	PrintWriter out = new PrintWriter(System.out);

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}

		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();

		char[][] map = new char[n][m];
		for (int i = 0; i < n; i++) {
			map[i] = nextToken().toCharArray();
		}

		int[] ans = new int[m];
		for (int i = 0; i < n; i++) {
			int[] now = new int[m];
			Arrays.fill(now, Integer.MAX_VALUE);
			for (int j = 0; j < m; j++) {
				if (map[i][j] == '1') {
					now[j] = 0;
					int z = j - 1 == -1 ? m - 1 : j - 1;
					int k = 1;
					while (map[i][z] != '1') {
						now[z] = Math.min(now[z], k);
						z--;
						k++;
						if (z == -1)
							z += m;
					}
					z = j + 1 == m ? 0 : j + 1;
					k = 1;
					while (map[i][z] != '1') {
						now[z] = Math.min(now[z], k);
						z++;
						k++;
						if (z == m)
							z = 0;
					}
				}
			}

			if (now[0] == Integer.MAX_VALUE) {
				out.println(-1);
				return;
			}

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

		int min = Integer.MAX_VALUE;
		for (int i = 0; i < m; i++) {
			min = Math.min(min, ans[i]);
		}

		out.println(min);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}


import java.io.*;
import java.util.*;

public class Sdvigi implements Runnable {
	public static void main(String[] args) {
		new Thread(new Sdvigi()).run();
	}

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	StringTokenizer in;
	PrintWriter out = new PrintWriter(System.out);

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}

		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();

		char[][] map = new char[n][m];
		for (int i = 0; i < n; i++) {
			map[i] = nextToken().toCharArray();
		}

		int[] ans = new int[m];
		for (int i = 0; i < n; i++) {
			int[] now = new int[m];
			Arrays.fill(now, -1);
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if (map[i][k] == '1') {
						now[j] = Math.min(Math.abs(j - k), m + j - k);
						now[j] = Math.min(now[j], m + k - j);
					}
				}
			}

			if (now[0] == -1) {
				out.println(-1);
				return;
			}

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

		int min = Integer.MAX_VALUE;
		for (int i = 0; i < m; i++) {
			min = Math.min(min, ans[i]);
		}

		out.println(min);
	}

	public void run() {
		try {
			solve();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}


import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.regex.*;
import static java.lang.Math.*;
public class A {

    public A() throws Exception {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] counts = new int[m];
        for (int i=0; i<n; i++) {
            String line = in.next();
            List<Integer> list = new ArrayList<Integer>();
            for (int j=0; j<m; j++) {
                if (line.charAt(j)=='1') list.add(j);
            }
            if (list.isEmpty()) {
                System.out.println(-1);
                return;
            }

            list.add(0, list.get(list.size()-1)-m);
            list.add(list.get(1)+m);

            for (int j=0; j<m; j++) {
                if (line.charAt(j)=='1') continue;
                int p = Collections.binarySearch(list, j);
                counts[j] += min(j-list.get(~p-1), list.get(~p)-j);
            }
        }

        int ans = 1<<28;
        for (int i=0; i<m; i++) ans = min(ans, counts[i]);
        System.out.println(ans);
    }

    Scanner in = new Scanner(System.in);
    StringBuilder buf = new StringBuilder();
    public static void main(String[] args) throws Exception { // {{{
        new A();
    } // }}}
    public static void debug(Object... arr) { // {{{
        System.err.println(Arrays.deepToString(arr));
    } // }}}
    public static class Scanner { // {{{
        BufferedReader br;
        String line;
        StringTokenizer st;
        public Scanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }
        public boolean hasNext() throws IOException {
            while ((st==null||!st.hasMoreTokens())&&(line=br.readLine())!=null)
                st = new StringTokenizer(line);
            return st.hasMoreTokens();
        }
        public String next() throws IOException {
            if (hasNext()) return st.nextToken();
            throw new NoSuchElementException();
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    } // }}}
}


import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.regex.*;
import static java.lang.Math.*;
public class A {

    public A() throws Exception {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] counts = new int[m];
        for (int i=0; i<n; i++) {
            String line = in.next();
            List<Integer> list = new ArrayList<Integer>();
            for (int j=0; j<m; j++) {
                if (line.charAt(j)=='1') {
                    list.add(j);
                }
            }
            if (list.isEmpty()) {
                Arrays.fill(counts, 1<<28);
                break;
            }

            list.add(0, list.get(list.size()-1)-m);
            list.add(list.get(1)+m);

            for (int j=0; j<m; j++) {
                if (line.charAt(j)=='1') continue;
                int p = Collections.binarySearch(list, j);
                counts[j] += min(j-list.get(~p-1), list.get(~p)-j);
            }
        }

        int min = 1<<28;
        for (int i=0; i<m; i++) min = min(min, counts[i]);
        if (min==1<<28) min = -1;
        System.out.println(min);
    }

    Scanner in = new Scanner(System.in);
    StringBuilder buf = new StringBuilder();
    public static void main(String[] args) throws Exception { // {{{
        new A();
    } // }}}
    public static void debug(Object... arr) { // {{{
        System.err.println(Arrays.deepToString(arr));
    } // }}}
    public static class Scanner { // {{{
        BufferedReader br;
        String line;
        StringTokenizer st;
        public Scanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
        }
        public boolean hasNext() throws IOException {
            while ((st==null||!st.hasMoreTokens())&&(line=br.readLine())!=null)
                st = new StringTokenizer(line);
            return st.hasMoreTokens();
        }
        public String next() throws IOException {
            if (hasNext()) return st.nextToken();
            throw new NoSuchElementException();
        }
        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }
        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }
        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    } // }}}
}


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

public class _AA implements Runnable{

	final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tok = new StringTokenizer("");

	void init() throws FileNotFoundException{
		if (ONLINE_JUDGE){
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
		}else{
			in = new BufferedReader(new FileReader("input.txt"));
			out = new PrintWriter("output.txt");
		}
	}

	String readString() throws IOException{
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(in.readLine());
			}catch (Exception e){
				return null;
			}
		}
		return tok.nextToken();
	}

	int readInt() throws IOException{
		return Integer.parseInt(readString());
	}

	long readLong() throws IOException{
		return Long.parseLong(readString());
	}

	double readDouble() throws IOException{
		return Double.parseDouble(readString());
	}

	public static void main(String[] args){
		new Thread(null, new _AA(), "", 128 * (1L << 20)).start();
	}

	long timeBegin, timeEnd;

	void time(){
		timeEnd = System.currentTimeMillis();
		System.err.println("Time = " + (timeEnd - timeBegin));
	}

	long memoryTotal, memoryFree;

	void memory(){
		memoryFree = Runtime.getRuntime().freeMemory();
		System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB");
	}

	void debug(Object... objects){
		if (DEBUG){
			for (Object o: objects){
				System.err.println(o.toString());
			}
		}
	}

	public void run(){
		try{
			timeBegin = System.currentTimeMillis();
			memoryTotal = Runtime.getRuntime().freeMemory();
			init();
			solve();
			out.close();
			time();
			memory();
		}catch (Exception e){
			e.printStackTrace(System.err);
			System.exit(-1);
		}
	}

	boolean DEBUG = false;

	void solve() throws IOException{
		int n = readInt();
		int m = readInt();

		long[] dp = new long[n];
		Arrays.fill(dp, 0L);

		int[] count = new int[m];

		int[] left = new int[m];
		int[] right = new int[m];

		for (int i = 0; i < n; ++i){
			char[] c = in.readLine().toCharArray();

			left[0] = (c[0] == '1'? 0: Integer.MIN_VALUE);
			for (int j = 1; j < m; ++j){
				left[j] = (c[j] == '1'? 0: left[j-1] + 1);
			}

			if (left[m-1] < 0){
				out.print(-1);
				return;
			}

			for (int j = 0; j < m; ++j){
				if (left[j] >= 0) break;

				left[j] = left[(j-1+m)%m] + 1;
			}

			right[m-1] = (c[m-1] == '1'? 0: Integer.MIN_VALUE);
			for (int j = m - 2; j >= 0; --j){
				right[j] = (c[j] == '1'? 0: right[j+1] + 1);
			}

			for (int j = m - 1; j >= 0; --j){
				if (right[j] >= 0) break;

				right[j] = right[(j+1)%m] + 1;
			}

			for (int j = 0; j < m; ++j){
				count[j] += Math.min(left[j], right[j]);
			}
		}

		int ans = count[0];
		for (int i = 1; i < m; ++i){
			ans = Math.min(ans, count[i]);
		}

		out.print(ans);
	}
}


import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;

import static java.lang.Math.*;

public class Solution230C {
        final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
        public static void main(String[] args){
                new Solution230C().run();
        }

        BufferedReader in;
        PrintWriter out;
        StringTokenizer tok = new StringTokenizer("");

        void init() throws FileNotFoundException{
                if (ONLINE_JUDGE){
                        in = new BufferedReader(new InputStreamReader(System.in));
                        out = new PrintWriter(System.out);
                }else{
                        in = new BufferedReader(new FileReader("input.txt"));
                        out = new PrintWriter("output.txt");
                }
        }

        String readString() throws IOException{
                while(!tok.hasMoreTokens()){
                        tok = new StringTokenizer(in.readLine());
                }
                return tok.nextToken();
        }

        int readInt() throws IOException{
                return Integer.parseInt(readString());
        }

        long readLong() throws IOException{
                return Long.parseLong(readString());
        }

        double readDouble() throws IOException{
                return Double.parseDouble(readString());
        }

        int[] readArr(int n) throws IOException{
        	int[] res = new int[n];
        	for(int i = 0; i < n; i++){
        		res[i] = readInt();
        	}
        	return res;
        }

        long[] readArrL(int n) throws IOException{
        	long[] res = new long[n];
        	for(int i = 0; i < n; i++){
        		res[i] = readLong();
        	}
        	return res;
        }

        public void run(){
                try{
                        long t1 = System.currentTimeMillis();
                        init();
                        solve();
                        out.close();
                        long t2 = System.currentTimeMillis();
                        System.err.println("Time = "+(t2-t1));
                }catch (Exception e){
                        e.printStackTrace(System.err);
                        System.exit(-1);
                }
        }
        void solve() throws IOException {
            int n = readInt();
            int m = readInt();
            boolean[][] a = new boolean[n][m];
            for(int i = 0; i < n; i++){
                char[] s = readString().toCharArray();
                for(int j = 0; j < m; j++){
                    a[i][j] = s[j] == '1';
                }
            }

            int[][] l = new int[n][m];
            int[][] r = new int[n][m];

            for(int i = 0; i < n; i++){
                int curL = -1;
                for(int j = 0; j < m; j++){
                    if(a[i][j]){
                        l[i][j] = 0;
                        curL = j;
                    }
                    else{
                        if(curL == -1){
                            l[i][j] = -1;
                        }
                        else{
                            l[i][j] = j - curL;
                        }
                    }
                }

                int curR = -1;
                for(int j = m-1; j >= 0; j--){
                    if(a[i][j]){
                        r[i][j] = 0;
                        curR = j;
                    }
                    else{
                        if(curR == -1){
                            r[i][j] = -1;
                        }
                        else{
                            r[i][j] = curR - j;
                        }
                    }
                }

                if(curL == -1 || curR == -1){
                    out.println(-1);
                    return;
                }

                int first = -1;
                int last = -1;
                for(int j = 0 ; j < m; j++){
                	if(a[i][j]){
                		first = j;
                		break;
                	}
                }

                for(int j = m-1 ; j >= 0; j--){
                	if(a[i][j]){
                		last = j;
                		break;
                	}
                }

                for(int j = m-1 ; j >= 0; j--){
                	if(r[i][j] == -1){
                		r[i][j] = (m-j) + first;
                	}
                }

                for(int j = 0; j < m; j++){
                	if(l[i][j] == -1){
                		l[i][j] = j + (m-last);
                	}
                }
            }

            int[] d = new int[m];
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    int cur = l[j][i];
                    if(cur == -1){
                        cur = r[j][i];
                    }
                    else{
                        cur = min(cur, r[j][i] >= 0 ? r[j][i] : Integer.MAX_VALUE);
                    }
                    d[i] += cur;
                }
            }

            int min = Integer.MAX_VALUE;
            for(int i = 0; i < m; i++){
                min = min(min, d[i]);
            }
            out.println(min);
        }
}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class A {
	public static final boolean debug = false;
	void pln(Object o) {System.out.println(o.toString());}
	void pl(Object o) {System.out.print(o.toString());}

	public void solve() {
		Scanner sc = null;

		if (debug) {
			try {
				sc = new Scanner(new File("input.in"));
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		else {
			sc = new Scanner(System.in);
		}

		int n = sc.nextInt();
		int m = sc.nextInt();

		int[] totalCost = new int[m];
		int[] crtCost = new int[m];
		int[] row = new int[m];
		sc.nextLine();

		for (int i=0; i<n; i++) {
			boolean ok = true;
			String line = sc.nextLine();
			for (int j=0; j<m; j++) {
				row[j] = line.charAt(j) == '0' ? 0 : 1;
			}
			int dist = 40000;
			for (int j=0; j<m; j++) {
				if (row[j] == 1) dist = 0;
				crtCost[j] = dist;
				dist++;
			}
			for (int j=0; j<m; j++) {
				if (row[j] == 1) dist = 0;
				if (dist < crtCost[j]) crtCost[j] = dist;
				dist++;
			}

			dist = 40000;
			for (int j=m-1; j>=0; j--) {
				if (row[j] == 1) dist = 0;
				if (dist < crtCost[j]) crtCost[j] = dist;
				dist++;
			}
			for (int j=m-1; j>=0; j--) {
				if (row[j] == 1) dist = 0;
				if (dist < crtCost[j]) crtCost[j] = dist;
				dist++;
			}

			for (int j=0; j<m; j++) {
				if (crtCost[j] >= 40000) {
					pln(-1);
					return;
				}
				totalCost[j] += crtCost[j];
			}
		}
		int minCost = Integer.MAX_VALUE;
		for (int j=0; j<m; j++) {
			if (totalCost[j] < minCost) {
				minCost = totalCost[j];
			}
		}

		pln(minCost);
	}

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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String [] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int minSteps = n * m;

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (lines[i].charAt(j) != '1') {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (lines[i].charAt(turnToRight(j, k, m)) == '1' || lines[i].charAt(turnToLeft(j, k ,m)) == '1') {
                            counter += k;
                            onlyNulls = false;
                            break;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String[] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int[] minSteps = new int[m];

        for (int i = 0; i < n; i++) {
            int help[] = new int[m];
            boolean onlyNulls = true;
            for (int j = 0; j < m; j++) {
                if (help[j] == 1) {
                    continue;
                }
                if (lines[i].charAt(j) != '1') {
                    for (int k = 1; k <= m / 2; k++) {
                        int toLeft = turnToLeft(j, k, m);
                        if (lines[i].charAt(toLeft) == '1') {
                            minSteps[j] += k;
                            help[j] = 1;
                            for (int t = 1; t < k; t++) {
                                int toLeft2 = turnToLeft(j, t, m);
                                if (help[toLeft2] == 1) {
                                    continue;
                                }
                                minSteps[toLeft2] += k - t;
                                help[toLeft2] = 1;
                            }
                            onlyNulls = false;
                            break;
                        } else {
                            int toRight = turnToRight(j, k, m);
                            if (lines[i].charAt(toRight) == '1') {
                                minSteps[j] += k;
                                help[j] = 1;
                                for (int t = 1; t < k; t++) {
                                    int toRight2 = turnToRight(j, t, m);
                                    if (help[toRight2] == 1) {
                                        continue;
                                    }
                                    minSteps[toRight2] += k - t;
                                    help[toRight2] = 1;
                                }
                                onlyNulls = false;
                                break;
                            }
                        }
                    }
                } else {
                    onlyNulls = false;
                }
            }
            if (onlyNulls) {
                System.out.println(-1);
                return false;
            }
        }

        int min = n * m;
        for (int i = 0; i < m; i++) {
            if (minSteps[i] < min) {
                min = minSteps[i];
            }
        }
        System.out.println(min);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        byte[][] matrix = new byte[n][m];
        for (int i = 0; i < n; i++) {
            String line = in.readLine();
            for (int j = 0; j < line.length(); j++) {
                matrix[i][j] = Byte.valueOf(line.substring(j, j + 1));
            }
        }
        checkSolution(matrix, n, m);
    }

    private static boolean checkSolution(byte[][] matrix, int n, int m) {
        int minSteps = n * m;

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (matrix[i][j] != 1) {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (matrix[i][turnToRight(j, k, m)] == 1 || matrix[i][turnToLeft(j, k, m)] == 1) {
                            counter += k;
                            onlyNulls = false;
                            break;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String[] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int[] minSteps = new int[m];

        for (int i = 0; i < n; i++) {
            int help[] = new int[m];
            boolean onlyNulls = true;
            for (int j = 0; j < m; j++) {
                if (help[j] == 1) {
                    continue;
                }
                if (lines[i].charAt(j) != '1') {
                    for (int k = 1; k <= m / 2; k++) {
                        int toLeft = turnToLeft(j, k, m);
                        if (lines[i].charAt(toLeft) == '1') {
                            minSteps[j] += k;
                            for (int t = 1; t < k; t++) {
                                minSteps[turnToLeft(j, t, m)] += k - t;
                                help[turnToLeft(j, t, m)] = 1;
                            }
                            onlyNulls = false;
                            break;
                        } else {
                            int toRight = turnToRight(j, k, m);
                            if (lines[i].charAt(toRight) == '1') {
                                minSteps[j] += k;
                                for (int t = 1; t < k; t++) {
                                    minSteps[turnToRight(j, t, m)] += k - t;
                                    help[turnToRight(j, t, m)] = 1;
                                }
                                onlyNulls = false;
                                break;
                            }
                        }
                    }
                }
            }
            if (onlyNulls) {
                System.out.println(-1);
                return false;
            }
        }

        int min = n * m;
        for (int i = 0; i < m; i++) {
            if (minSteps[i] < min) {
                min = minSteps[i];
            }
        }
        System.out.println(min);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String[] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int[] minSteps = new int[m];
        int[] help = new int[m];

        for (int i = 0; i < n; i++) {
            int firstOne = lines[i].indexOf("1");
            int counter = 0;
            boolean nullsExist = true;
            for (int j = 1; j <= m; j++) {
                int toRight = turnToRight(firstOne, j, m);
                if (lines[i].charAt(toRight) == '1') {
                    nullsExist = false;
                    counter = 0;
                    help[toRight] = counter;
                } else {
                    counter++;
                    help[toRight] = counter;
                }
            }
            if (nullsExist) {
                System.out.println(-1);
                return false;
            }
            counter = 0;
            for (int j = 1; j <= m; j++) {
                int toLeft = turnToLeft(firstOne, j, m);
                if (lines[i].charAt(toLeft) == '1') {
                    counter = 0;
                    help[toLeft] = counter;
                } else {
                    counter++;
                    if (help[toLeft] > counter)
                        help[toLeft] = counter;
                }
            }

            for (int j = 0; j < m; j++) {
                minSteps[j] += help[j];
            }
        }

        int min = n * m;
        for (int i = 0; i < m; i++) {
            if (minSteps[i] < min) {
                min = minSteps[i];
            }
        }
        System.out.println(min);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String [] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int minSteps = n * m;

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (lines[i].charAt(j) != '1') {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (lines[i].charAt(turnToRight(j, k, m)) == '1' || lines[i].charAt(turnToLeft(j, k ,m)) == '1') {
                            counter += k;
                            onlyNulls = false;
                            break;
                        }
                    }
                    if (counter >= minSteps) {
                        break;
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        byte[][] matrix = new byte[n][m];
        for (int i = 0; i < n; i++) {
            String line = in.readLine();
            for (int j = 0; j < line.length(); j++) {
                matrix[i][j] = Byte.valueOf(line.substring(j, j + 1));
            }
        }
        checkSolution(matrix, n, m);
    }

    private static boolean checkSolution(byte[][] matrix, int n, int m) {
        int minSteps = (int) Math.pow(n,m);

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (matrix[i][j] != 1) {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (matrix[i][turnToRight(j, k, m)] == 1 || matrix[i][turnToLeft(j, k, m)] == 1) {
                            counter += k;
                            onlyNulls = false;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max - 1 + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }

    private static void sdvig(byte[][] matrix, int i, boolean isLeft) {
        if (isLeft) {
            byte value = matrix[i][0];
            for (int j = 0; j < matrix[i].length - 1; j++) {
                matrix[i][j] = matrix[i][j + 1];
            }
            matrix[i][matrix[i].length - 1] = value;
        } else {
            byte value = matrix[i][matrix[i].length - 1];
            for (int j = matrix[i].length - 2; j > 0; j--) {
                matrix[i][j] = matrix[i][j - 1];
            }
            matrix[i][0] = value;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        byte[][] matrix = new byte[n][m];
        for (int i = 0; i < n; i++) {
            String line = in.readLine();
            for (int j = 0; j < line.length(); j++) {
                matrix[i][j] = Byte.valueOf(line.substring(j, j + 1));
            }
        }
        checkSolution(matrix, n, m);
    }

    private static boolean checkSolution(byte[][] matrix, int n, int m) {
        int minSteps = (int) Math.pow(n,m);

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (matrix[i][j] != 1) {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (matrix[i][turnToRight(j, k, m)] == 1 || matrix[i][turnToLeft(j, k, m)] == 1) {
                            counter += k;
                            onlyNulls = false;
                            break;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }

    private static void sdvig(byte[][] matrix, int i, boolean isLeft) {
        if (isLeft) {
            byte value = matrix[i][0];
            for (int j = 0; j < matrix[i].length - 1; j++) {
                matrix[i][j] = matrix[i][j + 1];
            }
            matrix[i][matrix[i].length - 1] = value;
        } else {
            byte value = matrix[i][matrix[i].length - 1];
            for (int j = matrix[i].length - 2; j > 0; j--) {
                matrix[i][j] = matrix[i][j - 1];
            }
            matrix[i][0] = value;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String[] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int[] minSteps = new int[m];

        for (int i = 0; i < n; i++) {
            int help[] = new int[m];
            boolean onlyNulls = true;
            for (int j = 0; j < m; j++) {
                if (help[j] == 1) {
                    continue;
                }
                if (lines[i].charAt(j) != '1') {
                    for (int k = 1; k <= m / 2; k++) {
                        int toLeft = turnToLeft(j, k, m);
                        if (lines[i].charAt(toLeft) == '1') {
                            minSteps[j] += k;
                            for (int t = 1; t < k; t++) {
                                minSteps[turnToLeft(j, t, m)] += k - t;
                                help[turnToLeft(j, t, m)] = 1;
                            }
                            onlyNulls = false;
                            break;
                        } else {
                            int toRight = turnToRight(j, k, m);
                            if (lines[i].charAt(toRight) == '1') {
                                minSteps[j] += k;
                                for (int t = 1; t < k; t++) {
                                    minSteps[turnToRight(j, t, m)] += k - t;
                                    help[turnToRight(j, t, m)] = 1;
                                }
                                onlyNulls = false;
                                break;
                            }
                        }
                    }
                } else {
                    onlyNulls = false;
                }
            }
            if (onlyNulls) {
                System.out.println(-1);
                return false;
            }
        }

        int min = n * m;
        for (int i = 0; i < m; i++) {
            if (minSteps[i] < min) {
                min = minSteps[i];
            }
        }
        System.out.println(min);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

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

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String[] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int[] minSteps = new int[m];

        for (int i = 0; i < n; i++) {
            int help[] = new int[m];
            boolean onlyNulls = true;
            for (int j = 0; j < m; j++) {
                if (help[j] == 1) {
                    continue;
                }
                if (lines[i].charAt(j) != '1') {
                    for (int k = 1; k <= m / 2; k++) {
                        int toLeft = turnToLeft(j, k, m);
                        if (lines[i].charAt(toLeft) == '1') {
                            minSteps[j] += k;
                            help[j] = 1;
                            for (int t = 1; t < k; t++) {
                                int toLeft2 = turnToLeft(j, t, m);
                                if (help[toLeft2] == 1) {
                                    continue;
                                }
                                minSteps[toLeft2] += k - t;
                                help[toLeft2] = 1;
                            }
                            onlyNulls = false;
                            break;
                        } else {
                            int toRight = turnToRight(j, k, m);
                            if (lines[i].charAt(toRight) == '1') {
                                minSteps[j] += k;
                                for (int t = 1; t < k; t++) {
                                    minSteps[turnToRight(j, t, m)] += k - t;
                                    help[turnToRight(j, t, m)] = 1;
                                }
                                onlyNulls = false;
                                break;
                            }
                        }
                    }
                } else {
                    onlyNulls = false;
                }
            }
            if (onlyNulls) {
                System.out.println(-1);
                return false;
            }
        }

        int min = n * m;
        for (int i = 0; i < m; i++) {
            if (minSteps[i] < min) {
                min = minSteps[i];
            }
        }
        System.out.println(min);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        byte[][] matrix = new byte[n][m];
        for (int i = 0; i < n; i++) {
            String line = in.readLine();
            for (int j = 0; j < line.length(); j++) {
                matrix[i][j] = Byte.valueOf(line.substring(j, j + 1));
            }
        }
        checkSolution(matrix, n, m);
    }

    private static boolean checkSolution(byte[][] matrix, int n, int m) {
        int minSteps = (int) Math.pow(n,m);

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (matrix[i][j] != 1) {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (matrix[i][turnToRight(j, k, m)] == 1 || matrix[i][turnToLeft(j, k, m)] == 1) {
                            counter += k;
                            onlyNulls = false;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }

    private static void sdvig(byte[][] matrix, int i, boolean isLeft) {
        if (isLeft) {
            byte value = matrix[i][0];
            for (int j = 0; j < matrix[i].length - 1; j++) {
                matrix[i][j] = matrix[i][j + 1];
            }
            matrix[i][matrix[i].length - 1] = value;
        } else {
            byte value = matrix[i][matrix[i].length - 1];
            for (int j = matrix[i].length - 2; j > 0; j--) {
                matrix[i][j] = matrix[i][j - 1];
            }
            matrix[i][0] = value;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Sdvigi {
    private static final String SEPARATOR = " ";

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] firstLineSplitted = in.readLine().split(SEPARATOR);
        int n = Integer.parseInt(firstLineSplitted[0]);
        int m = Integer.parseInt(firstLineSplitted[1]);

        String [] lines = new String[n];
        for (int i = 0; i < n; i++) {
            lines[i] = in.readLine();
        }
        checkSolution(lines, n, m);
    }

    private static boolean checkSolution(String[] lines, int n, int m) {
        int minSteps = n * m;

        for (int j = 0; j < m; j++) {
            int counter = 0;
            for (int i = 0; i < n; i++) {
                if (lines[i].charAt(j) != 1) {
                    boolean onlyNulls = true;
                    for (int k = 1; k <= m / 2; k++) {
                        if (lines[i].charAt(turnToRight(j, k, m)) == 1 || lines[i].charAt(turnToLeft(j, k ,m)) == 1) {
                            counter += k;
                            onlyNulls = false;
                            break;
                        }
                    }
                    if (onlyNulls) {
                        System.out.println(-1);
                        return false;
                    }
                }
            }
            if (counter < minSteps) {
                minSteps = counter;
            }
        }
        System.out.println(minSteps);
        return true;
    }

    private static int turnToLeft(int j, int k, int max) {
        int diff = j - k;
        if (diff < 0) {
            return max + diff;
        }
        return diff;
    }

    private static int turnToRight(int j, int k, int max) {
        int sum = j + k;
        if (sum >= max) {
            return sum - max;
        }
        return sum;
    }
}


import java.util.*;
import static java.lang.Math.*;
import java.io.*;

public class A{
    public static void main(String[] args){
        try{
            new A().run();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    int h, w;
    ArrayList<Integer>[] ps;

    public void run() throws Exception{
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String[] nm = sc.readLine().split(" ");
        h = Integer.parseInt(nm[0]);
        w = Integer.parseInt(nm[1]);
        ps = new ArrayList[h];
        for(int i = 0; i < h; i++)
            ps[i] = new ArrayList<Integer>();
        for(int i = 0; i < h; i++){
            char[] b = sc.readLine().toCharArray();
            for(int j = 0; j < w; j++)
                if(b[j] == '1')
                    ps[i].add(j);
        }
        int ans = 1<<20;
        for(int j = 0; j < w; j++){
            int sum = 0;
            for(int i = 0; i < h; i++){
                int ind = Collections.binarySearch(ps[i], j);
                if(ind < 0){
                    ind = -ind;
                    int mn = 1<<20;
                    int n = ps[i].size();
                    if(n == 0){sum = 1<<20; break;}
                    for(int a = -2; a <= 2; a++){
                        int k = (ind+a+n*10)%n;
                        mn = min(mn, min(abs(ps[i].get(k)-j), w-abs(ps[i].get(k)-j)) );
                    }
                    sum += mn;
                }
            }
            ans = min(ans, sum);
        }
        if(ans == 1<<20)ans = -1;
        System.out.println(ans);
    }
}


import java.util.*;
import static java.lang.Math.*;
import java.io.*;

public class A{
    public static void main(String[] args){
        try{
            new A().run();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    int h, w;
    ArrayList<Integer>[] ps;

    public void run() throws Exception{
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String[] nm = sc.readLine().split(" ");
        h = Integer.parseInt(nm[0]);
        w = Integer.parseInt(nm[1]);
        ps = new ArrayList[h];
        for(int i = 0; i < h; i++)
            ps[i] = new ArrayList<Integer>();
        for(int i = 0; i < h; i++){
            char[] b = sc.readLine().toCharArray();
            for(int j = 0; j < w; j++)
                if(b[j] == '1')
                    ps[i].add(j);
        }
        int ans = 1<<20;
        for(int j = 0; j < w; j++){
            int sum = 0;
            for(int i = 0; i < h; i++){
                int ind = Collections.binarySearch(ps[i], j);
                if(ind < 0){
                    ind = -ind;
                    int mn = 1<<20;
                    for(int k = max(ind-1, 0); k < ps[i].size() && k <= ind+1; k++)
                        mn = min(mn, min(abs(ps[i].get(k)-j), w-abs(ps[i].get(k)-j)) );
                    sum += mn;
                }
            }
            ans = min(ans, sum);
        }
        if(ans == 1<<20)ans = -1;
        System.out.println(ans);
    }
}


import java.util.*;
import static java.lang.Math.*;
import java.io.*;

public class A{
    public static void main(String[] args){
        try{
            new A().run();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    int h, w;
    ArrayList<Integer>[] ps;

    public void run() throws Exception{
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String[] nm = sc.readLine().split(" ");
        h = Integer.parseInt(nm[0]);
        w = Integer.parseInt(nm[1]);
        ps = new ArrayList[h];
        for(int i = 0; i < h; i++)
            ps[i] = new ArrayList<Integer>();
        for(int i = 0; i < h; i++){
            char[] b = sc.readLine().toCharArray();
            for(int j = 0; j < w; j++)
                if(b[j] == '1')
                    ps[i].add(j);
        }
        int ans = 1<<20;
        for(int j = 0; j < w; j++){
            int sum = 0;
            for(int i = 0; i < h; i++){
                int ind = Collections.binarySearch(ps[i], j);
                if(ind < 0){
                    ind = -ind;
                    int mn = 1<<20;
                    int n = ps[i].size();
                    if(n == 0){sum = 1<<20; break;}
                    for(int a = -1; a <= 1; a++){
                        int k = (ind+a+n)%n;
                        mn = min(mn, min(abs(ps[i].get(k)-j), w-abs(ps[i].get(k)-j)) );
                    }
                    sum += mn;
                }
            }
            ans = min(ans, sum);
        }
        if(ans == 1<<20)ans = -1;
        System.out.println(ans);
    }
}


import java.util.*;
import static java.lang.Math.*;
import java.io.*;

public class A{
    public static void main(String[] args){
        try{
            new A().run();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    int h, w;
    ArrayList<Integer>[] ps;

    public void run() throws Exception{
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        String[] nm = sc.readLine().split(" ");
        h = Integer.parseInt(nm[0]);
        w = Integer.parseInt(nm[1]);
        ps = new ArrayList[h];
        for(int i = 0; i < h; i++)
            ps[i] = new ArrayList<Integer>();
        for(int i = 0; i < h; i++){
            char[] b = sc.readLine().toCharArray();
            for(int j = 0; j < w; j++)
                if(b[j] == '1')
                    ps[i].add(j);
        }
        int ans = 1<<20;
        for(int j = 0; j < w; j++){
            int sum = 0;
            for(int i = 0; i < h; i++){
                int ind = Collections.binarySearch(ps[i], j);
                if(ind < 0){
                    ind = -ind;
                    int mn = 1<<20;
                    for(int k = max(ind-1, 0); k < ps[i].size() && k <= ind+1; k++)
                        mn = min(mn, abs(ps[i].get(k)-j));
                    sum += mn;
                }
            }
            ans = min(ans, sum);
        }
        if(ans == 1<<20)ans = -1;
        System.out.println(ans);
    }
}