#include <bits/stdc++.h>
using namespace std;
int r, c;
char M[105][10010];
int D[105][10010];
int A[10010];
int main() {
  scanf("%d %d", &r, &c);
  for (int i = 0; i < r; ++i) scanf("%s", M[i]);
  memset(D, -1, sizeof(D));
  memset(A, 0, sizeof(A));
  bool bo = true;
  for (int i = 0; i < r; ++i) {
    int mini = -1, maxi = -1;
    for (int j = 0; j < c; ++j)
      if (M[i][j] == '1') {
        D[i][j] = 0;
        if (mini == -1) mini = j;
        maxi = max(j, maxi);
      }
    if (maxi == -1) {
      bo = false;
      break;
    }
    int last = c - maxi;
    last = -last;
    for (int j = 0; j < c; ++j)
      if (D[i][j] == -1) {
        D[i][j] = j - last;
      } else
        last = j;
    D[i][c - 1] = min(D[i][c - 1], mini + 1);
    A[c - 1] += D[i][c - 1];
    for (int j = c - 2; j >= 0; j--) {
      D[i][j] = min(D[i][j], 1 + D[i][j + 1]);
      A[j] += D[i][j];
    }
  }
  if (!bo)
    cout << "-1" << endl;
  else {
    int res = (1 << 30);
    for (int i = 0; i < c; ++i) res = min(A[i], res);
    cout << res << endl;
  }
  return 0;
}
