#include <bits/stdc++.h>
using namespace std;
const int con = pow(10, 9);
int M, N;
int give = con;
int t[110][10010];
int L[110][10010];
int R[110][10010];
int main() {
  cin >> M >> N;
  for (int i = 1; i <= M; i++) {
    bool flag = false;
    for (int j = 1; j <= N; j++) {
      char c;
      scanf("%c", &c);
      while (c != '0' && c != '1') scanf("%c", &c);
      if (c == '1') {
        flag = true;
        t[i][j] = true;
      }
    }
    if (!flag) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i <= M; i++) {
    int e;
    if (t[i][1])
      L[i][1] = 0;
    else {
      for (e = N; !t[i][e]; e--)
        ;
      L[i][1] = N - e + 1;
    }
    for (int j = 2; j <= N; j++) {
      if (t[i][j])
        L[i][j] = 0;
      else
        L[i][j] = L[i][j - 1] + 1;
    }
  }
  for (int i = 1; i <= M; i++) {
    int e;
    if (t[i][N])
      R[i][N] = 0;
    else {
      for (e = 1; !t[i][e]; e++)
        ;
      R[i][N] = e;
    }
    for (int j = N - 1; j >= 1; j--) {
      if (t[i][j])
        R[i][j] = 0;
      else
        R[i][j] = R[i][j + 1] + 1;
    }
  }
  for (int j = 1; j <= N; j++) {
    int use = 0;
    for (int i = 1; i <= M; i++) use += min(L[i][j], R[i][j]);
    give = min(give, use);
  }
  cout << give;
  return 0;
}
