#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
int m;
string s[100];
int g[10000];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++)
    if (s[i] == string(m, '0')) {
      printf("-1\n");
      return 0;
    }
  for (int i = 0; i < n; i++) {
    vector<int> v;
    for (int j = 0; j < m; j++)
      if (s[i][j] == '1') v.push_back(j);
    int s = v[0], t = v[((int)(v).size()) - 1];
    v.push_back(t - m);
    v.push_back(s + m);
    sort((v).begin(), (v).end());
    int k = 0;
    for (int j = 0; j < m; j++) {
      while (v[k + 1] <= j) k++;
      g[j] += min(j - v[k], v[k + 1] - j);
    }
  }
  int res = 1e9;
  for (int j = 0; j < m; j++) res = min(res, g[j]);
  printf("%d\n", res);
  return 0;
}
