#include <bits/stdc++.h>
using namespace std;
template <class T>
void rdNum(T &n) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  n = x * f;
}
const int maxN = 1e4 + 10;
char s[110][maxN];
int l[110][maxN];
int r[110][maxN];
int pos[110][maxN];
int n, m;
const int inf = 0x3f3f3f3f;
int main() {
  while (~scanf("%d%d", &n, &m)) {
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%s", s[i] + 1);
      int p;
      bool flag = 0;
      p = 0;
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == '1') {
          l[i][j] = 0;
          p = j;
          flag = 1;
        } else {
          l[i][j] = j - p;
        }
      }
      p = m + 1;
      for (int j = m; j >= 1; j--) {
        if (s[i][j] == '1') {
          r[i][j] = 0;
          p = j;
          flag = 1;
        } else {
          r[i][j] = p - j;
        }
      }
      for (int j = 1; j <= m; j++) {
        if (l[i][j] == j) l[i][j] = l[i][m] + j;
        if (r[i][j] == m + 1 - j) r[i][j] = r[i][1] + m - j + 1;
        pos[i][j] = min(l[i][j], r[i][j]);
      }
      if (flag) cnt++;
    }
    if (cnt < n) {
      puts("-1");
      continue;
    }
    int an = inf;
    for (int j = 1; j <= m; j++) {
      int tmp = 0;
      for (int i = 1; i <= n; i++) tmp += pos[i][j];
      if (tmp < an) an = tmp;
    }
    if (an == inf) an = -1;
    printf("%d\n", an);
  }
  return 0;
}
