#include <bits/stdc++.h>
using namespace std;
int n, m, f[11000], r[11000];
char s[11000];
int main() {
  scanf("%d%d", &n, &m);
  bool _(true);
  int c(0);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    c = 0;
    for (int j = 1; j <= m; j++) c += s[j] - '0';
    if (!c) _ = false;
    memset(f, 0x3f, sizeof f);
    for (int j = 1; j <= m; j++)
      if (s[j] == '1') f[j] = 0;
    for (int j = 2; j <= m; j++) f[j] = min(f[j], f[j - 1] + 1);
    for (int j = m - 1; j >= 1; j--) f[j] = min(f[j], f[j + 1] + 1);
    f[1] = min(f[1], f[m] + 1);
    f[m] = min(f[m], f[1] + 1);
    for (int j = 2; j <= m; j++) f[j] = min(f[j], f[j - 1] + 1);
    for (int j = m - 1; j >= 1; j--) f[j] = min(f[j], f[j + 1] + 1);
    for (int j = 1; j <= m; j++) r[j] += f[j];
  }
  int z(0x3f3f3f3f);
  for (int i = 1; i <= m; i++) z = min(z, r[i]);
  if (_)
    cout << z;
  else
    puts("-1");
  return 0;
}
