#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int a[101][10001], d[101][10001];
  vector<vector<int> > f(n + 1, vector<int>(0));
  memset(d, 0x3f, sizeof(d));
  memset(a, 0, sizeof(a));
  bool bad = false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == '1') a[i][j] = 1, f[i].push_back(j);
    }
    if (f[i].size() == 0) bad = true;
  }
  if (bad) {
    cout << -1 << "\n";
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int k = f[i].size();
    for (int j = 0; j < k; j++) {
      d[i][f[i][j]] = 0;
      for (int l = 1; l < m; l++) {
        if (a[i][(l + f[i][j]) % m]) break;
        d[i][(l + f[i][j]) % m] = min(d[i][(l + f[i][j]) % m], l);
      }
      for (int l = 1; l < m; l++) {
        if (a[i][(f[i][j] - l + m) % m]) break;
        d[i][((f[i][j] - l) + m) % m] = min(d[i][((f[i][j] - l) + m) % m], l);
      }
    }
  }
  int ans = 2e9;
  for (int i = 0; i < m; i++) {
    int tot = 0;
    for (int j = 0; j < n; j++) tot += d[j][i];
    ans = min(ans, tot);
  }
  cout << ans << "\n";
}
