#include <bits/stdc++.h>
using namespace std;
const int maxn = 1E2 + 10, maxm = 2 * 1E4 + 10;
bool q[maxn][maxm];
int _move[maxn][maxm];
int main() {
  int N, M;
  cin >> N >> M;
  for (decltype(N) i = 0; i < N; i++) {
    string s;
    cin >> s;
    int lst_idx = -1;
    for (decltype(M) j = 0; j < M; j++) {
      q[i][j] = q[i][M + j] = (s[j] - '0');
      _move[i][j] = _move[i][M + j] = 2 * 1E8;
    }
  }
  for (decltype(N) i = 0; i < N; i++) {
    int lpos = -1E9;
    for (decltype(2 * M) j = 0; j < 2 * M; j++) {
      if (q[i][j] == 1) lpos = j;
      _move[i][j] = min(_move[i][j], j - lpos);
    }
    int rpos = 1E9;
    for (decltype(2 * M) j = 2 * M - 1; j >= 0; j--) {
      if (q[i][j] == 1) rpos = j;
      _move[i][j] = min(_move[i][j], rpos - j);
    }
    for (decltype(M) j = 0; j < M; j++) {
      _move[i][j] = min(_move[i][j], _move[i][M + j]);
    }
  }
  long long ans = 1E18;
  for (decltype(M) j = 0; j < M; j++) {
    long long cur = 0;
    for (decltype(N) i = 0; i < N; i++) {
      cur += _move[i][j];
    }
    ans = min(cur, ans);
  }
  cout << (ans > N * M ? -1 : ans) << endl;
  return 0;
}
