#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int n, m;
int d[100][MAXN];
int l[MAXN], r[MAXN];
char s[MAXN];
bool visit[MAXN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) l[i] = (i - 1 + m) % m, r[i] = (i + 1) % m;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    queue<int> q;
    bool flag = false;
    for (int j = 0; j < m; j++) {
      visit[j] = false;
      if (s[j] == '1') {
        flag = true;
        q.push(j);
        visit[j] = true;
        d[i][j] = 0;
      }
    }
    if (!flag) {
      printf("-1\n");
      exit(0);
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      int v;
      v = l[u];
      if (!visit[v]) {
        visit[v] = true;
        d[i][v] = d[i][u] + 1;
        q.push(v);
      }
      v = r[u];
      if (!visit[v]) {
        visit[v] = true;
        d[i][v] = d[i][u] + 1;
        q.push(v);
      }
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < m; i++) {
    int temp = 0;
    for (int j = 0; j < n; j++) temp += d[j][i];
    ans = min(ans, temp);
  }
  cout << ans << endl;
  return 0;
}
