#include <bits/stdc++.h>
using namespace std;
int cnt(vector<vector<int> >& v, int C, int col, int minm) {
  int n = (int)v.size(), r = 0;
  for (int i = 0; i < n; i++) {
    if (binary_search(v[i].begin(), v[i].end(), col)) continue;
    int k = v[i].size();
    if (col < v[i][0]) {
      int x = min(v[i][0] - col, col - v[i][k - 1] + C);
      r += x;
    } else if (col > v[i][k - 1]) {
      int x = min(col - v[i][k - 1], C - col + v[i][0]);
      r += x;
    } else {
      int lo = 0, hi = k - 2;
      while (lo + 1 < hi) {
        int m = lo + (hi - lo) / 2;
        if (col < v[i][m])
          hi = m - 1;
        else
          lo = m;
      }
      if (col > v[i][lo + 1] && lo + 2 < k && col < v[i][lo + 2])
        lo++;
      else if (lo - 1 >= 0 && col > v[i][lo - 1] && col < v[i][lo])
        lo--;
      int x = min(col - v[i][lo], v[i][lo + 1] - col);
      r += x;
    }
    if (r >= minm) return r;
  }
  return r;
}
int main() {
  int n, m, x;
  string s;
  cin >> n >> m;
  vector<vector<int> > v;
  for (int i = 0; i < n; i++) {
    cin >> s;
    vector<int> t;
    for (int j = 0; j < m; j++) {
      if (s[j] == '1') t.push_back(j);
    }
    v.push_back(t);
    if ((int)t.size() == 0) {
      cout << -1;
      return 0;
    }
  }
  int res = INT_MAX;
  for (int i = 0; i < m; i++) {
    res = min(res, cnt(v, m, i, res));
  }
  cout << res;
  return 0;
}
