#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> s;
vector<vector<int> > lft;
vector<vector<int> > rht;
int l(int i, int j) {
  if (s[i][j] == '1') return 0;
  int& res = lft[i][j];
  if (res != -1) return res;
  res = 1 + l(i, (j - 1 + m) % m);
  return res;
}
int r(int i, int j) {
  if (s[i][j] == '1') return 0;
  int& res = rht[i][j];
  if (res != -1) return res;
  res = 1 + r(i, (j + 1) % m);
  return res;
}
int main() {
  cin >> n >> m;
  s = vector<string>(n);
  lft = vector<vector<int> >(n, vector<int>(m, -1));
  rht = vector<vector<int> >(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i) cin >> s[i];
  for (int i = 0; i < n; ++i)
    if (count((s[i]).begin(), (s[i]).end(), '1') == 0) {
      cout << -1 << endl;
      return 0;
    }
  int res = 1e7;
  for (int i = 0; i < m; ++i) {
    int s = 0;
    for (int j = 0; j < n; ++j) s += min(l(j, i), r(j, i));
    res = min(res, s);
  }
  if (res >= 1e7)
    cout << -1 << endl;
  else
    cout << res << endl;
  return 0;
}
