#include <bits/stdc++.h>
using namespace std;
vector<int> a[101];
int b[101] = {0};
int main() {
  int m, n;
  cin >> m >> n;
  string st;
  bool flag = true;
  for (int i = 1; i <= m; i++) {
    cin >> st;
    a[i].push_back(-10000);
    for (int j = 1; j <= n; j++) {
      char ch = st[j - 1];
      if (ch == '1') a[i].push_back(j);
    }
    if (a[i].size() == 1) {
      flag = false;
      break;
    }
    a[i][0] = a[i][a[i].size() - 1] - n;
    a[i].push_back(a[i][1] + n);
  }
  if (!flag) {
    cout << "-1" << endl;
    return 0;
  }
  int ans = 2147483647;
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = 1; j <= m; j++) {
      while (!((i >= a[j][b[j]]) && (i <= a[j][b[j] + 1]))) b[j]++;
      cnt += min(i - a[j][b[j]], a[j][b[j] + 1] - i);
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
