#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  char val;
  cin >> n >> m;
  vector<vector<int>> mat(n, vector<int>(m, INT_MAX));
  vector<vector<int>> pos(n, vector<int>(2));
  for (int i = 0; i < n; i++) {
    pos[i][0] = INT_MAX;
    pos[i][1] = INT_MIN;
    for (int j = 0; j < m; j++) {
      cin >> val;
      if (val == '1') {
        mat[i][j] = 0;
        if (pos[i][0] > j) pos[i][0] = j;
        if (pos[i][1] < j) pos[i][1] = j;
      }
    }
    if (pos[i][0] == INT_MAX || pos[i][1] == INT_MIN) {
      cout << -1 << "\n";
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (mat[i][0] == INT_MAX) mat[i][0] = m - pos[i][1];
    for (int j = 1; j < m; j++) {
      if (mat[i][j] > mat[i][j - 1] + 1) mat[i][j] = mat[i][j - 1] + 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (mat[i][m - 1] > pos[i][0] + 1) mat[i][m - 1] = pos[i][0] + 1;
    for (int j = m - 2; j >= 0; j--) {
      if (mat[i][j] > mat[i][j + 1] + 1) mat[i][j] = mat[i][j + 1] + 1;
    }
  }
  int moves = INT_MAX;
  for (int j = 0; j < m; j++) {
    int sum = 0;
    for (int i = 0; i < n; i++) sum += mat[i][j];
    if (moves > sum) moves = sum;
  }
  cout << moves << "\n";
  return 0;
}
static auto fast = [] {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  return 0;
}();
