#include <bits/stdc++.h>
using namespace std;
string grid[110];
int l[110][10010], r[110][10010];
int main() {
  int n, m, step;
  bool one;
  cin >> n >> m;
  memset(l, -1, sizeof l);
  memset(r, -1, sizeof r);
  for (int i = 0; i < n; i++) cin >> grid[i];
  for (int i = 0; i < n; i++) {
    step = 0;
    one = 0;
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '1') step = 0, one = 1;
      step++;
    }
    for (int j = 0; j < m; j++) {
      if (grid[i][j] == '1') step = 0;
      if (one) l[i][j] = step;
      step++;
    }
    step = 0;
    one = 0;
    for (int j = m - 1; j >= 0; j--) {
      if (grid[i][j] == '1') step = 0, one = 1;
      step++;
    }
    for (int j = m - 1; j >= 0; j--) {
      if (grid[i][j] == '1') step = 0;
      if (one) r[i][j] = step;
      step++;
    }
  }
  int res = 1e9;
  for (int j = 0; j < m; j++) {
    int dist = 0;
    for (int i = 0; i < n; i++) {
      if (l[i][j] == -1 && r[i][j] == -1) {
        cout << "-1\n";
        return 0;
      }
      step = 1e9;
      if (l[i][j] != -1) step = l[i][j];
      if (r[i][j] != -1) step = min(step, r[i][j]);
      dist += step;
    }
    res = min(res, dist);
  }
  cout << res << "\n";
  return 0;
}
