#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 1;
int n, m, minMoves[MAXN], R[MAXN], dp[101][MAXN];
bool ok;
string s[MAXN];
void calMinRot(string s, int idx) {
  int N = s.size();
  memset(minMoves, -1, sizeof(minMoves));
  memset(R, -1, sizeof(R));
  for (int i = 0; i < N; i++) {
    if (s[i] == '1')
      minMoves[i] = 0;
    else if (i > 0 and minMoves[i - 1] != -1)
      minMoves[i] = minMoves[i - 1] + 1;
  }
  if (minMoves[0] == -1) {
    int fromRight = -1;
    for (int i = N - 1; i > 0; i--)
      if (minMoves[i] != -1) {
        fromRight = minMoves[i];
        break;
      }
    if (fromRight == -1) {
      ok = 0;
      return;
    }
    minMoves[0] = fromRight + 1;
    for (int i = 1; i < N and minMoves[i] == -1; i++)
      minMoves[i] = minMoves[i - 1] + 1;
  }
  for (int i = N - 1; i >= 0; i--) {
    if (s[i] == '1')
      R[i] = 0;
    else if (i != N - 1 and R[i + 1] != -1)
      R[i] = R[i + 1] + 1;
  }
  if (R[N - 1] == -1) {
    int fromLeft = -1;
    for (int i = 0; i < N; i++)
      if (R[i] != -1) {
        fromLeft = R[i];
        break;
      }
    R[N - 1] = fromLeft + 1;
    for (int i = N - 2; i >= 0 and R[i] == -1; i--) R[i] = R[i + 1] + 1;
  }
  for (int i = 0; i < N; i++) {
    dp[idx][i] = min(minMoves[i], R[i]);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    ok = true;
    calMinRot(s[i], i);
    if (not ok) {
      printf("-1\n");
      return 0;
    }
  }
  long long ans = numeric_limits<long long>::max();
  for (int j = 0; j < m; j++) {
    long long cur = 0;
    for (int i = 0; i < n; i++) cur += dp[i][j];
    ans = min(ans, cur);
  }
  cout << ans << endl;
  return 0;
}
