#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long int n, m, x, l, r;
  string s;
  long long int cnt[10005], ans[10005] = {};
  cin >> n >> m;
  for (long long int j = 0; j < n; j++) {
    cin >> s;
    memset(cnt, -1, sizeof(cnt));
    l = -1, x = -1;
    for (long long int i = 0; i < m; i++) {
      if (s[i] == '1') {
        cnt[i] = 0;
        if (l == -1) l = i;
        x = i;
      } else if (x != -1)
        cnt[i] = min(i - x, m - i + l);
    }
    if (x == -1) {
      cout << -1;
      return 0;
    }
    r = -1, x = -1;
    for (long long int i = m - 1; i >= 0; i--) {
      if (s[i] == '1') {
        cnt[i] = 0;
        if (r == -1) r = i;
        x = i;
      } else if (cnt[i] == -1)
        cnt[i] = min(x - i, m - r + i);
      else if (x != -1)
        cnt[i] = min(cnt[i], min(x - i, m - r + i));
      ans[i] += cnt[i];
    }
  }
  long long int mn = ans[0];
  for (long long int i = 1; i < m; i++)
    if (mn > ans[i]) mn = ans[i];
  cout << mn << endl;
  return 0;
}
