#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
void debugarr(int* arr, int n) {
  cout << "[";
  for (int i = 0; i < n; i++) cout << arr[i] << " ";
  cout << "]" << endl;
}
void debugvec(vector<int> arr) {
  cout << "[";
  for (int i = 0; i < ((int)(arr.size())); i++) cout << arr[i] << " ";
  cout << "]" << endl;
}
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  int mat[n][2 * m];
  long long cost1[n][2 * m];
  long long cost2[n][2 * m];
  long long cost[n][m];
  bool sol = true;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    str = str + str;
    int sum = 0;
    for (int j = 0; j < 2 * m; j++) {
      if (str[j] == '1')
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
      sum += mat[i][j];
    }
    if (sum == 0) sol = false;
  }
  if (sol) {
    for (int k = 0; k < n; k++) {
      long long c = (long long)1e15;
      for (int l = 0; l < 2 * m; l++) {
        if (mat[k][l] == 1) {
          cost1[k][l] = 0;
          c = 0;
        } else {
          c++;
          cost1[k][l] = c;
        }
      }
      c = (long long)1e15;
      for (int pp = 2 * m - 1; pp >= 0; pp--) {
        if (mat[k][pp] == 1) {
          cost2[k][pp] = 0;
          c = 0;
        } else {
          c++;
          cost2[k][pp] = c;
        }
      }
      for (int iii = 0; iii < m; iii++) {
        cost[k][iii] =
            ((cost1[k][m + iii]) < (cost2[k][iii]) ? (cost1[k][m + iii])
                                                   : (cost2[k][iii]));
      }
    }
    long long ans = (long long)1e15;
    for (int q = 0; q < m; q++) {
      long long tot = 0;
      for (int tt = 0; tt < n; tt++) tot += cost[tt][q];
      ans = ((ans) < (tot) ? (ans) : (tot));
    }
    cout << ans << endl;
  } else
    cout << "-1" << endl;
  return 0;
}
