#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d ", &n, &m);
  char A[147][10047];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%c ", &A[i][j]);
  int P[147][10047];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) P[i][j] = -1;
    queue<int> Q;
    for (int j = 0; j < m; j++)
      if (A[i][j] == '1') {
        Q.push(j);
        Q.push(0);
        P[i][j] = 0;
      }
    while (!Q.empty()) {
      int kde = Q.front();
      Q.pop();
      int kolko = Q.front();
      Q.pop();
      int dx[] = {1, -1};
      for (int k = 0; k < 2; k++) {
        int kde1 = kde + dx[k];
        kde1 += m;
        kde1 %= m;
        if (P[i][kde1] == -1) {
          P[i][kde1] = kolko + 1;
          Q.push(kde1);
          Q.push(kolko + 1);
        }
      }
    }
  }
  int vys = -1;
  int V[10047];
  for (int i = 0; i < m; i++) V[i] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (P[i][j] == -1)
        V[j] = -1;
      else if (V[j] != -1)
        V[j] += P[i][j];
    }
  vys = V[0];
  for (int i = 0; i < m; i++) vys = min(vys, V[i]);
  printf("%d\n", vys);
  return 0;
}
