#include <bits/stdc++.h>
using std::cout;
using std::endl;
char input[200][20000];
int closest[200][20000];
int left[200][20000];
int right[200][20000];
int abs(int x) {
  if (x >= 0)
    return x;
  else
    return -x;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", input[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) closest[i][j] = 10000000;
  for (int i = 0; i < n; i++) {
    bool check = false;
    for (int j = 0; j < m; j++)
      if (input[i][j] == '1') {
        closest[i][j] = 0;
        check = true;
      }
    if (!check) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    left[i][0] = 0;
    for (int j = 1; j < m; j++)
      if (input[i][j] == '1')
        left[i][j] = 0;
      else
        left[i][j] = left[i][j - 1] + 1;
    int idx = 0;
    while (input[i][idx] == '0') {
      left[i][idx] = left[i][m - 1] + idx + 1;
      idx++;
    }
    right[i][n - 1] = 0;
    for (int j = m - 2; j >= 0; j--)
      if (input[i][j] == '1')
        right[i][j] = 0;
      else
        right[i][j] = right[i][j + 1] + 1;
    idx = m - 1;
    while (input[i][idx] == '0') {
      right[i][idx] = right[i][0] + m - idx;
      idx--;
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      closest[i][j] = std::min(left[i][j], right[i][j]);
  if (0)
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) cout << closest[i][j] << " ";
      cout << endl;
    }
  int min = 10000000, sum;
  for (int j = 0; j < m; j++) {
    sum = 0;
    for (int i = 0; i < n; i++) sum += abs(closest[i][j]);
    if (sum < min) min = sum;
  }
  printf("%d\n", min);
  return 0;
}
