#include <bits/stdc++.h>
using namespace std;
int n, m, flag, ans, sum;
string s[110];
int a[110][10010];
void bfs(string s, int z) {
  queue<int> q;
  for (int i = 0; i < m; i++)
    if (s[i] == '1') {
      a[z][i] = 0;
      q.push(i);
    }
  while (!q.empty()) {
    int now = q.front(), nxt;
    q.pop();
    nxt = (now + 1) % m;
    if (a[z][nxt] == -1) {
      a[z][nxt] = a[z][now] + 1;
      q.push(nxt);
    }
    nxt = now - 1;
    if (nxt < 0) nxt = m - 1;
    if (a[z][nxt] == -1) {
      a[z][nxt] = a[z][now] + 1;
      q.push(nxt);
    }
  }
}
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++) cin >> s[i];
  flag = 1;
  for (i = 0; i < n; i++) {
    int ans = 0;
    for (j = 0; j < s[i].size(); j++) ans += s[i][j] - '0';
    if (ans == 0) {
      flag = 0;
      break;
    }
  }
  if (!flag) {
    printf("-1\n");
    return 0;
  }
  memset(a, -1, sizeof(a));
  ans = m * n + 1;
  for (i = 0; i < n; i++) bfs(s[i], i);
  for (i = 0; i < m; i++) {
    int sum = 0;
    for (j = 0; j < n; j++) sum += a[j][i];
    if (sum < ans) ans = sum;
  }
  cout << ans << endl;
  return 0;
}
