#include <bits/stdc++.h>
using namespace std;
long long int rght[10005];
long long int lft[10005];
long long int col[10005] = {0};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string a;
  long long int n, m, i, j, ans;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a;
    rght[0] = 0;
    for (j = 1; j < m; j++) {
      if (a[j] == '1')
        rght[j] = j;
      else
        rght[j] = rght[j - 1];
    }
    j = 0;
    while (a[j] != '1' && j < m) {
      rght[j] = rght[m - 1] - m;
      j++;
    }
    lft[m - 1] = m - 1;
    for (j = (m - 2); j >= 0; j--) {
      if (a[j] == '1')
        lft[j] = j;
      else
        lft[j] = lft[j + 1];
    }
    j = m - 1;
    while (a[j] != '1' && j >= 0) {
      lft[j] = lft[0] + m;
      j--;
    }
    if (j < 0) {
      for (j = 0; j < m; j++) col[j] = col[j] + 1000000000000000;
    } else {
      for (j = 0; j < m; j++) col[j] = col[j] + min(j - rght[j], lft[j] - j);
    }
  }
  ans = 100000000000;
  for (j = 0; j < m; j++) ans = min(ans, col[j]);
  if (ans == 100000000000)
    cout << "-1\n";
  else
    cout << ans << "\n";
  return 0;
}
