#include <bits/stdc++.h>
char a[101][20001];
int b[101][10001];
int main() {
  int n, m, i, j, s, g, t;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; ++i) {
    scanf("%s", a[i]);
    for (j = 0; j < m; ++j) {
      a[i][m + j] = a[i][j];
    }
  }
  memset(b, -1, sizeof(b));
  for (i = 0; i < n; ++i) {
    for (j = 0, t = -1; j < 2 * m; ++j) {
      if (a[i][j] == '1') t = j;
      if (t != -1) {
        if (b[i][j % m] == -1 || b[i][j % m] > j - t) b[i][j % m] = j - t;
      }
    }
    if (t == -1) {
      printf("-1\n");
      return 0;
    }
    for (j = 2 * m - 1, t = -1; j >= 0; --j) {
      if (a[i][j] == '1') t = j;
      if (t != -1) {
        if (b[i][j % m] == -1 || b[i][j % m] > t - j) b[i][j % m] = t - j;
      }
    }
  }
  for (j = 0, g = 20000001; j < m; ++j) {
    for (i = 0, s = 0; i < n; ++i) {
      s += b[i][j];
    }
    if (g > s) g = s;
  }
  printf("%d\n", g);
  return 0;
}
