#include <bits/stdc++.h>
using namespace std;
char a[105][10005];
long long bins(long long ar[], long long n, long long x) {
  long long l = 0, r = n - 1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (ar[mid] > x)
      r = mid - 1;
    else if (ar[mid] < x)
      l = mid + 1;
    else
      return mid + 1;
  }
  return l + 1;
}
bool seive(long long a) {
  for (long long i = 2; i <= sqrt(a); i++) {
    if (a % i == 0) return false;
  }
  return true;
}
const int inf = (long long)1e9;
int main() {
  int q;
  q = 1;
  while (q--) {
    long long n, m;
    cin >> n >> m;
    vector<vector<long long> > v;
    v.assign(101, vector<long long>());
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        cin >> a[i][j];
        if (a[i][j] == '1') v[i].push_back(j);
      }
    }
    long long c = 0;
    for (long long i = 0; i < m; i++) {
      c = 0;
      for (long long j = 0; j < n; j++) {
        if (a[j][i] == '1') c++;
      }
      if (c == n) {
        cout << 0;
        return 0;
      }
    }
    long long dd = 100000000;
    long long cnt = 0;
    for (long long j = 0; j < m; j++) {
      cnt = 0;
      for (long long i = 0; i < n; i++) {
        if (v[i].size() == 0) {
          cout << -1;
          return 0;
        }
        long long qq;
        long long tt;
        long long rr;
        long long ans = 100000;
        long long l = 0;
        long long h = v[i].size() - 1;
        long long y2 = v[i][0];
        long long y1 = v[i][v[i].size() - 1];
        long long mid;
        while (l <= h) {
          mid = l + (h - l) / 2;
          if (v[i][mid] <= j) {
            y1 = v[i][mid];
            l = mid + 1;
          } else
            h = mid - 1;
        }
        l = 0;
        h = v[i].size() - 1;
        while (l <= h) {
          mid = l + (h - l) / 2;
          if (v[i][mid] >= j) {
            y2 = v[i][mid];
            h = mid - 1;
          } else
            l = mid + 1;
        }
        tt = min(abs(j - y2), m - abs(j - y2));
        qq = min(abs(j - y1), m - abs(j - y1));
        rr = min(tt, qq);
        cnt += rr;
      }
      if (cnt < dd) dd = cnt;
    }
    cout << dd;
  }
  return 0;
}
