#include <bits/stdc++.h>
using namespace std;
bool t[200][20000], p[200][20000];
int d[200][20000];
int main() {
  int n, m;
  char c;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c;
      t[i][j] = (c == '1');
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (t[i][j]) {
        p[i][j] = true;
        c = 1;
        for (int k = (j + 1) % m; !t[i][k]; k = (k + 1) % m, c++) {
          if (!p[i][k] || c < d[i][k]) {
            d[i][k] = c;
            p[i][k] = true;
          } else
            break;
        }
        c = 1;
        for (int k = (j + m - 1) % m; !t[i][k]; k = (k + m - 1) % m, c++) {
          if (!p[i][k] || c < d[i][k]) {
            d[i][k] = c;
            p[i][k] = true;
          } else
            break;
        }
      }
  int mx = -1;
  for (int j = 0; j < m; j++) {
    int z = 0;
    bool ok = true;
    for (int i = 0; i < n && ok; i++) {
      if (!p[i][j])
        ok = false;
      else
        z += d[i][j];
    }
    if (ok && (mx == -1 || z < mx)) mx = z;
  }
  cout << mx;
  return 0;
}
