#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  bool ok = true;
  map<long long, long long> cnt;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    s += s + s;
    vector<long long int> v;
    for (long long j = 0; j < s.size(); j++)
      if (s[j] == '1') v.push_back(j);
    if (v.size() == 0)
      ok = false;
    else {
      for (long long j = m; j < 2 * m; j++) {
        if (s[j] == '1') {
          cnt[j] += 0;
          continue;
        }
        long long ri = upper_bound((v).begin(), (v).end(), j) - v.begin();
        long long le = j - v[ri - 1];
        ri = v[ri] - j;
        cnt[j] += min(ri, le);
      }
    }
  }
  long long ans = 10000000;
  for (auto i : cnt) {
    if (i.first >= m && i.first < 2 * m) {
      ans = min(ans, i.second);
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
