#include <bits/stdc++.h>
const double PI = acos(-1.0);
using namespace std;
int n, m;
int a[110][10010];
vector<int> p[110];
int gao(int n, int m) {
  int i, j;
  int ans, tmp, sum, x;
  for (i = 0; i < 110; i++) p[i].clear();
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (a[i][j] == 1) break;
    }
    if (j >= m) return -1;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (a[i][j] == 1) p[i].push_back(j);
    }
  }
  ans = 0x7FFFFFFF;
  for (j = 0; j < m; j++) {
    sum = 0;
    for (i = 0; i < n; i++) {
      tmp = 0x7FFFFFFF;
      x = lower_bound(p[i].begin(), p[i].end(), j) - p[i].begin();
      if (x != (int)p[i].size()) tmp = min(tmp, abs(j - p[i][x]));
      if (x) tmp = min(tmp, abs(j - p[i][x - 1]));
      tmp = min(tmp, abs(m - p[i][p[i].size() - 1] + j));
      tmp = min(tmp, abs(p[i][0] + m - j));
      sum += tmp;
    }
    ans = min(ans, sum);
  }
  return ans;
}
int main() {
  int n, m, i, j;
  while (~scanf("%d%d", &n, &m)) {
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) scanf("%1d", &a[i][j]);
    }
    printf("%d\n", gao(n, m));
  }
  return 0;
}
