#include <bits/stdc++.h>
using namespace std;
int a[101][10001];
int d[101][10001];
int main() {
  int n, m, q = 1;
  cin >> n >> m;
  int i, j, t;
  char k;
  for (i = 1; i <= n; i++) {
    t = 1;
    for (j = 1; j <= m; j++) {
      cin >> k;
      if (k == '1') a[i][t++] = j;
    }
    if (t == 1) q = 0;
    a[i][0] = a[i][t - 1] - m;
    a[i][t] = a[i][1] + m;
  }
  if (!q) {
    cout << "-1" << endl;
    return 0;
  }
  int sum, min = 10000000;
  for (i = 1; i <= m; i++) {
    sum = 0;
    for (j = 1; j <= n; j++) {
      t = 0;
      while (i >= a[j][t]) t++;
      if (a[j][t] - i >= i - a[j][t - 1])
        sum += (i - a[j][t - 1]);
      else
        sum += (a[j][t] - i);
      if (sum >= min) break;
    }
    if (sum < min) min = sum;
  }
  cout << min << endl;
  return 0;
}
