#include <bits/stdc++.h>
using namespace std;
long long a[111][10011] = {0}, b[10011] = {0}, x, n, m;
void leftToright(long long i, long long j) {
  long long k = 1;
  while (true) {
    j++;
    if (j == m) j = 0;
    if (a[i][j] > k)
      a[i][j] = k++;
    else
      return;
  }
}
void rightToleft(long long i, long long j) {
  long long k = 1;
  while (true) {
    j--;
    if (j == -1) j = m - 1;
    if (a[i][j] > k)
      a[i][j] = k++;
    else
      return;
  }
}
int main() {
  bool c;
  string s;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == '1')
        a[i][j] = 0;
      else
        a[i][j] = m;
    }
  }
  for (int i = 0; c = 1 && i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 0) {
        leftToright(i, j);
        rightToleft(i, j);
        c = 0;
      }
    }
    if (c) break;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      b[j] += a[i][j];
    }
  }
  long long mine = b[0];
  for (int j = 0; j < m; j++) mine = min(mine, b[j]);
  if (c)
    cout << "-1\n";
  else
    cout << mine << "\n";
  return 0;
}
