#include <bits/stdc++.h>
int ar[102][10005], l[102][10005], r[102][10005];
char s[102][10004];
int min2(int a, int b) {
  if (a > b) return b;
  return a;
}
int main() {
  int n, m, i, j;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; ++i) scanf("%s", &s[i]);
  for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) ar[i][j] = s[i][j] - '0';
  }
  for (j = 0; j < n; ++j) {
    for (i = 0; i < m; ++i) {
      if (ar[j][i] != 1)
        ++r[j][0];
      else
        break;
    }
    if (r[j][0] == m) r[j][0] = -1;
  }
  for (i = 0; i < n; ++i) {
    for (j = 1; j < m; ++j) {
      if (r[i][j - 1] == -1)
        r[i][j] = -1;
      else if (r[i][j - 1] != 0)
        r[i][j] = r[i][j - 1] - 1;
      else {
        int ct = 0, xj = j;
        while (ar[i][xj] != 1) {
          ++xj;
          ++ct;
          if (xj >= m) xj = xj % m;
        }
        r[i][j] = ct;
      }
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = m - 1; j >= 0; --j) {
      if (ar[i][j] != 1)
        ++l[i][m - 1];
      else
        break;
    }
    if (l[i][m - 1] == m) l[i][m - 1] = -1;
  }
  for (i = 0; i < n; ++i) {
    for (j = m - 2; j >= 0; --j) {
      if (l[i][j + 1] == -1)
        l[i][j] = -1;
      else if (l[i][j + 1] != 0)
        l[i][j] = l[i][j + 1] - 1;
      else {
        int ct = 0;
        int xj = j;
        while (ar[i][xj] != 1) {
          --xj, ++ct;
          if (xj < 0) xj += m;
        }
        l[i][j] = ct;
      }
    }
  }
  int min = -1;
  for (j = 0; j < m; ++j) {
    int ye = 0, ct = 0;
    for (i = 0; i < n; ++i) {
      ye = ye + min2(l[i][j], r[i][j]);
      if (l[i][j] == -1) {
        ye = -1;
        break;
      }
    }
    if (ye == -1)
      ;
    else if (min == -1)
      min = ye;
    else if (min > ye)
      min = ye;
  }
  printf("%d\n", min);
  return 0;
}
