#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> a[110];
int b[110];
int main(int argc, char *argv[]) {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    string stmp;
    cin >> stmp;
    for (int j = 0; j < ((int)((stmp).size())); ++j) {
      if (stmp[j] == '1') {
        a[i].push_back(j);
      }
    }
    if (((int)((a[i]).size())) == 0) {
      cout << "-1" << endl;
      return 0;
    }
    b[i] = ((int)((a[i]).size())) - 1;
  }
  int ans = (2100000000);
  for (int j = 0; j < m; ++j) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      int lval = j - a[i][b[i]];
      int rval = a[i][(b[i] + 1) % ((int)((a[i]).size()))] - j;
      if (lval < 0) lval += m;
      if (rval < 0) rval += m;
      sum += min(lval, rval);
      if (rval == 0) b[i] = (b[i] + 1) % ((int)((a[i]).size()));
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
