#include <bits/stdc++.h>
int map[110][31000], mapL[110][31000], mapR[110][31000];
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int main(void) {
  int n, m, i, j, temp, sw, dab, sum;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    sw = 0;
    for (j = 1; j <= m; j++) {
      scanf("%1d", &map[i][j]);
      if (map[i][j] == 1 && sw == 0) sw = 1;
      map[i][j + m] = map[i][j];
      map[i][j + m * 2] = map[i][j];
    }
    if (sw == 0) {
      printf("-1\n");
      return 0;
    }
  }
  temp = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m * 3; j++) {
      if (map[i][j] == 1) temp = 0;
      mapL[i][j] = temp;
      temp++;
    }
  }
  temp = 1;
  for (i = 1; i <= n; i++) {
    for (j = m * 3; j >= 1; j--) {
      if (map[i][j] == 1) temp = 0;
      mapR[i][j] = temp;
      temp++;
    }
  }
  dab = 2100000000;
  for (i = m + 1; i <= m * 2; i++) {
    sum = 0;
    for (j = 1; j <= n; j++) {
      sum += min(mapR[j][i], mapL[j][i]);
    }
    if (dab > sum) dab = sum;
  }
  printf("%d\n", dab);
  return 0;
}
