#include <bits/stdc++.h>
using namespace std;
int table[128][10010];
int minShifts[128][10010];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.size(); j++) table[i][j] = (s[j] == '1' ? 1 : 0);
  }
  for (int i = 0; i < 128; i++)
    for (int j = 0; j < 10010; j++) minShifts[i][j] = 1 << 20;
  for (int i = 0; i < N; i++) {
    bool ex = false;
    int prevOc = -1;
    for (int j = 0; j < 2 * M; j++) {
      if (table[i][j % M] == 1) ex = true, prevOc = j % M;
      if (prevOc != -1)
        minShifts[i][j % M] =
            min(minShifts[i][j % M],
                min(abs(j % M - prevOc), abs(M - abs(j % M - prevOc))));
    }
    prevOc = -1;
    for (int j = 2 * M - 1; j >= 0; j--) {
      if (table[i][j % M] == 1) ex = true, prevOc = j % M;
      if (prevOc != -1)
        minShifts[i][j % M] =
            min(minShifts[i][j % M],
                min(abs(j % M - prevOc), abs(M - abs(j % M - prevOc))));
    }
    if (ex == false) {
      cout << -1 << endl;
      return 0;
    }
  }
  int totalShifts = 1 << 20;
  for (int i = 0; i < M; i++) {
    int curr = 0;
    for (int j = 0; j < N; j++) curr += minShifts[j][i];
    totalShifts = min(totalShifts, curr);
  }
  if (totalShifts >= 1 << 20)
    cout << -1 << endl;
  else
    cout << totalShifts << endl;
  return 0;
}
