#include <bits/stdc++.h>
int MATRIX[111][10010], CLOSEST1[111][10010][2];
int N, M;
int colMinShifts(int col) {
  int sum = 0, left, right;
  for (int i = 0; i < N; i++) {
    if (MATRIX[i][col] == 1) {
      continue;
    }
    left = CLOSEST1[i][col][0];
    right = CLOSEST1[i][col][1];
    if (left == -1) {
      left = col + 1;
      left += (MATRIX[i][M - 1] == 1) ? (0) : (M - 1 - CLOSEST1[i][M - 1][0]);
    } else {
      left = (col - left);
    }
    if (right == -1) {
      right = (M - col);
      right += (MATRIX[i][0] == 1) ? (0) : (CLOSEST1[i][0][1]);
    } else {
      right = (right - col);
    }
    sum += ((left) < (right) ? (left) : (right));
  }
  return sum;
}
int minShifts() {
  int min = -1, colRes;
  for (int i = 0; i < N; i++) {
    int lastOne = -1;
    for (int j = 0; j < M; j++) {
      if (MATRIX[i][j] == 1) {
        lastOne = j;
      }
      CLOSEST1[i][j][0] = lastOne;
    }
    if (lastOne == -1) {
      return -1;
    }
  }
  for (int i = 0; i < N; i++) {
    int lastOne = -1;
    for (int j = (M - 1); j >= 0; j--) {
      if (MATRIX[i][j] == 1) {
        lastOne = j;
      }
      CLOSEST1[i][j][1] = lastOne;
    }
    if (lastOne == -1) {
      return -1;
    }
  }
  for (int j = 0; j < M; j++) {
    colRes = colMinShifts(j);
    if (colRes == -1) {
      return -1;
    }
    if (min == -1 || colRes < min) {
      min = colRes;
    }
  }
  return min;
}
void solve() {
  FILE *in = stdin;
  FILE *out = stdout;
  fscanf(in, "%d %d", &N, &M);
  char c;
  memset(CLOSEST1, 0, sizeof(CLOSEST1));
  fscanf(in, "%c", &c);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      fscanf(in, "%c", &c);
      if (c == '0') {
        MATRIX[i][j] = 0;
      } else {
        MATRIX[i][j] = 1;
      }
    }
    fscanf(in, "%c", &c);
  }
  int res = minShifts();
  fprintf(out, "%d\n", res);
}
int main() {
  solve();
  return 0;
}
