#include <bits/stdc++.h>
int main() {
  int n, m, left, right, min[10000], cnt[10000] = {0}, ans = 10000000;
  char t[100][10000 + 1];
  bool flag;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", t[i]);
  for (int i = 0; i < n; i++) {
    flag = false;
    for (int j = 0; j < m; j++)
      if (t[i][j] == '1') {
        flag = true;
        break;
      }
    if (!flag) break;
    for (int j = 0; j < m; j++) min[j] = 9999999;
    left = -1;
    for (int j = 0; j < 2 * m; j++) {
      if (t[i][j % m] == '1') left = j;
      if (left != -1 && min[j % m] > (j - left) % m)
        min[j % m] = (j - left) % m;
    }
    right = -1;
    for (int j = 2 * m - 1; j >= 0; j--) {
      if (t[i][j % m] == '1') right = j;
      if (right != -1 && min[j % m] > (right - j) % m)
        min[j % m] = (right - j) % m;
    }
    for (int j = 0; j < m; j++) cnt[j] += min[j];
  }
  for (int j = 0; j < m; j++)
    if (ans > cnt[j]) ans = cnt[j];
  if (flag)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
