#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int MAXM = 10005;
int N, M;
char buf[MAXM];
int d[MAXN][MAXM];
int dp[MAXM];
vector<int> one;
int main(int argc, char *argv[]) {
  scanf("%d%d", &N, &M);
  one.reserve(M);
  memset(d, 63, sizeof d);
  for (int i = 0, _i = N; i < _i; ++i) {
    one.clear();
    scanf("%s", buf);
    for (int j = 0, _j = M; j < _j; ++j) {
      if (buf[j] == '1') one.push_back(j);
    }
    if (one.empty()) {
      printf("-1\n");
      return 0;
    }
    int rmost = -(1 << 30);
    for (int j = 0, _j = M; j < _j; ++j) {
      if (buf[j] == '1') {
        rmost = j;
        continue;
      }
      d[i][j] = min(M - one.back() + j, j - rmost);
      d[i][j] = min(M - j + *one.begin(), d[i][j]);
      vector<int>::iterator it = lower_bound(one.begin(), one.end(), j);
      if (it != one.end()) {
        d[i][j] = min(d[i][j], abs(*it - j));
      }
      dp[j] += d[i][j];
    }
  }
  cout << *min_element(dp, dp + M);
  return 0;
}
