#include <bits/stdc++.h>
using namespace std;
int n;
int m;
vector<vector<char> > grid;
vector<vector<int> > mindist;
vector<int> first;
vector<int> last;
bool notpossible = false;
int main() {
  scanf("%d %d\n", &n, &m);
  grid.assign(n, vector<char>(m));
  first.assign(n, -1);
  last.assign(n, -1);
  mindist.assign(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%c", &grid[i][j]);
      if (grid[i][j] == '1') {
        if (first[i] == -1) {
          first[i] = j;
        }
        last[i] = j;
      }
    }
    if (last[i] == -1) {
      notpossible = true;
    }
    scanf("\n");
  }
  if (notpossible) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int current = m - last[i];
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '1') {
        current = 0;
      }
      mindist[i][j] = current;
      current++;
    }
    current = first[i] + 1;
    for (int j = m - 1; j >= 0; j--) {
      if (grid[i][j] == '1') {
        current = 0;
      }
      mindist[i][j] = min(mindist[i][j], current);
      current++;
    }
  }
  int mincolumn = 0;
  for (int i = 0; i < n; i++) {
    mincolumn += mindist[i][0];
  }
  for (int i = 1; i < m; i++) {
    int currentcolumn = 0;
    for (int j = 0; j < n; j++) {
      currentcolumn += mindist[j][i];
    }
    mincolumn = min(mincolumn, currentcolumn);
  }
  printf("%d\n", mincolumn);
  return 0;
}
