#include <bits/stdc++.h>
using namespace std;
int min(int x, int y) { return x < y ? x : y; }
int dp[101][10001];
char s[10001];
int que[10001];
int ans[10001];
int f_abs(int x, int l) {
  if (x < 0) x = -x;
  x = min(x, l - x);
  return x;
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    int i, j, top;
    memset(ans, 0, sizeof(ans));
    top = 999;
    for (i = 0; i < n; i++) {
      scanf("%s", s);
      if (!top) continue;
      top = 0;
      for (j = 0; j < m; j++) {
        dp[i][j] = -1;
        if (s[j] == '1') {
          que[top++] = j;
          dp[i][j] = 0;
        }
      }
      if (!top) continue;
      int now = 0;
      for (j = 0; j < m; j++) {
        if (s[j] == '1')
          now++, dp[i][j] = 0;
        else
          dp[i][j] = min(f_abs(j - que[(now + top - 1) % top], m),
                         f_abs(j - que[now % top], m));
      }
    }
    if (!top) {
      printf("-1\n");
      continue;
    }
    int res = -1;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        ans[j] += dp[i][j];
      }
    }
    for (i = 0; i < m; i++) {
      if (res == -1 || res > ans[i]) res = ans[i];
    }
    printf("%d\n", res);
  }
  return 0;
}
