#include <bits/stdc++.h>
using namespace std;
int n, m, sayac, mn = INT_MAX, tmp, k, l;
char dizi[101][10005];
vector<int> one[101];
vector<int>::iterator it, it2;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf(" %c", &dizi[i][j]);
      if (dizi[i][j] == '1') {
        one[i].push_back(j);
        ;
      }
    }
    if (!one[i].size()) {
      printf("-1\n");
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    int sayac = 0;
    for (int i = 0; i < n; i++) {
      it = lower_bound(one[i].begin(), one[i].end(), j);
      it2 = upper_bound(one[i].begin(), one[i].end(), j);
      if (it == one[i].end()) it = one[i].begin();
      if (it2 == one[i].begin())
        it2 = one[i].end() - 1;
      else
        it2--;
      k = min(m - *it2 + j, abs(*it2 - j));
      l = min(*it + m - j, abs(*it - j));
      sayac += min(k, l);
    }
    mn = min(sayac, mn);
  }
  printf("%d\n", mn);
}
