#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110, MAXM = 10010;
char c[MAXN][MAXM];
int res[MAXM];
int dist(int a, int b, int m) { return min(abs(a - b), m - abs(a - b)); }
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (n); ++i) {
    scanf("%s", c[i]);
  }
  for (int i = 0; i < (n); ++i) {
    bool ok = false;
    for (int j = 0; j < (m); ++j) {
      if (c[i][j] == '1') {
        ok = true;
      }
    }
    if (!ok) {
      puts("-1");
      return 0;
    }
    int first = -1, last = -1;
    vector<int> best(m, MAXM);
    for (int j = 0; j < (m); ++j) {
      if (c[i][j] == '1') {
        if (first == -1) {
          first = j;
        }
        last = j;
        best[j] = 0;
      } else if (last != -1) {
        best[j] = min(best[j], dist(j, last, m));
        best[j] = min(best[j], dist(j, first, m));
      }
    }
    first = -1;
    last = -1;
    for (int j = m - 1; j >= 0; --j) {
      if (c[i][j] == '1') {
        if (first == -1) {
          first = j;
        }
        last = j;
      } else if (last != -1) {
        best[j] = min(best[j], dist(j, last, m));
        best[j] = min(best[j], dist(j, first, m));
      }
    }
    for (int j = 0; j < (m); ++j) {
      res[j] += best[j];
    }
  }
  int R = MAXN * MAXM;
  for (int i = 0; i < (m); ++i) {
    R = min(R, res[i]);
  }
  printf("%d\n", R);
  return 0;
}
