#include <bits/stdc++.h>
int main() {
  char **s = new char *[105];
  int i, j, n, m, w, **left = new int *[105], **right = new int *[105], ans;
  for (i = 0; i < 105; i++) {
    s[i] = new char[10005];
    left[i] = new int[10005];
    right[i] = new int[10005];
  }
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%s", s[i]);
    int min = -1;
    for (j = 0; j < m; j++)
      if (s[i][j] == '1') {
        min = j;
        break;
      }
    if (min == -1) {
      printf("-1");
      return 0;
    }
    w = min;
    left[i][min] = 0;
    for (j = (min + 1) % m; j != min; j = (j + 1) % m) {
      if (s[i][j] == '1') w = j;
      left[i][j] = (j - w + m) % m;
    }
    w = min;
    right[i][min] = 0;
    for (j = (min - 1 + m) % m; j != min; j = (j - 1 + m) % m) {
      if (s[i][j] == '1') w = j;
      right[i][j] = (w - j + m) % m;
    }
    for (j = 0; j < m; j++)
      if (left[i][j] > right[i][j]) left[i][j] = right[i][j];
  }
  for (i = 0; i < m; i++) {
    w = 0;
    for (j = 1; j <= n; j++) w += left[j][i];
    if (w < ans || ans == -1) ans = w;
  }
  printf("%d", ans);
  return 0;
}
