#include <bits/stdc++.h>
using namespace std;
int n, m;
bool ans = true;
char c[101][101 * 101];
int p[101][101 * 101];
vector<int> v;
void init(int r) {
  if (!v.size()) {
    ans = false;
    return;
  }
  for (int j = 0; j <= v[0]; ++j) p[r][j] = min(v[0] - j, m - v.back() + j);
  for (int i = 1; i < v.size(); ++i)
    for (int j = v[i - 1]; j <= v[i]; ++j)
      p[r][j] = min(j - v[i - 1], v[i] - j);
  for (int j = v.back(); j < m; ++j) p[r][j] = min(j - v.back(), v[0] + m - j);
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    v.clear();
    for (int j = 0; j < m; ++j) {
      cin >> c[i][j];
      if (c[i][j] == '1') v.push_back(j);
    }
    init(i);
  }
  if (ans == false)
    cout << -1 << endl;
  else {
    int res = m * n;
    for (int j = 0; j < m; ++j) {
      int nr = 0;
      for (int i = 0; i < n; ++i) nr += p[i][j];
      res = min(res, nr);
    }
    cout << res << endl;
  }
  return 0;
}
