#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename X>
inline X abs(const X& a) {
  return a < 0 ? -a : a;
}
template <typename X>
inline X sqr(const X& a) {
  return a * a;
}
const int INF = INT_MAX / 2;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 100 + 3;
const int M = 10 * 1000 + 3;
int n, m;
char buf[M];
int a[N][M];
inline bool read() {
  if (scanf("%d%d", &n, &m) != 2) return false;
  for (int i = 0; i < int(n); i++) {
    assert(scanf("%s", buf) == 1);
    for (int j = 0; j < int(m); j++) a[i][j] = buf[j] - '0';
  }
  return true;
}
int lf[N][M], rg[N][M];
inline void solve() {
  int ans = INF;
  for (int i = 0; i < int(n); i++) {
    int cur;
    int last1 = -1;
    for (int j = 0; j < int(m); j++)
      if (a[i][j] == 1) last1 = j;
    cur = last1;
    if (last1 == -1) {
      puts("-1");
      return;
    }
    for (int j = 0; j < int(m); j++) {
      if (a[i][j] == 1) cur = j;
      lf[i][j] = cur;
    }
    int first1 = -1;
    for (int j = int(m) - 1; j >= 0; j--)
      if (a[i][j] == 1) first1 = j;
    assert(first1 != -1);
    cur = first1;
    for (int j = int(m) - 1; j >= 0; j--) {
      if (a[i][j] == 1) cur = j;
      rg[i][j] = cur;
    }
  }
  for (int j = 0; j < int(m); j++) {
    int cur = 0;
    for (int i = 0; i < int(n); i++) {
      int val = INF;
      {
        int d = abs(j - lf[i][j]);
        d = min(d, m - d);
        val = min(val, d);
      }
      {
        int d = abs(j - rg[i][j]);
        d = min(d, m - d);
        val = min(val, d);
      }
      assert(val < INF / 2);
      cur += val;
    }
    ans = min(ans, cur);
  }
  if (ans > INF / 2) ans = -1;
  cout << ans << endl;
}
int main() {
  assert(read());
  solve();
  return 0;
}
