#include <bits/stdc++.h>
using namespace std;
char s[11111];
int m, n, a[11111], l[11111], r[11111], f[11111], ans;
int main() {
  scanf("%d%d\n", &m, &n);
  for (int i = 1; i <= m; ++i) {
    scanf("%s\n", s + 1);
    bool ok = 0;
    for (int j = 1; j <= n; ++j) a[j] = s[j] - 48, ok |= a[j];
    if (!ok) {
      printf("-1\n");
      return 0;
    }
    for (int j = 1; j <= n; ++j) l[j] = (a[j]) ? j : l[j - 1];
    for (int j = 1; j <= n; ++j) {
      if (a[j]) break;
      l[j] = l[n] - n;
    }
    for (int j = n; j >= 1; --j) r[j] = (a[j]) ? j : r[j + 1];
    for (int j = n; j >= 1; --j) {
      if (a[j]) break;
      r[j] = r[1] + n;
    }
    for (int j = 1; j <= n; ++j) f[j] += min(abs(j - l[j]), abs(r[j] - j));
  }
  ans = 1;
  for (int i = 2; i <= n; ++i)
    if (f[ans] > f[i]) ans = i;
  printf("%d\n", f[ans]);
  return 0;
}
