#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[100][10001];
int closest[100][10000];
int l[100][10000];
int r[100][10000];
int main() {
  scanf("%d %d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s\n", s[i]);
    int id = -1;
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '1') id = j;
    }
    if (id == -1) {
      printf("-1");
      return 0;
    }
    l[i][id] = 0;
    r[i][id] = 0;
    for (int j = 1; j < m; j++) {
      if (s[i][(id + j) % m] == '1')
        l[i][(id + j) % m] = 0;
      else
        l[i][(id + j) % m] = l[i][(id + j - 1) % m] + 1;
      if (s[i][(((id - j) % m) + m) % m] == '1')
        r[i][(((id - j) % m) + m) % m] = 0;
      else
        r[i][(((id - j) % m) + m) % m] = r[i][(((id - j + 1) % m) + m) % m] + 1;
    }
  }
  int ans = 1 << 30;
  for (int i = 0; i < m; i++) {
    int currAns = 0;
    for (int j = 0; j < n; j++) {
      currAns += min(l[j][i], r[j][i]);
    }
    ans = min(ans, currAns);
  }
  printf("%d", ans);
  return 0;
}
