#include <bits/stdc++.h>
using namespace std;
vector<int> v[100 + 10];
int grid[100 + 10][10010];
int data[10010];
int main() {
  int n, m, p, q, a, b, pos, last, mn;
  char ch;
  bool chk = true;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    v[i].push_back(0);
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == '1') v[i].push_back(j);
    }
    if (v[i].size() == 1) chk = false;
  }
  if (!chk)
    cout << "-1" << endl;
  else {
    for (int i = 1; i <= n; i++) {
      for (int j = 2; j < v[i].size(); j++) {
        a = v[i][j - 1];
        b = v[i][j];
        pos = a + 1;
        while (pos <= b) {
          p = pos - a;
          q = b - pos;
          grid[i][pos] = p > q ? q : p;
          pos++;
        }
      }
      pos = 1;
      a = v[i][1];
      last = m - v[i][v[i].size() - 1];
      while (pos <= a) {
        p = pos + last;
        q = a - pos;
        grid[i][pos] = p > q ? q : p;
        pos++;
      }
      a = v[i][v[i].size() - 1];
      last = v[i][1];
      pos = a + 1;
      while (pos <= m) {
        p = pos - a;
        q = m - pos + last;
        grid[i][pos] = p > q ? q : p;
        pos++;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        data[j] += grid[i][j];
      }
    }
    mn = INT_MAX;
    for (int i = 1; i <= m; i++) {
      mn = data[i] > mn ? mn : data[i];
    }
    cout << mn << endl;
  }
  return (0);
}
