#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000007;
const long long linf = 1000000000000000000LL;
const double eps = 0.000001;
const double pi = 3.14159265358979323846;
template <class T>
T abs(T k) {
  return k >= 0 ? k : -k;
}
template <class T>
T sqr(T n) {
  return n * n;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T mod(T a, T b) {
  return (a % b + b) % b;
}
template <class T>
void mode(T &a, T b) {
  a = mod(a, b);
}
template <class T>
void maxe(T &a, T b) {
  a = max(a, b);
}
template <class T>
void mine(T &a, T b) {
  a = min(a, b);
}
void fileio_in_out() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}
void fileio_txt() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int test;
int n, m;
int s[33333], a[111][33333];
char c[111][33333], str[33333];
int ans() {
  for (int i = 0; i < n; i++) {
    strcpy(str, c[i]);
    strcat(c[i], str);
    strcat(c[i], str);
    for (int j = 1; j <= 3 * m; j++) s[j] = s[j - 1] + (c[i][j - 1] == '1');
    if (!s[m]) return -1;
    for (int j = m + 1; j <= 2 * m; j++) {
      int l = j, r = 3 * m, mid;
      while (l < r) {
        mid = (l + r) >> 1;
        if (s[mid] > s[j - 1])
          r = mid;
        else
          l = mid + 1;
      }
      a[i][j] = l - j;
      if (s[j]) {
        l = 0, r = j - 1;
        while (l < r) {
          mid = (l + r + 1) >> 1;
          if (s[mid] < s[j])
            l = mid;
          else
            r = mid - 1;
        }
        mine(a[i][j], j - 1 - l);
      }
    }
  }
  int ret = inf;
  for (int j = m + 1; j <= 2 * m; j++) {
    int t = 0;
    for (int i = 0; i < n; i++) t += a[i][j];
    mine(ret, t);
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", c[i]);
  printf("%d\n", ans());
  return 0;
}
