#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  scanf("%d %d\n", &N, &M);
  int i, j;
  static char data[105][10005];
  static int prev[105];
  static int next[105];
  for (i = 0; i < N; i++) {
    scanf("%s\n", data[i]);
    char flag = 0;
    for (j = 0; j < M; j++) {
      if (data[i][j] == '1') {
        flag = 1;
        break;
      }
    }
    if (!flag) {
      printf("-1\n");
      return 0;
    }
    j = 0;
    while (data[i][j] == '0') j++;
    next[i] = j;
    j = M - 1;
    while (data[i][j] == '0') j--;
    prev[i] = j;
  }
  int res = 1000000000;
  int now, t1, t2;
  for (j = 0; j < M; j++) {
    now = 0;
    for (i = 0; i < N; i++) {
      t1 = (next[i] - j + M) % M;
      t2 = (j - prev[i] + M) % M;
      if (t1 < t2)
        now += t1;
      else
        now += t2;
    }
    if (now < res) res = now;
    for (i = 0; i < N; i++) {
      if (next[i] == j) {
        prev[i] = j;
        next[i] = (j + 1) % M;
        while (data[i][next[i]] == '0') next[i] = (next[i] + 1) % M;
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
