#include <bits/stdc++.h>
using namespace std;
bool matrix[10001];
int m_log[10001];
int n, m;
char c;
int v = 0;
int l, r;
int dpr[10001];
int dpl[10001];
void dp_line() {
  int j = m - 1;
  dpl[0] = 0;
  if (matrix[0] == 1)
    dpl[0] = 0;
  else {
    dpl[0] = 1;
    while (matrix[j] == 0 && j >= m / 2) {
      j--;
      dpl[0]++;
    }
  }
  dpr[0] = 0;
  j = 0;
  if (matrix[m - 1] == 1)
    dpr[m - 1] = 0;
  else {
    dpr[m - 1] = 1;
    while (matrix[j] == 0) {
      dpr[m - 1]++;
      j++;
    }
  }
  for (int j = 1; j < m; j++) {
    if (matrix[j] == 0)
      dpl[j] = dpl[j - 1] + 1;
    else
      dpl[j] = 0;
  }
  for (int j = m - 2; j >= 0; j--) {
    if (matrix[j] == 0)
      dpr[j] = dpr[j + 1] + 1;
    else
      dpr[j] = 0;
  }
}
int main() {
  cin >> n >> m;
  int k = m / 2 + 1;
  for (int i = 0; i < m; i++) {
    m_log[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    v = 0;
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == '0')
        matrix[j] = 0;
      else {
        matrix[j] = 1;
        v++;
      }
    }
    if (v == 0) {
      cout << -1 << endl;
      return 0;
    }
    dp_line();
    for (int j = 0; j < m; j++) {
      m_log[j] += min(dpl[j], dpr[j]);
    }
  }
  int min = m_log[0];
  for (int i = 1; i < m; i++) {
    if (m_log[i] < min) min = m_log[i];
  }
  cout << min << endl;
  return 0;
}
