#include <bits/stdc++.h>
using namespace std;
const int N = 102;
const int M = 1e4 + 10;
const int inf = 1e9 + 7;
int n, m, ans[N][M], res;
char s[M];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) ans[i][j] = M;
  res = inf;
  int sum, e;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", &s[1]);
    bool f = false;
    for (int j = 1; j <= m; ++j)
      if (s[j] == '1') ans[i][j] = 0, f = true;
    for (int j = 1; j <= m; ++j) {
      if (s[j] == '1') {
        int k = m / 2;
        for (int l = 1; l <= k; ++l) {
          e = j - l;
          if (e <= 0) e = m + e;
          if (l < ans[i][e])
            ans[i][e] = l;
          else
            break;
        }
        for (int l = 1; l <= k; ++l) {
          e = j + l;
          if (e > m) e = e - m;
          if (l < ans[i][e])
            ans[i][e] = l;
          else
            break;
        }
      }
    }
    if (!f) res = -1;
  }
  if (res == -1) {
    printf("%d\n", res);
    return 0;
  }
  for (int i = 1; i <= m; ++i) {
    sum = 0;
    for (int j = 1; j <= n; ++j) {
      sum += ans[j][i];
    }
    if (i == 1) res = sum;
    res = min(sum, res);
  }
  printf("%d\n", res);
  return 0;
}
