#include <bits/stdc++.h>
using namespace std;
int N, M;
char s[105][20005];
int col[105][20005], col2[105][20005];
int arr[105][20005], arr2[105][20005];
int main() {
  memset(col, -1, sizeof col);
  memset(col2, -1, sizeof col2);
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%s", s[i]);
    for (int j = M; j < M + M; j++) s[i][j] = s[i][j - M];
  }
  M += M;
  int k = 0, F = 0;
  for (int i = 0; i < N; i++) {
    k = 0;
    bool flag = 0;
    for (int j = 0; j < M; j++) {
      if (s[i][j] == '1') flag = 1;
      arr[i][j] = k;
      if (s[i][j] == '1') {
        col[i][k] = j;
        k++;
      }
    }
    if (!flag) {
      F = 1;
    }
  }
  if (F) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < N; i++) {
    k = 0;
    for (int j = M - 1; j >= 0; j--) {
      arr2[i][j] = k;
      if (s[i][j] == '1') {
        col2[i][k] = j;
        k++;
      }
    }
  }
  int ret = 1 << 30;
  for (int i = 0; i < M; i++) {
    int x = 0;
    for (int j = 0; j < N; j++) {
      int a = 1 << 30;
      if (col[j][arr[j][i]] != -1) a = abs(i - col[j][arr[j][i]]);
      int b = 1 << 30;
      if (col2[j][arr2[j][i]] != -1) b = abs(i - col2[j][arr2[j][i]]);
      x += min(a, b);
    }
    ret = min(ret, x);
  }
  printf("%d\n", ret);
  getchar();
  getchar();
}
