#include <bits/stdc++.h>
using namespace std;
long long a[111][11111];
long long b[111][11111];
long long oo = 1e18;
int main() {
  int n, m;
  cin >> n >> m;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      a[i][j] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) b[i][j] = oo;
  long long cur;
  for (int i = 0; i < n; i++) {
    cur = oo;
    for (int j = 0; j < 2 * m; j++) {
      ++cur;
      if (a[i][j % m] == 1) cur = 0;
      b[i][j % m] = min(b[i][j % m], cur);
    }
    if (cur >= oo) {
      cout << -1 << endl;
      return 0;
    }
    cur = oo;
    for (int j = 2 * m - 1; j >= 0; j--) {
      ++cur;
      if (a[i][j % m] == 1) cur = 0;
      b[i][j % m] = min(b[i][j % m], cur);
    }
  }
  long long mini = 1e18, sum;
  for (int j = 0; j < m; j++) {
    sum = 0;
    for (int i = 0; i < n; i++) {
      sum += b[i][j];
    }
    mini = min(mini, sum);
  }
  cout << mini << endl;
}
