#include <bits/stdc++.h>
using namespace std;
vector<int> mat[110];
int p[110];
char buff[20010];
int solve(int n, int m) {
  int i, j, ret = n * m + 1, cur;
  for (i = 0; i < n; ++i) {
    if (!mat[i].size()) return -1;
  }
  memset(p, 0, sizeof(p));
  for (i = 0; i < (m << 1); ++i) {
    for (cur = j = 0; j < n; ++j) {
      if (p[j] + 1 < mat[j].size() &&
          abs(i - mat[j][p[j]]) > abs(i - mat[j][p[j] + 1]))
        ++p[j];
      cur += abs(i - mat[j][p[j]]);
    }
    ret = min(ret, cur);
  }
  return ret;
}
int main() {
  int n, m, i, j;
  while (~scanf("%d %d", &n, &m)) {
    for (i = 0; i < n; ++i) {
      scanf("%s", buff);
      for (j = 0; j < m; ++j) buff[j + m] = buff[j];
      mat[i].clear();
      for (j = 0; j < (m << 1); ++j) {
        if (buff[j] == '1') mat[i].push_back(j);
      }
    }
    printf("%d\n", solve(n, m));
  }
  return 0;
}
