#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int maxn = 10000 + 5;
int n, m, res;
bool flag, fg;
int ans[maxn], value[maxn];
int l[maxn], r[maxn];
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    fg = false;
    for (int j = 0; j < n; j++) {
      getchar();
      flag = false;
      for (int i = 0; i < m; i++) {
        value[i] = getchar() - '0';
        flag |= value[i];
      }
      if (!flag) {
        fg = true;
        continue;
      }
      for (int i = 0; i < m; i++)
        l[i] = value[i] ? 0 : (i == 0 ? INF : l[i - 1] + 1);
      for (int i = 0; i < m; i++) l[i] = min(l[i], l[(i - 1 + m) % m] + 1);
      for (int i = m - 1; i >= 0; i--)
        r[i] = value[i] ? 0 : (i == m - 1 ? INF : r[i + 1] + 1);
      for (int i = m - 1; i >= 0; i--) r[i] = min(r[i], r[(i + 1) % m] + 1);
      for (int i = 0; i < m; i++) ans[i] += min(l[i], r[i]);
    }
    res = INF;
    for (int i = 0; i < m; i++) res = min(res, ans[i]);
    if (fg)
      printf("-1\n");
    else
      printf("%d\n", res);
  }
  return 0;
}
