You are given a table consisting of *n* rows and *m* columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.

To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".

Determine the minimum number of moves needed to make some table column consist only of numbers 1.
The first line contains two space-separated integers: *n* (1 ≤ *n* ≤ 100) — the number of rows in the table and *m* (1 ≤ *m* ≤ 10<sup class="upper-index">4</sup>) — the number of columns in the table. Then *n* lines follow, each of them contains *m* characters "0" or "1": the *j*-th character of the *i*-th line describes the contents of the cell in the *i*-th row and in the *j*-th column of the table.

It is guaranteed that the description of the table contains no other characters besides "0" and "1".
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

file=1
def solve():


    # for _ in range(ii()):

    n,m=mi()
    a=[]
    f=0
    for i in range(n):
        s=list(si())
        if '1' not in s:
            f=1
            continue
        # s1=s+""+s
        # print(s1)
        pre=[m]*(2*m)
        if s[0]=='1':
            pre[0]=0
        for j in range(1,2*m):
            if s[j%m]=='1':
                pre[j]=0
            else:
                pre[j]=pre[j-1]+1
        suff=[m]*(2*m)
        if s[-1]=='1':
            suff[-1]=0
        for j in range(2*m-2,-1,-1):
            if s[j%m]=='1':
                suff[j]=0
            else:
                suff[j]=suff[j+1]+1
        # print(pre,suff)
        b=[m]*m
        for j in range(2*m):
            b[j%m]=min({b[j%m],pre[j],suff[j]})
        a.append(b)

    # for i in a:
    #     print(*i)
    if(f):
        print("-1")
        exit(0)
    # print(a)
    ans=inf
    for i in range(m):
        s=0
        for j in range(n):
            s+=a[j][i]
        ans=min(ans,s)
    print(ans)


 








        
if __name__ =="__main__":

    if(file):

        if path.exists('input1.txt'):
            sys.stdin=open('input1.txt', 'r')
            sys.stdout=open('output1.txt','w')
        else:
            input=sys.stdin.readline
    solve()
