#include <bits/stdc++.h>
using namespace std;
vector<int> l[100];
int busca(int n, int linha, int i, int j) {
  if (i >= j - 1) {
    if ((size_t)i == l[linha].size() - 1) {
      return l[linha][i];
    } else {
      if (abs(n - l[linha][i]) < abs(n - l[linha][i + 1])) {
        return l[linha][i];
      } else {
        return l[linha][i + 1];
      }
    }
  }
  int mid = i + (j - i) / 2;
  int dif = n - l[linha][mid];
  if (dif == 0) {
    return l[linha][mid];
  } else if (dif < 0) {
    return busca(n, linha, i, mid);
  } else {
    return busca(n, linha, mid, j);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  bool cagou = false;
  for (int y = 0; y < n; ++y) {
    bool tudo_zero = true;
    for (int x = 0; x < m; ++x) {
      char c;
      cin >> c;
      if (c == '1') {
        tudo_zero = false;
        l[y].push_back(x);
      }
    }
    if (tudo_zero) {
      cagou = true;
    }
  }
  if (cagou) {
    printf("-1\n");
    return 0;
  }
  int min_col = -1;
  int min_sum = 0;
  for (int x = 0; x < m; ++x) {
    int sum = 0;
    for (int y = 0; y < n; ++y) {
      int r1 = abs(x - busca(x, y, 0, l[y].size() - 1));
      int r2 = l[y][0] + abs(x - m);
      int r3 = m - l[y][l[y].size() - 1] + x;
      int m1 = (r2) < (r3) ? (r2) : (r3);
      sum += (r1) < (m1) ? (r1) : (m1);
    }
    if (min_col == -1 || sum < min_sum) {
      min_col = x;
      min_sum = sum;
    }
  }
  printf("%d\n", min_sum);
  return 0;
}
