#include <bits/stdc++.h>
using namespace std;
const char NEWL = '\n', SPACE = ' ';
int GetDiff(int a, int b, int size) {
  return min((a - b + size) % size, (b - a + size) % size);
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> indxOf1(n);
  cin.get();
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    for (int j = 0; j < m; j++) {
      if (s[j] == '1') {
        indxOf1[i].push_back(j);
      }
    }
    if (indxOf1[i].empty()) {
      cout << "-1";
      return;
    }
  }
  vector<vector<int>::iterator> iters(n);
  for (int i = 0; i < n; i++) {
    iters[i] = indxOf1[i].begin();
  }
  long long cntMin = LLONG_MAX;
  for (int j = 0; j < m; j++) {
    long long cntAll = 0;
    for (int i = 0; i < n; i++) {
      auto& arr = indxOf1[i];
      int diff_cur = abs(*iters[i] - j);
      auto itPlus1 = iters[i] + 1;
      if (itPlus1 != arr.end()) {
        int diff_incr = abs(*itPlus1 - j);
        if (diff_incr <= diff_cur) {
          iters[i] = itPlus1;
          diff_cur = diff_incr;
        }
      }
      cntAll += min({GetDiff(*arr.begin(), j, m),
                     GetDiff(*(arr.end() - 1), j, m), diff_cur});
    }
    if (cntAll < cntMin) {
      cntMin = cntAll;
    }
  }
  cout << cntMin;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tests = 1;
  while (tests--) {
    solve();
    cout << NEWL;
  }
}
