#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979;
const double PI2 = 6.28318530717958;
const double PId2 = 1.570796326794895;
inline long long pw(long long n, int p) {
  long long ans = 1;
  for (int i = 0; i < p; i++) ans *= n;
  return ans;
}
inline vector<int> ReadVI(int count) {
  vector<int> arrayname(count);
  for (int i = 0; i < int(count); i++) cin >> arrayname[i];
  return arrayname;
};
inline vector<long long> ReadVlong(int count) {
  vector<long long> arrayname(count);
  for (int i = 0; i < int(count); i++) cin >> arrayname[i];
  return arrayname;
};
const int MOD = 100000000;
const int MAXVALUE = 101;
int main() {
  int n, m;
  char c;
  cin >> n >> m;
  vector<vector<int> > ed(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == '1') ed[i].push_back(j);
    }
    if (ed[i].size() == 0) return cout << -1, 0;
  }
  int minimum = INT_MAX;
  for (int j = 0; j < m; j++) {
    int st = 0;
    for (int i = 0; i < n; i++) {
      auto ir = lower_bound(ed[i].begin(), ed[i].end(), j);
      int r = *ir, l = 999;
      if (ir == ed[i].end()) r = ed[i][0];
      if (r != j) {
        if (ir == ed[i].end()) r = ed[i][0];
        if (ir == ed[i].begin())
          l = ed[i][ed[i].size() - 1];
        else {
          ir--;
          l = *ir;
        }
        r = (m + r - j) % m;
        l = (m + j - l) % m;
        st += min(r, l);
      }
    }
    minimum = min(minimum, st);
  }
  cout << minimum;
  return 0;
}
