#include <bits/stdc++.h>
using namespace std;
int n, m, a[101][10001], f[101][10001];
char s1[10001];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s1);
    for (int j = 1; j <= m; j++)
      if (s1[j - 1] == '0')
        a[i][j] = 0;
      else
        a[i][j] = 1;
  }
  for (int i = 1; i <= n; i++) {
    bool ok = false;
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 1) {
        ok = true;
        break;
      }
    if (!ok) {
      printf("-1\n");
      return 0;
    }
  }
  memset(f, 127, sizeof(f));
  for (int i = 1; i <= n; i++) {
    int now = 0;
    for (int j = m; j; --j)
      if (a[i][j] == 0)
        ++now;
      else
        break;
    for (int j = 1; j <= m; j++) {
      ++now;
      if (a[i][j] == 1) now = 0;
      if (now < f[i][j]) f[i][j] = now;
    }
    now = 0;
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 0)
        ++now;
      else
        break;
    for (int j = m; j; --j) {
      ++now;
      if (a[i][j] == 1) now = 0;
      if (now < f[i][j]) f[i][j] = now;
    }
  }
  int min = 1 << 30;
  for (int i = 1; i <= m; i++) {
    int now = 0;
    for (int j = 1; j <= n; j++) now += f[j][i];
    if (now < min) min = now;
  }
  printf("%d\n", min);
}
