#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:500000000")
using namespace std;
char A[102][10009];
int c[102][10009];
int L[102], R[102];
int main() {
  int n, m, i, j, k;
  cin >> n >> m;
  for (i = 0; i < n; i++) scanf("%s", A[i]);
  for (i = 0; i < n; i++) {
    for (j = m - 1; j >= 0; j--) {
      if (A[i][j] == '1') {
        L[i] = j;
        break;
      }
    }
    if (j < 0) {
      puts("-1");
      return 0;
    }
    for (j = 0; j < m; j++) {
      if (A[i][j] == '1') {
        R[i] = j;
        break;
      }
    }
  }
  for (i = 0; i < n; i++) c[i][0] = A[i][0] == '1' ? 0 : (m - L[i]);
  for (i = 0; i < n; i++)
    for (j = 1; j < m; j++) c[i][j] = A[i][j] == '1' ? 0 : (c[i][j - 1] + 1);
  for (i = 0; i < n; i++)
    c[i][m - 1] = min(c[i][m - 1], A[i][m - 1] == '1' ? 0 : (R[i] + 1));
  for (i = 0; i < n; i++)
    for (j = m - 2; j >= 0; j--)
      c[i][j] = min(c[i][j], A[i][j] == '1' ? 0 : (c[i][j + 1] + 1));
  int minV = 0x3F3F3F3F;
  for (j = 0; j < m; j++) {
    int sum = 0;
    for (i = 0; i < n; i++) sum += c[i][j];
    minV = min(minV, sum);
  }
  printf("%d\n", minV);
}
