#include <bits/stdc++.h>
using namespace std;
void sieve(bool prime[], int n) {
  for (int i = 2; i * i <= n; i++) {
    if (prime[i])
      for (int j = i * 2; j <= n; j += i) prime[j] = false;
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  memset(a, 0, sizeof a);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    size_t pos = s.find('1');
    if (pos == string::npos) {
      cout << "-1";
      return;
    }
    int dis = 0;
    for (int j = 0; j < m; j++) {
      if (s[(j + pos) % m] == '1')
        dis = 0;
      else
        dis++;
      a[i][(j + pos) % m] = dis;
    }
    for (int j = m; j > 0; j--) {
      if (s[(j + pos) % m] == '1')
        dis = 0;
      else
        dis++;
      a[i][(j + pos) % m] = min(dis, a[i][(j + pos) % m]);
    }
  }
  int ans = int(1e9);
  for (int i = 0; i < m; i++) {
    int s = 0;
    for (int j = 0; j < n; j++) s += a[j][i];
    ans = min(ans, s);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
