#include <bits/stdc++.h>
using namespace std;
int A[110][10010];
int P[110][10010];
int C[110];
int Q[110][10010];
int N, M;
int main() {
  cin >> N >> M;
  for (int i = 0; i < N * M; i++) {
    char c = '\n';
    while (c != '0' && c != '1') scanf("%c", &c);
    A[i / M][i % M] = c == '1';
  }
  bool is_bad = false;
  for (int i = 0; i < N; i++) {
    int cnt = 0;
    for (int j = 0; j < M; j++) cnt += A[i][j];
    if (cnt == 0) is_bad = true;
    C[i] = cnt;
  }
  if (is_bad) {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 0; i < N; i++) {
    bool started = false;
    int filled = 0;
    int j = 0;
    while (filled < M) {
      if (A[i][j] == 1) {
        P[i][j] = 0;
        started = true;
        filled++;
      } else if (started) {
        P[i][j] = P[i][(j - 1 + M) % M] + 1;
        filled++;
      }
      j = (j + 1) % M;
    }
  }
  for (int i = 0; i < N; i++) {
    bool started = false;
    int filled = 0;
    int j = M - 1;
    while (filled < M) {
      if (A[i][j] == 1) {
        Q[i][j] = 0;
        started = true;
        filled++;
      } else if (started) {
        Q[i][j] = Q[i][(j + 1) % M] + 1;
        filled++;
      }
      j = (j - 1 + M) % M;
    }
  }
  int m = M * N;
  for (int j = 0; j < M; j++) {
    int c = 0;
    for (int i = 0; i < N; i++) c += P[i][j] < Q[i][j] ? P[i][j] : Q[i][j];
    m = m > c ? c : m;
  }
  cout << m << '\n';
}
