#include <bits/stdc++.h>
using namespace std;
char s[110][10010];
int t[110][10010];
int main() {
  int i = 0, j, n, m;
  int l[10020], r[10020], flag = 1;
  memset(r, 0, sizeof(r));
  cin >> n >> m;
  for (i = 1; i <= n; i++) scanf("%s", s[i]);
  for (i = 1; i <= n; i++) {
    memset(l, 0, sizeof(l));
    for (j = 0; j < m; j++)
      if (s[i][j] == '1') l[++l[0]] = j;
    if (l[0] == 0) {
      flag = 0;
      break;
    }
    l[++l[0]] = l[1] + m;
    for (j = 1; j < l[0]; j++) {
      int k = (l[j] + l[j + 1]) / 2;
      int p = l[j];
      for (; p <= k; p++) r[p % m] += p - l[j];
      for (; p <= l[j + 1]; p++) r[p % m] += l[j + 1] - p;
    }
  }
  if (flag == 0)
    cout << "-1" << endl;
  else {
    int ans = -1;
    for (i = 0; i < m; i++)
      if (ans == -1 || ans > r[i]) ans = r[i];
    cout << ans << endl;
  }
  return 0;
}
