#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double PI = 3.1415926535897932384626433832795;
const long double E = 2.7182818284;
const int INF = 1000000000;
const int inf = 100000;
int main(void) {
  int n, m, i, j;
  char c;
  scanf("%d%d", &n, &m);
  char a[n + 2][m + 2];
  c = getchar();
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(m); j++) {
      scanf("%c", &a[i][j]);
    }
    c = getchar();
  }
  int tab[n + 2][m + 2];
  for (int i = 0; i < int(n); i++) {
    int t = inf;
    for (int j = 0; j < int(2 * m); j++) {
      if (a[i][j % m] == '1') {
        tab[i][j % m] = 0;
        t = 1;
      } else {
        tab[i][j % m] = t;
        if (t != inf) t++;
      }
    }
  }
  for (int i = 0; i < int(n); i++) {
    int t = inf;
    for (j = 2 * m - 1; j >= 0; j--) {
      if (a[i][j % m] == '1') {
        tab[i][j % m] = 0;
        t = 1;
      } else {
        tab[i][j % m] = min(tab[i][j % m], t);
        if (t != inf) t++;
      }
    }
  }
  int res = -1;
  for (int i = 0; i < int(m); i++) {
    int sum = 0;
    for (int j = 0; j < int(n); j++) {
      if (tab[j][i] == inf) {
        printf("-1\n");
        return 0;
      }
      sum += tab[j][i];
    }
    if (sum < res || res == -1) res = sum;
  }
  cout << res;
  return 0;
}
