#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  cin >> n >> m;
  vector<string> mat(n);
  vector<vector<int> > ones(n);
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
    k = 1;
    ones[i].push_back(0);
    for (int j = 0; j < m; j++) {
      if (mat[i][j] == '1') ones[i].push_back(j);
    }
    if (ones[i].size() == 1) {
      cout << -1 << endl;
      return 0;
    }
    ones[i][0] = ones[i].back() - m;
    ones[i].push_back(ones[i][1] + m);
  }
  vector<vector<int> > dists(n, *new vector<int>(m, 0));
  for (int i = 0; i < n; i++) {
    int p = 0;
    for (int j = 0; j < m; j++) {
      dists[i][j] = min(abs(j - ones[i][p]), abs(ones[i][p + 1] - j));
      if (ones[i][p + 1] == j) p++;
    }
  }
  int minimum = 1000001;
  for (int j = 0; j < m; j++) {
    int sum = 0;
    for (int i = 0; i < n; i++) sum += dists[i][j];
    minimum = min(sum, minimum);
  }
  cout << minimum << endl;
  return 0;
}
