#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;
const int MAXM = 1e4 + 100;
bool tab[MAXN][MAXM];
vector<int> ones[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    int j = 0;
    for (auto x : str) {
      tab[i][j] = x == '1';
      if (tab[i][j]) ones[i].push_back(j);
      j++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (ones[i].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    sort(ones[i].begin(), ones[i].end());
  }
  int mans = 100000000;
  for (int i = 0; i < m; i++) {
    int ans = 0;
    for (int j = 0; j < n; j++) {
      if (!tab[j][i]) {
        vector<int>::iterator it =
            lower_bound(ones[j].begin(), ones[j].end(), i);
        int dif = 100000000;
        if (it != ones[j].end()) {
          int nn = *it;
          dif = min(dif, min(nn - i, m - nn + i));
          if (it != ones[j].begin()) {
            it--;
            int nn = *it;
            dif = min(dif, min(i - nn, m - i + nn));
          }
        }
        int ans1 = abs(i - ones[j][ones[j].size() - 1]);
        ans1 = min(ans1, m - ans1);
        dif = min(dif, ans1);
        ans1 = abs(i - ones[j][0]);
        ans1 = min(ans1, m - ans1);
        dif = min(dif, ans1);
        ans += dif;
      }
    }
    mans = min(ans, mans);
  }
  cout << mans << endl;
  return 0;
}
