#include <bits/stdc++.h>
using namespace std;
int a[101][10001], d[101][10001], n, m;
int main() {
  int i, j, p, ans = 100100100, l = 0;
  char c;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      cin >> c;
      a[i][j] = c - '0';
    }
  for (i = 1; i <= n; i++) {
    p = -10001;
    for (j = 1; j <= m * 2; j++) {
      if (j <= m) {
        if (a[i][j] == 1) p = j;
        d[i][j] = abs(j - p);
      } else {
        if (a[i][j - m] == 1) p = j;
        d[i][j - m] = abs(j - p);
      }
    }
    if (p == -10001) l = 1;
  }
  for (i = 1; i <= n; i++) {
    p = 110001;
    for (j = m * 2; j >= 1; j--) {
      if (j <= m) {
        if (a[i][j] == 1) p = j;
        if (abs(j - p) < d[i][j]) d[i][j] = abs(j - p);
      } else {
        if (a[i][j - m] == 1) p = j;
        if (abs(j - p) < d[i][j - m]) d[i][j - m] = abs(j - p);
      }
    }
  }
  int o;
  for (i = 1; i <= m; i++) {
    o = 0;
    for (j = 1; j <= n; j++) o += d[j][i];
    if (o < ans) ans = o;
  }
  if (l == 1)
    printf("-1\n");
  else
    printf("%d\n", ans);
}
