#include <bits/stdc++.h>
using namespace std;
char a[103][10004];
int b[103][10004];
int c[103][10004];
int main() {
  int i, j, ii;
  int n;
  int m;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", a[i]);
    for (j = 0; a[i][j]; j++) b[i][j] = a[i][j] - '0';
  }
  bool fl;
  for (i = 0; i < n; i++) {
    fl = false;
    for (j = 0; j < m; j++) {
      if (b[i][j] == 1) {
        fl = true;
        c[i][j] = 0;
      } else
        c[i][j] = 2000000000;
    }
    if (!fl) {
      printf("-1\n");
      return 0;
    }
  }
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++) {
      if (c[i][j] == 0) {
        ii = j + 1;
        while (c[i][ii % m] != 0) {
          c[i][ii % m] = min(c[i][ii % m], ii - j);
          ii++;
        }
        ii = j - 1 + m;
        while (c[i][ii % m] != 0) {
          c[i][ii % m] = min(c[i][ii % m], j + m - ii);
          ii--;
        }
      }
    }
  int sum;
  int mini = 2000000000;
  for (i = 0; i < m; i++) {
    sum = 0;
    for (j = 0; j < n; j++) sum += c[j][i];
    mini = min(sum, mini);
  }
  printf("%d\n", mini);
  return 0;
}
