#include <bits/stdc++.h>
using namespace std;
int a[101][10005], mn[101][10005], f[101], l[101];
int main() {
  int i, j, n, m, k, ans = 1000000000, cnt = 0;
  char c;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 1; i <= (n); i++) {
    scanf("%c", &c);
    for (j = 1; j <= (m); j++) {
      scanf("%c", &c);
      a[i][j] = c - 48;
    }
  }
  for (i = 1; i <= (n); i++) {
    f[i] = 100000;
    l[i] = 100000;
    for (j = 1; j <= m; j++) {
      if (a[i][j] == 1) {
        f[i] = j - 1;
        break;
      }
    }
    for (j = m; j > 0; j--) {
      if (a[i][j] == 1) {
        l[i] = m - j;
        break;
      }
    }
  }
  for (i = 1; i <= (n); i++) {
    for (j = 1; j <= m; j++) mn[i][j] = 100000;
  }
  for (i = 1; i <= (n); i++) {
    mn[i][0] = l[i];
    for (j = 1; j <= m; j++) {
      if (a[i][j] == 1)
        mn[i][j] = 0;
      else
        mn[i][j] = mn[i][j - 1] + 1;
    }
    mn[i][m + 1] = f[i];
    for (j = m; j > 0; j--) {
      if (a[i][j] == 1)
        mn[i][j] = 0;
      else
        mn[i][j] =
            ((mn[i][j]) < (mn[i][j + 1] + 1) ? (mn[i][j]) : (mn[i][j + 1] + 1));
    }
  }
  for (i = 1; i <= (n); i++) {
    if (mn[i][1] >= 10000) {
      printf("-1");
      return 0;
    }
  }
  for (j = 1; j <= (m); j++) {
    cnt = 0;
    for (i = 1; i <= (n); i++) {
      cnt += mn[i][j];
    }
    ans = ((ans) < (cnt) ? (ans) : (cnt));
  }
  printf("%d\n", ans);
  return 0;
}
