#include <bits/stdc++.h>
using namespace std;
const int N = 100;
const int M = 10000;
vector<bitset<M> > a(N);
int main(int argc, char **argv) {
  int m, n;
  scanf("%d %d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) a[i][j] = getchar() - '0';
    getchar();
  }
  vector<int> b(m), count(m);
  for (int i = 0; i < n; i++) {
    int sj;
    for (sj = 0; sj < m; sj++)
      if (a[i][sj]) break;
    if (sj == m) {
      cout << -1;
      return 0;
    }
    int c = 0;
    for (int j = 0; j < m; j++) {
      int index = (sj + j) % m;
      if (a[i][index] == 1)
        c = 0;
      else
        c++;
      b[index] = c;
    }
    c = 0;
    for (int j = 0; j < m; j++) {
      int index = sj - j;
      if (index < 0) index = m + index;
      if (a[i][index] == 1)
        c = 0;
      else
        c++;
      b[index] = min(c, b[index]);
    }
    for (int j = 0; j < m; j++) count[j] += b[j];
  }
  cout << *min_element(count.begin(), count.end());
  return 0;
}
