#include <bits/stdc++.h>
using namespace std;
template <typename T>
int size(T& a) {
  return (int)a.size();
}
template <typename T>
T sqr(T a) {
  return a * a;
}
void dbg(const char* fmt, ...) {}
const int MAXN = 10010;
int ans[MAXN];
int state[MAXN];
char s[MAXN];
void update(int& a, int b) {
  if (a == -1 || a > b) {
    a = b;
  }
}
int main() {
  int n, m;
  scanf("%d %d\n", &n, &m);
  for (int i = (0); i < (n); ++i) {
    gets(s);
    memset((state), (-1), sizeof(state));
    int lastPos = -1;
    for (int j = (0); j < (m); ++j) {
      if (s[j] == '1') {
        state[j] = 0;
        lastPos = j;
      } else if (lastPos != -1) {
        update(state[j], j - lastPos);
      }
    }
    if (lastPos == -1) {
      printf("-1\n");
      exit(0);
    }
    for (int j = (0); j < (m); ++j) {
      if (s[j] == '1') break;
      update(state[j], m - lastPos + j);
    }
    lastPos = -1;
    for (int j = (m)-1; j >= 0; --j) {
      if (s[j] == '1') {
        state[j] = 0;
        lastPos = j;
      } else if (lastPos != -1) {
        update(state[j], lastPos - j);
      }
    }
    for (int j = (m)-1; j >= 0; --j) {
      if (s[j] == '1') break;
      update(state[j], m - j + lastPos);
    }
    for (int j = (0); j < (m); ++j) {
      ans[j] += state[j];
    }
  }
  int ret = 1000000000;
  for (int i = (0); i < (m); ++i) {
    ret = min(ret, ans[i]);
  }
  printf("%d\n", ret);
}
