#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m;
char a[110][10010];
int l[110][10010], r[110][10010];
int calc_l(int i, int j) {
  if (l[i][j] >= 0) return l[i][j];
  if (a[i][j] == '1') return l[i][j] = 0;
  return l[i][j] = calc_l(i, (j + m - 1) % m) + 1;
}
int calc_r(int i, int j) {
  if (r[i][j] >= 0) return r[i][j];
  if (a[i][j] == '1') return r[i][j] = 0;
  return r[i][j] = calc_r(i, (j + 1) % m) + 1;
}
int main() {
  memset(l, -1, sizeof(l));
  memset(r, -1, sizeof(r));
  cin >> n >> m;
  for (int i = 0; i < (int)(n); ++i) cin >> a[i];
  for (int i = 0; i < (int)(n); ++i) {
    bool found = false;
    for (int j = 0; j < (int)(m); ++j)
      if (a[i][j] == '1') found = true;
    if (!found) {
      cout << -1 << endl;
      return 0;
    }
  }
  int val = INF;
  for (int t = 0; t < (int)(m); ++t) {
    int ans = 0;
    for (int i = 0; i < (int)(n); ++i) {
      ans += min(calc_l(i, t), calc_r(i, t));
    }
    val = min(ans, val);
  }
  cout << val << endl;
  return 0;
}
