#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, i, j, k, ans, mm, ss;
  cin >> n >> m;
  vector<string> s(n);
  bool valid = true;
  for (i = 0; i < n; i++) {
    cin >> s[i];
    bool has1 = false;
    for (auto ch : s[i]) {
      if (ch == '1') {
        has1 = true;
        break;
      }
    }
    if (!has1) valid = false;
    s[i] = s[i] + s[i] + s[i];
  }
  if (!valid)
    cout << "-1\n";
  else {
    mm = 3 * m;
    vector<vector<int>> ll(n, vector<int>(mm)), rr(n, vector<int>(mm));
    for (i = 0; i < n; i++) {
      ll[i][0] = 0;
      for (j = 1; j < mm; j++) {
        if (s[i][j] == '1')
          ll[i][j] = j;
        else
          ll[i][j] = ll[i][j - 1];
      }
      rr[i][mm - 1] = mm - 1;
      for (j = mm - 2; j >= 0; j--) {
        if (s[i][j] == '1')
          rr[i][j] = j;
        else
          rr[i][j] = rr[i][j + 1];
      }
    }
    ans = n * m * 100;
    for (i = m; i < m + m; i++) {
      ss = 0;
      for (j = 0; j < n; j++) ss += min(abs(i - ll[j][i]), abs(i - rr[j][i]));
      ans = min(ans, ss);
    }
    cout << ans << "\n";
  }
  return 0;
}
