#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, dp[110][100100], sum[100100] = {0};
char maze[110][100100];
int main(void) {
  scanf("%d%d", &n, &m);
  memset(dp, 63, sizeof(dp));
  for ((i) = 0; i < n; i++) {
    scanf("%s", maze[i]);
  }
  int ans = INT_MAX, ok = 1;
  for ((i) = 0; i < n; i++) {
    int last = -1;
    for (j = 0; j < m; j++) {
      if (maze[i][j] == '1') last = j;
      if (last != -1) {
        dp[i][j] = min(dp[i][j], abs(j - last));
      }
    }
    if (last == -1) {
      ok = 0;
      break;
    } else {
      for (j = 0; j < m && maze[i][j] != '1'; j++) {
        dp[i][j] = min(dp[i][j], j + (m - last));
      }
    }
    last = -1;
    for (j = m - 1; j >= 0; j--) {
      if (maze[i][j] == '1') last = j;
      if (last != -1) {
        dp[i][j] = min(dp[i][j], abs(j - last));
      }
    }
    if (last != -1) {
      for (j = m - 1; j >= 0 && maze[i][j] != '1'; j--) {
        dp[i][j] = min(dp[i][j], m - j + last);
      }
    }
  }
  for ((j) = 0; j < m; j++)
    for ((i) = 0; i < n; i++) sum[j] += dp[i][j];
  for ((i) = 0; i < m; i++) ans = min(ans, sum[i]);
  printf("%d\n", !ok ? -1 : ans);
  return 0;
}
