#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101;
const int INFINITE = 0x3FFFFFFF;
int n, m;
char c;
set<int> t[MAXN];
int main() {
  scanf("%d %d\n", &n, &m);
  bool noSolution = false;
  for (int i = 1; i <= n; ++i) {
    bool notEmpty = false;
    for (int j = 1; j <= m; ++j) {
      scanf("%c", &c);
      if (c == '1') {
        t[i].insert(j);
        notEmpty = true;
      }
    }
    scanf("\n");
    if (!notEmpty) noSolution = true;
  }
  if (noSolution)
    printf("-1\n");
  else {
    int res = INFINITE;
    for (int i = 1; i <= m; ++i) {
      int tot = 0;
      for (int j = 1; j <= n; ++j) {
        set<int>::iterator p = t[j].find(i);
        if (p == t[j].end()) {
          p = t[j].upper_bound(i);
          int num = INFINITE;
          if (p != t[j].end()) num = min(num, (*p) - i);
          if (p-- != t[j].begin()) num = min(num, i - (*p));
          p = t[j].begin(), num = min(num, (*p) + m - i);
          p = t[j].end(), p--, num = min(num, i - (*p) + m);
          tot += num;
        }
      }
      if (res > tot) res = tot;
    }
    printf("%d\n", res);
  }
  fclose(stdin);
  fclose(stdout);
  return 0;
}
