#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long n, m, k;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> v[1010];
int sum[100010] = {0};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m, k = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '1') v[i].push_back(j);
    }
    if ((int)(v[i]).size() == 0) k = 1;
  }
  if (k) return cout << -1 << endl, 0;
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      int l, r;
      int pos = lower_bound(v[i].begin(), v[i].end(), j) - v[i].begin();
      if (pos == 0) {
        l = v[i][0] - j;
        r = m - v[i][(int)(v[i]).size() - 1] + j;
      } else if (pos == (int)(v[i]).size()) {
        l = j - v[i][(int)(v[i]).size() - 1];
        r = m + v[i][0] - j;
      } else {
        l = j - v[i][pos - 1];
        r = v[i][pos] - j;
      }
      sum[j] += min(l, r);
    }
  }
  int ma = 0x3f3f3f3f;
  for (int i = 0; i < m; i++) ma = min(ma, sum[i]);
  cout << ma << endl;
  return 0;
}
