#include <bits/stdc++.h>
using namespace std;
char matrix[111][11111];
int n, m;
int mindep[111][11111];
int que[11111], head, tail;
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 0; i < n; i++) scanf(" %s", matrix[i]);
    for (int i = 0; i < n; i++) {
      head = tail = 0;
      for (int j = 0; j < m; j++) {
        if (matrix[i][j] == '0')
          mindep[i][j] = 0x3f3f3f3f;
        else {
          mindep[i][j] = 0;
          que[tail++] = j;
        }
      }
      while (head != tail) {
        int pos = que[head++];
        if (pos == 0) {
          if (mindep[i][m - 1] == 0x3f3f3f3f) {
            mindep[i][m - 1] = mindep[i][pos] + 1;
            que[tail++] = m - 1;
          }
        } else {
          if (mindep[i][pos - 1] == 0x3f3f3f3f) {
            mindep[i][pos - 1] = mindep[i][pos] + 1;
            que[tail++] = pos - 1;
          }
        }
        if (pos == m - 1) {
          if (mindep[i][0] == 0x3f3f3f3f) {
            mindep[i][0] = mindep[i][pos] + 1;
            que[tail++] = 0;
          }
        } else {
          if (mindep[i][pos + 1] == 0x3f3f3f3f) {
            mindep[i][pos + 1] = mindep[i][pos] + 1;
            que[tail++] = pos + 1;
          }
        }
      }
    }
    int ans = 0x3f3f3f3f;
    for (int j = 0; j < m; j++) {
      int i, tmp = 0;
      for (i = 0; i < n; i++) {
        if (mindep[i][j] == 0x3f3f3f3f) break;
        tmp += mindep[i][j];
      }
      if (i == n && tmp < ans) ans = tmp;
    }
    if (ans == 0x3f3f3f3f) ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}
