#include <bits/stdc++.h>
using namespace std;
const int inf = 10000000;
const int minf = -10000000;
int main() {
  int i, j, k, l, m, n, d, f, g, h, mx = -1, rez1, rez2;
  string s;
  int a[100][10000];
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> s;
    g = minf;
    for (j = 0; j < m; j++) {
      if (s[j] == '1') {
        g = j;
        a[i][j] = 0;
        continue;
      }
      a[i][j] = j - g;
    }
    if (g == minf) {
      cout << "-1" << endl;
      return 0;
    }
    h = 1;
    j = (g + 1) % m;
    while (s[j] != '1') {
      a[i][j] = h;
      h++;
      j = (j + 1) % m;
    }
    g = inf;
    for (j = m - 1; j >= 0; j--) {
      if (s[j] == '1') {
        g = j;
        continue;
      }
      a[i][j] = min(a[i][j], g - j);
    }
    h = 1;
    j = (g - 1 + m) % m;
    while (s[j] != '1') {
      a[i][j] = min(a[i][j], h);
      h++;
      j = (j - 1 + m) % m;
    }
  }
  int mn = 2000000000;
  for (j = 0; j < m; j++) {
    g = 0;
    for (i = 0; i < n; i++) g += a[i][j];
    mn = min(mn, g);
  }
  cout << mn << endl;
  return 0;
}
