#include <bits/stdc++.h>
using namespace std;
vector<int> pos[110];
int n;
int main() {
  int m;
  cin >> n >> m;
  int i, j;
  char c;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> c;
      if (c == '1') pos[i].push_back(j);
    }
    if (pos[i].empty()) {
      cout << -1 << endl;
      return 0;
    }
  }
  int mm = 100000000;
  int k;
  for (k = 0; k < m; k++) {
    int sum = 0;
    for (i = 0; i < n; i++) {
      vector<int>::iterator it = lower_bound(pos[i].begin(), pos[i].end(), k);
      if (it != pos[i].end() && *it == k)
        continue;
      else {
        int x, y;
        if (it == pos[i].end()) {
          x = k - pos[i].back();
          y = m - k + pos[i].front();
        } else if (it == pos[i].begin()) {
          x = *it - k;
          y = m - pos[i].back() + k;
        } else {
          x = *it - k;
          y = k - *(it - 1);
        }
        sum += min(x, y);
      }
    }
    if (sum < mm) mm = sum;
  }
  cout << mm << endl;
  return 0;
}
