#include <bits/stdc++.h>
using namespace std;
int n, m;
char c[1111][11111];
int sum[11111];
int cost[11111];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", &c[i]);
  for (int i = 0; i < n; i++) {
    int l1 = -1;
    int sl = -1, sr = -1;
    for (int j = 0; j < m; j++) {
      if (c[i][j] == '1') {
        sr = l1 = j;
        cost[j] = 0;
      } else {
        cost[j] = (l1 == -1) ? (0x3fffffff) : (j - l1);
      }
    }
    l1 = -1;
    for (int j = m - 1; j >= 0; j--) {
      if (c[i][j] == '1') {
        sl = l1 = j;
        cost[j] = 0;
      } else {
        cost[j] = min(cost[j], (l1 == -1) ? (0x3fffffff) : (l1 - j));
      }
    }
    if (sl != -1) {
      for (int j = 0; j < sl; j++) cost[j] = min(cost[j], m - sr + j);
      for (int j = sr + 1; j < m; j++) cost[j] = min(cost[j], sl + m - j);
    } else {
      printf("-1\n");
      return 0;
    }
    for (int j = 0; j < m; j++) sum[j] += cost[j];
  }
  int ans = 0x3fffffff;
  for (int i = 0; i < m; i++) {
    if (sum[i] < ans) ans = sum[i];
  }
  printf("%d\n", (ans >= 0x3fffffff) ? (-1) : (ans));
}
