#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  string a;
  int sum[10009];
  memset(sum, 0, sizeof sum);
  int ans = 0x3f3f3f3f;
  int tmp[10009];
  int flag = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (flag) continue;
    int s = 1;
    for (int j = 0; j < a.size(); j++) {
      if (a[j] == '1') tmp[s++] = j;
    }
    if (s == 1) {
      flag = 1;
      continue;
    }
    tmp[0] = tmp[s - 1] - m;
    tmp[s] = tmp[1] + m;
    int ans = 0, l, r;
    for (int j = 0; j < m; j++) {
      r = j - tmp[ans];
      l = tmp[ans + 1] - j;
      if (l == 0) ans++;
      sum[j] += min(l, r);
    }
  }
  if (flag)
    cout << "-1" << endl;
  else {
    for (int i = 0; i < m; i++) ans = min(ans, sum[i]);
    printf("%d\n", ans);
  }
  return 0;
}
