#include <bits/stdc++.h>
using namespace std;
static int const int_max = numeric_limits<int>::max();
static int const int_min = numeric_limits<int>::min();
static int const ll_max = numeric_limits<long long>::max();
static int const ll_min = numeric_limits<long long>::min();
static int const ull_max = numeric_limits<unsigned long long>::max();
static int const ull_min = numeric_limits<unsigned long long>::min();
static int const double_max = numeric_limits<double>::max();
static int const double_min = numeric_limits<double>::min();
template <typename>
struct stdio_format_for_type {};
template <>
struct stdio_format_for_type<int> {
  static char const *const value;
};
char const *const stdio_format_for_type<int>::value = "%d";
template <>
struct stdio_format_for_type<long long> {
  static char const *const value;
};
char const *const stdio_format_for_type<long long>::value = "%I64d";
template <>
struct stdio_format_for_type<unsigned long long> {
  static char const *const value;
};
char const *const stdio_format_for_type<unsigned long long>::value = "%I64u";
template <>
struct stdio_format_for_type<double> {
  static char const *const value;
};
char const *const stdio_format_for_type<double>::value = "%lf";
template <typename E>
int read_back_vector(
    vector<E> &out, int size = int_max,
    char const *element_format = stdio_format_for_type<E>::value) {
  E elem = E();
  int i = 0;
  for (; i < size; ++i) {
    if (scanf(element_format, &elem) != 1) {
      break;
    }
    out.push_back(elem);
  }
  return i;
}
template <typename E>
void read_in_vector(
    vector<E> &out, int size,
    char const *element_format = stdio_format_for_type<E>::value) {
  out.resize(size);
  for (int i = 0; i < size; ++i) {
    assert(scanf(element_format, &out[i]) == 1);
  }
}
template <typename E>
void dump_named_vector(vector<E> const &v, char const *name) {
  int const size = v.size();
  (void)0;
  if (size) {
    (void)0;
  }
  for (int i = 1; i < size; ++i) {
    (void)0;
    (void)0;
  }
  (void)0;
}
bool print_preceding_space;
template <typename T>
void print(T const &item, FILE *out = stdout) {
  if (print_preceding_space) {
    fprintf(out, " ");
  }
  fprintf(out, stdio_format_for_type<T>::value, item);
  print_preceding_space = true;
}
template <typename T>
void println(T const &item, FILE *out = stdout) {
  print(item, out);
  fprintf(out, "\n");
  print_preceding_space = false;
}
char row[(10 * 1000) + 1];
int dists[(10 * 1000)];
int results[(10 * 1000)];
int main() {
  int n = int();
  assert(scanf(stdio_format_for_type<int>::value, &n) == 1);
  int m = int();
  assert(scanf(stdio_format_for_type<int>::value, &m) == 1);
  for (int i = 0; i < n; ++i) {
    scanf("%s", row);
    for (int j = 0; j < m; ++j) dists[j] = (1000 * 1000) + 1;
    for (int j = 0; j < 2 * m; ++j) {
      if (row[j % m] == '0') {
        dists[j % m] = dists[(j + m - 1) % m] + 1;
      } else
        dists[j % m] = 0;
    }
    for (int j = 2 * m - 1; j >= 0; --j) {
      if (row[j % m] == '0') {
        dists[j % m] = min(dists[j % m],
                           min(dists[(j + 1) % m], dists[(j + m - 1) % m]) + 1);
      } else
        dists[j % m] = 0;
    }
    for (int j = 0; j < m; ++j) results[j] += dists[j];
  }
  int res = *min_element(results, results + m);
  if (res > (1000 * 1000))
    println(-1);
  else
    println(res);
  return 0;
}
