#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int t = 1;
  while (t--) {
    long long int n, m, i, j, ans = 999999999999999999;
    cin >> n >> m;
    string s[n];
    for (i = 0; i < n; i++) {
      cin >> s[i];
    }
    long long int a[n][m];
    for (i = 0; i < n; i++) {
      long long int l = 999999999999999999, r = 999999999999999999,
                    l1 = 999999999999999999, r1 = 999999999999999999;
      for (j = 0; j < m; j++) {
        if (s[i][j] == '1') {
          l = j;
          if (l1 == 999999999999999999) {
            l1 = l;
          }
          a[i][j] = 0;
        } else {
          if (l != 999999999999999999) {
            a[i][j] = min(j - l, m - j + l1);
          } else {
            a[i][j] = 999999999999999999;
          }
        }
      }
      for (j = m - 1; j >= 0; j--) {
        if (s[i][j] == '1') {
          r = j;
          if (r1 == 999999999999999999) {
            r1 = r;
          }
        } else {
          if (r != 999999999999999999) {
            a[i][j] = min(a[i][j], min(r - j, m - r1 + j));
          }
        }
      }
    }
    for (i = 0; i < m; i++) {
      long long int sum = 0;
      for (j = 0; j < n; j++) {
        if (a[j][i] == 999999999999999999) {
          break;
        } else {
          sum += a[j][i];
        }
      }
      if (j == n) {
        ans = min(ans, sum);
      }
    }
    if (ans == 999999999999999999) {
      cout << -1 << "\n";
    } else {
      cout << ans << "\n";
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
       << "\n";
  return 0;
}
