#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1};
const int inf = 0x3f3f3f3f;
const int maxn = 110;
const int maxm = 1e4 + 10;
int n, m;
char niz[maxn][maxm];
int dis[maxn][maxm];
queue<int> q;
int main() {
  memset(dis, -1, sizeof dis);
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", niz + i);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (niz[i][j] == '1') {
        q.push(j);
        dis[i][j] = 0;
      }
    }
    if (q.empty()) {
      printf("-1\n");
      return 0;
    }
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int y = 0; y < 2; y++) {
        int tx = x + dx[y];
        tx += m, tx %= m;
        if (dis[i][tx] != -1) continue;
        dis[i][tx] = dis[i][x] + 1;
        q.push(tx);
      }
    }
  }
  int sol = inf;
  for (int i = 0; i < m; i++) {
    int ac = 0;
    for (int j = 0; j < n; j++) ac += dis[j][i];
    sol = min(sol, ac);
  }
  printf("%d", sol);
  return 0;
}
