#include <bits/stdc++.h>
using namespace std;
int a[110][10010], b[110][10010], n, m;
char s[10010];
int t[10010];
int Min(int x, int y) { return (x < y) ? x : y; }
int main() {
  int i, j, cur, ans, ok;
  while (~scanf("%d%d", &n, &m)) {
    ok = 1;
    for (i = 0; i < n; i++) {
      scanf("%s", s);
      cur = 0;
      for (j = 0; j < m; j++) {
        a[i][j] = s[j] - '0';
        cur += a[i][j];
      }
      if (cur == 0) {
        ok = 0;
      }
    }
    if (ok == 0) {
      printf("-1\n");
      continue;
    }
    for (i = 0; i < n; i++) {
      if (a[i][0] == 1)
        b[i][0] = 0;
      else {
        ans = 1;
        cur = m - 1;
        while (a[i][cur] == 0) {
          ans++;
          cur--;
        }
        b[i][0] = ans;
      }
      for (j = 1; j < m; j++) {
        if (a[i][j] == 1)
          b[i][j] = 0;
        else
          b[i][j] = b[i][j - 1] + 1;
      }
      if (a[i][m - 1] == 1)
        t[m - 1] = 0;
      else {
        ans = 0;
        while (a[i][ans] == 0) ans++;
        t[m - 1] = ans + 1;
      }
      for (j = m - 2; j >= 0; j--) {
        if (a[i][j] == 1)
          t[j] = 0;
        else
          t[j] = t[j + 1] + 1;
      }
      for (j = 0; j < m; j++) {
        b[i][j] = Min(b[i][j], t[j]);
      }
    }
    memset(t, 0, sizeof(t));
    ans = 1e9;
    for (j = 0; j < m; j++) {
      for (i = 0; i < n; i++) {
        t[j] += b[i][j];
      }
      if (t[j] < ans) ans = t[j];
    }
    printf("%d\n", ans);
  }
  return 0;
}
