#include <bits/stdc++.h>
using namespace std;
int a[105][10005];
int der[105][10005], izq[105][10005];
int minDer[105], maxIzq[105];
string t[105];
int INF = 1 << 20;
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) cin >> t[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < t[i].size(); j++) {
        if (t[i][j] == '1')
          a[i + 1][j + 1] = 1;
        else
          a[i + 1][j + 1] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (a[i][1])
        der[i][1] = 1;
      else
        der[i][1] = -1 * INF;
      if (a[i][1] == 0)
        minDer[i] = INF;
      else
        minDer[i] = 1;
      for (int j = 2; j <= m; j++) {
        if (a[i][j] == 1)
          der[i][j] = j;
        else
          der[i][j] = der[i][j - 1];
        if (a[i][j]) minDer[i] = min(minDer[i], der[i][j]);
      }
      if (minDer[i] == INF) minDer[i] = -1 * INF;
    }
    for (int i = 1; i <= n; i++) {
      if (a[i][m])
        izq[i][m] = m;
      else
        izq[i][m] = -1 * INF;
      if (a[i][m] == 0)
        maxIzq[i] = 0;
      else
        maxIzq[i] = m;
      for (int j = m - 1; j >= 1; j--) {
        if (a[i][j] == 1)
          izq[i][j] = j;
        else
          izq[i][j] = izq[i][j + 1];
        if (a[i][j]) maxIzq[i] = max(maxIzq[i], izq[i][j]);
      }
      if (maxIzq[i] == 0) maxIzq[i] = INF;
    }
    int mini = 1 << 20;
    for (int j = 1; j <= m; j++) {
      int res = 0;
      for (int i = 1; i <= n; i++) {
        int add = min(abs(j - der[i][j]), abs(izq[i][j] - j));
        add = min(add, min(abs(minDer[i]) + m - j, abs(m - maxIzq[i]) + j));
        res += add;
      }
      mini = min(res, mini);
    }
    if (mini >= (1000000))
      puts("-1");
    else
      printf("%d\n", mini);
  }
}
