#include <bits/stdc++.h>
struct node {
  int l, r;
} coo[10010];
char r[10010];
int ans[110][10010];
int cnt[10010];
int Min(int a, int b) {
  if (a < b) return a;
  return b;
}
int main() {
  int n, m, i, j, l, p, ml, mr, ll, rr, aaaa;
  scanf("%d%d", &n, &m);
  memset(ans, 1, sizeof(ans));
  for (i = 0; i < n; i++) {
    scanf(" %s", r);
    l = strlen(r);
    ml = mr = p = -1;
    for (j = 0; j < l; j++) {
      if (r[j] == '1' && ml == -1) ml = j;
      if (r[j] == '1') p = j;
      coo[j].l = p;
    }
    p = -1;
    for (j = l - 1; j >= 0; j--) {
      if (r[j] == '1' && mr == -1) mr = j;
      if (r[j] == '1') p = j;
      coo[j].r = p;
    }
    if (ml == -1) {
      printf("-1\n");
      return 0;
    }
    for (j = 0; j < l; j++) {
      ll = j - coo[j].l;
      if (coo[j].l == -1) ll = l - mr + j;
      rr = coo[j].r - j;
      if (coo[j].r == -1) rr = l - j + ml;
      ans[i][j] = Min(ll, rr);
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < l; j++) {
      cnt[j] += ans[i][j];
    }
  }
  aaaa = 0x7fffffff;
  for (j = 0; j < l; j++) {
    aaaa = Min(aaaa, cnt[j]);
  }
  printf("%d\n", aaaa);
  return 0;
}
